#File: Erin/intersection.py
"""Given two lists, computes the
union and intersection of the two lists. Improve solution
list1? 4 5 6
list2? 6 7 8 9
L1 = ['4', '5', '6'] L2 = ['6', '7', '8', '9']
intersection = ['6']
union = ['4', '5', '6', '7', '8', '9']
"""
def intersection(L1, L2):
R = [] #R result nothing inside
for e in L1: # conmpare all with all
for k in L2:
if ( e == k):
R.append(e) # only if they are equal put in 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( "list1? " ).split( " " ) # input
L2 = input( "list2? " ).split( " " )
print ("L1 =", L1, "L2 =", L2)
print ("intersection =", intersection( L1,L2 ))
print ("union =", union( L1, L2 ))
main()