#File: ChapterLists/intersection.py """ Union and intersection of the two lists as sets Enter ints (Ex: 3 4 5)? 4 5 6 Enter ints (Ex: 3 4 5)? 5 1 3 8 L1 = [4, 5, 6] L2 = [5, 1, 3, 8] intersection = [5] union = [4, 5, 6, 1, 3, 8] """ def intersection(L1, L2): R = [] # R result nothing inside for e in L1: # compare all with all for k in L2: if ( e == k): R.append(e) # only if they are equal add to R return R def union(L1, L2): L = L1 + L2 # put all toghether R = [] [ R.append(x) for x in L if x not in R ] # remove duplicates return R def main(): L1 = input( "Enter ints (Ex: 3 4 5)? ").split( " " ) # input L2 = input( "Enter ints (Ex: 3 4 5)? ").split( " " ) L1 = [int(x) for x in L1] L2 = [int(x) for x in L2] print ("L1 =", L1, "L2 =", L2) print ("intersection =", intersection( L1,L2 )) print ("union =", union( L1, L2 )) main()