+ 1
why does this code print [1, 0]?
def find_uniq(arr): n = [] for num in arr: if num != arr[0]: n.append(num) arr.remove(num) else: continue if len(n) == 1: return n else: return arr print(find_uniq([ 1, 0, 0, 0 ]))
7 Antworten
+ 2
madeline no problem, iam also learning, thank you for even taking your time, that means a lot
+ 2
~ swim ~
i dont know if it is the most practical way to do it or not but it worked perfectly for me so thank you.
+ 1
i think it has to do with the index reference arr[0]... try this:
def find_uniq(arr):
n = []
if len(arr) == 1:
return arr
else:
for x in range(0, len(arr)-1):
if arr[x] != arr[x-1]:
n.append(arr[x])
arr.remove(arr[x])
else:
return n
+ 1
~ swim ~ that was the best way you could explain the problem, but iam not quite sure how to fix it, can you help me with that?
+ 1
madeline it doesnt seem to work, for example when the input is [2, 2, 2, 1, 2] it returns an empty list
+ 1
maybe try to add “if len(arr) == 1:“ after the last else? sorry im still learning 😳
0
Kirill Vidov no problem at all!!!