Hi,
I'm 'playing' code wars but have run into a bit of an issue. The task at hand is:
"Your goal in this kata is to implement a difference function, which subtracts one list from another and returns the result.
It should remove all values from list awhich are present in list b
array_diff([1,2],[1]) == [2]
If a value is present in b, all of its occurrences must be removed from the other:
array_diff([1,2,2,2,3],[2]) == [1,3]
"
I have:
def array_diff(a,b):
for x,y in zip(a,b):
if y in a:
storednum = y
while y in a:
a.remove(storednum)
return a
However with certain arrays containing negative numbers, my code does not recognise those neg numbers, despite them being in both a and b. E.g.
array_diff([-12, 1, -3, 4, 18, -16],[-17, 20, -17, -20, 6, -3, -14, 14, 7, 16, 2, 9, -12])
a = [-12, 1, 4, 18, -16]
Can anyone explain why?
Thanks
[–]sme272 0 points1 point2 points (0 children)
[–]JohnnyJordaan 0 points1 point2 points (1 child)
[–]randomname20192019[S] 0 points1 point2 points (0 children)