To make the union, you can modify the function by adding the lists a
and b
and turning them into a set
(who is responsible for removing duplicate values).
Would look like this:
def union(x, y):
aux3 = ''
s = set(x + y)
for i in s:
aux3 += str(i) + ' '
return aux3
a = [7, 2, 5, 8, 4]
b = [4, 2, 9, 5]
print(union(a, b))
EDIT: If you can’t use set
, maybe this will solve the problem:
def union(x, y):
aux3 = ''
x.extend(i for i in y if i not in x)
for i in x:
aux3 += str(i) + ' '
return aux3
a = [7, 2, 5, 8, 4]
b = [4, 2, 9, 5]
print(union(a, b))
Or even if you prefer to use your original code, just left to add the numbers in the list aux2
:
def union (x,y):
aux1 = x+y
aux2 = []
aux3 = ""
for i in aux1:
if i not in aux2:
aux3 += str(i) + ' '
aux2.append(i) # ou aux2 += [i]
return aux3
a = [7, 2, 5, 8, 4]
b = [4, 2, 9, 5]
print(union(a,b))
So my teacher, is forcing the staff to n use no list function other than Len, only the vector functions we can use
– Luis Henrique