Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Complete the following Python CODE. Only modify this file. Put code in after the

ID: 3745335 • Letter: C

Question

 Complete the following Python CODE. Only modify this file. Put code in after the line ######### YOUR CODE STARTS HERE ################   #### Note: In the case of dictionaries, use the keys as the determining factor for all work on the data structures  #### Note: You can give preference to data structure a over b, but no use case will require that you have done this. ####      This means if a is a dictionary with keys "a","b", and "c" and b is a dictionary with key of "a" ####      You will not be tested as two which value for key "a" would be in the result  #### Note: You will only be tested in which both data structures are the same type, no a is a list and b is a tuple. ####      The result of the methods will be tested to ensure they are the same data structure as a and b    def unionOfTwoDataStructures(a,b):    result = None    #Find the concatenation of the data structures (with no uniqueness guarantee) a and b and set the return variable 'result' to this value    #Note: if you ensure that each value from a and b only occurs once, it may make other methods easier to complete    #Note: While using concatenation will get you most of the way to this, it will not result in only unique values    if type(a) is  list and type(b) is  list:       ######### YOUR CODE STARTS HERE ################            ######### YOUR CODE ENDS HERE ################    elif type(a) is tuple and type(b) is  tuple:       ######### YOUR CODE STARTS HERE ################        ######### YOUR CODE ENDS HERE ################    elif type(a) is dict and type(b) is  dict:       ######### YOUR CODE STARTS HERE ################              ######### YOUR CODE ENDS HERE ################    else:       print "a and b have to both be either lists, tuples, or dictionaries"    return  result  def intersectionOfTwoDataStructures(a,b):    result = None    #Find the elements of both of the data structures a and b and set the return variable 'result' to this value    #Note: if you ensure that each value from a and b only occurs once, it may make other methods easier to complete    if type(a) is  list and type(b) is  list:       ######### YOUR CODE STARTS HERE ################        ######### YOUR CODE ENDS HERE ################    elif type(a) is tuple and type(b) is  tuple:       ######### YOUR CODE STARTS HERE ################        ######### YOUR CODE ENDS HERE ################    elif type(a) is dict and type(b) is  dict:       ######### YOUR CODE STARTS HERE ################        ######### YOUR CODE ENDS HERE ################    else:       print "a and b have to both be either lists, tuples, or dictionaries"    return  result  def symmetricDifferenceOfTwoDataStructures(a,b):    result = None    #Find the elements that are not common to both data structures a and b and set the return variable 'result' to this value    #Note: you may be able to use other methods together to get this done without much extra code, look up venn diagrams to see    #     how this could happen    ######### YOUR CODE STARTS HERE ################    if type(a) is  list and type(b) is  list:       ######### YOUR CODE STARTS HERE ################        ######### YOUR CODE ENDS HERE ################    elif type(a) is tuple and type(b) is  tuple:       ######### YOUR CODE STARTS HERE ################        ######### YOUR CODE ENDS HERE ################    elif type(a) is dict and type(b) is  dict:       ######### YOUR CODE STARTS HERE ################        ######### YOUR CODE ENDS HERE ################    else:       print "a and b have to both be either lists, tuples, or dictionaries"    ######### YOUR CODE ENDS HERE ################    return  result   def equalityOfTwoDataStructures(a,b):    result = None    #Determine if the two data structures passed in as variables are equivalent and place the associated    #  boolean value of True or False into the variable result    ######### YOUR CODE STARTS HERE ################    if type(a) is  list and type(b) is  list:       #Note: Equality of two lists means that they contain all of the same values, it does not mean       #     mean that both lists have them in same order or do not include duplicates       ######### YOUR CODE STARTS HERE ################        ######### YOUR CODE ENDS HERE ################    elif type(a) is tuple and type(b) is  tuple:       ######### YOUR CODE STARTS HERE ################        ######### YOUR CODE ENDS HERE ################    elif type(a) is dict and type(b) is  dict:       ######### YOUR CODE STARTS HERE ################        ######### YOUR CODE ENDS HERE ################    else:       print "a and b have to both be either lists, tuples, or dictionaries"    ######### YOUR CODE ENDS HERE ################    return  result   def test_all_methods(a,b):    result = []    result.append(unionOfTwoDataStructures(a,b))    result.append(intersectionOfTwoDataStructures(a,b))    result.append(symmetricDifferenceOfTwoDataStructures(a,b))    result.append(equalityOfTwoDataStructures(a,b))    return result

Explanation / Answer

Python:

#### Note: In the case of dictionaries, use the keys as the determining factor for all work on the data structures

#### Note: You can give preference to data structure a over b, but no use case will require that you have done this.
####      This means if a is a dictionary with keys "a","b", and "c" and b is a dictionary with key of "a"
####      You will not be tested as two which value for key "a" would be in the result

#### Note: You will only be tested in which both data structures are the same type, no a is a list and b is a tuple.
####      The result of the methods will be tested to ensure they are the same data structure as a and b

def unionOfTwoDataStructures(a,b):
    result = None
   #Find the concatenation of the data structures (with no uniqueness guarantee) a and b and set the return variable 'result' to this value
   #Note: if you ensure that each value from a and b only occurs once, it may make other methods easier to complete
   #Note: While using concatenation will get you most of the way to this, it will not result in only unique values
    if type(a) is list and type(b) is list:
      ######### YOUR CODE STARTS HERE ################
        result=[]
        for i in a:
            result.append(i)
        for i in b:
            result.append(i)

   ######### YOUR CODE ENDS HERE ################
    elif type(a) is tuple and type(b) is tuple:
      ######### YOUR CODE STARTS HERE ################
     
        result=[]
        for i in list(a):
            result.append(i)
        for i in list(b):
            result.append(i)
        result=tuple(result)

      ######### YOUR CODE ENDS HERE ################
    elif type(a) is dict and type(b) is dict:
      ######### YOUR CODE STARTS HERE ################
        result={}
        for i in a:
            result[i]=a[i]
        for i in b:
            if(i not in result):        ## I am giving priority to a over b so if element is in a then consider it as prior.
                result[i]=b[i]
    
      ######### YOUR CODE ENDS HERE ################
    else:
        print("a and b have to both be either lists, tuples, or dictionaries")
    return result

def intersectionOfTwoDataStructures(a,b):
    result = None
   #Find the elements of both of the data structures a and b and set the return variable 'result' to this value
   #Note: if you ensure that each value from a and b only occurs once, it may make other methods easier to complete
    if type(a) is list and type(b) is list:
      ######### YOUR CODE STARTS HERE ################
        result=[]
        for i in a:
            if(i in b):
                result.append(i)
        for i in b:
            if(i in a and i not in b):
                result.append(i)
      ######### YOUR CODE ENDS HERE ################
    elif type(a) is tuple and type(b) is tuple:
      ######### YOUR CODE STARTS HERE ################
        result=[]
        for i in list(a):
            if(i in b):
                result.append(i)
        for i in list(b):
            if(i in a and i not in b):
                result.append(i)
        result=tuple(result)
      ######### YOUR CODE ENDS HERE ################
    elif type(a) is dict and type(b) is dict:
      ######### YOUR CODE STARTS HERE ################
        result={}
        for i in a:
            if(i in b):
                result[i]=a[i]          ## Here i am again giving priority to a over b.
      ######### YOUR CODE ENDS HERE ################
    else:
        print("a and b have to both be either lists, tuples, or dictionaries")
    return result

def symmetricDifferenceOfTwoDataStructures(a,b):
  
    result = None
   #Find the elements that are not common to both data structures a and b and set the return variable 'result' to this value
   #Note: you may be able to use other methods together to get this done without much extra code, look up venn diagrams to see
   #     how this could happen
   ######### YOUR CODE STARTS HERE ################
    if type(a) is list and type(b) is list:
      ######### YOUR CODE STARTS HERE ################
       result=[]
       for i in a:
           if(i not in b):
               result.append(i)
       for i in b:
           if(i not in a):
               result.append(i)
      ######### YOUR CODE ENDS HERE ################
    elif type(a) is tuple and type(b) is tuple:
      
      ######### YOUR CODE STARTS HERE ################
        result=[]
        for i in list(a):
            if(i not in b):
                result.append(i)
        for i in list(b):
            if(i not in a):
                result.append(i)
        result=tuple(result)
      ######### YOUR CODE ENDS HERE ################
    elif type(a) is dict and type(b) is dict:
      ######### YOUR CODE STARTS HERE ################
        result={}
        for i in a:
            if(i not in b):
                result[i]=a[i]
        for i in b:
            if(i not in a):
                result[i]=b[i]
      ######### YOUR CODE ENDS HERE ################
    else:
        print("a and b have to both be either lists, tuples, or dictionaries")
   ######### YOUR CODE ENDS HERE ################
    return result

def equalityOfTwoDataStructures(a,b):
    result = None
   #Determine if the two data structures passed in as variables are equivalent and place the associated
   # boolean value of True or False into the variable result
   ######### YOUR CODE STARTS HERE ################
    if type(a) is list and type(b) is list:
        result=True
        for i in a:
            if(i not in b):
                result=False
        for i in b:
            if(i not in a):
                result=False
          
      #Note: Equality of two lists means that they contain all of the same values, it does not mean
      #     mean that both lists have them in same order or do not include duplicates
      ######### YOUR CODE STARTS HERE ################
      ######### YOUR CODE ENDS HERE ################
    elif type(a) is tuple and type(b) is tuple:
      
      ######### YOUR CODE STARTS HERE ################
        result=True
        for i in list(a):
            if(i not in b):
                result=False
        for i in list(b):
            if(i not in a):
                result=False
      ######### YOUR CODE ENDS HERE ################
    elif type(a) is dict and type(b) is dict:
      ######### YOUR CODE STARTS HERE ################
        result=True
        for i in a:
            if(i not in b):
                result=False
        for i in b:
            if(i not in a):
                result=False
      ######### YOUR CODE ENDS HERE ################
    else:
        print("a and b have to both be either lists, tuples, or dictionaries")
   ######### YOUR CODE ENDS HERE ################
    return result


def test_all_methods(a,b):
    result = []
    result.append(unionOfTwoDataStructures(a,b))
    result.append(intersectionOfTwoDataStructures(a,b))
    result.append(symmetricDifferenceOfTwoDataStructures(a,b))
    result.append(equalityOfTwoDataStructures(a,b))
    return result