In Python:
Sublist of list A is defined as a list whose elements are all from list A. For example, suppose list A = [0, 1, 2, 3, 4, 5, 6], its has many sublists and one of them is [0, 1, 3] because elements 0, 1 and 3 are all contained in list A.
Define a function named returnComplement that accepts two integer lists as the parameter (one of the list is the sublist of the other). Suppose names of these two parameters are A and B, and list B is a sublist of list A. Your function should return a list containing elements that are in list A but not in list B. Test your function in the main. For example, suppose A = [0, 1, 2, 3, 4, 5, 6] and B = [0, 1, 3], then your function should return list [2, 4, 5, 6].
Python code pasted below.
#function definition
def returnComplement(list1,list2):
#convert both list1 and list2 to set
set1=set(list1)
set2=set(list2)
#apply the set difference to get the complement
set1.difference_update(set2)
#convert the result back to list
result=list(set1)
return result
#main program
#function calling
print(returnComplement([0,1,2,3,4,5,6],[0,1,3]))
Python code in IDLE pasted for better understanding of the indent.
Output Screen
Get Answers For Free
Most questions answered within 1 hours.