0

What's wrong with this simple duplicate-checker?

def dup(x): for k in x: for z in x: if x.index(k) != x.index(z): if k == z: return True return False print(dup([1,3,2,1]))

26th Feb 2021, 6:34 PM
madeline
madeline - avatar
7 odpowiedzi
+ 3
it’s not so much that it’s too complex, but rather, you seem to be trying to scissor walk backwards so you can get in your car more easily without having to turn around when you sit down.
26th Feb 2021, 6:49 PM
Wilbur Jaywright
Wilbur Jaywright - avatar
+ 1
I think by trying to make this more efficient using a double for loop, you’ve actually made it more confusing. What I recommend is adding the characters in the string to a new string as you go through them, but first checking if they’re not in the string already.
26th Feb 2021, 6:42 PM
Wilbur Jaywright
Wilbur Jaywright - avatar
+ 1
what you can also do is use the count method of the string object for each character, bucking the loop if it returns true.
26th Feb 2021, 6:45 PM
Wilbur Jaywright
Wilbur Jaywright - avatar
0
yeah, I think a better way is to do set(x) == x. but it should still work.
26th Feb 2021, 6:43 PM
madeline
madeline - avatar
0
I’m sorry, I mean if it returns >1
26th Feb 2021, 6:46 PM
Wilbur Jaywright
Wilbur Jaywright - avatar
0
ok.. that would work, my code is probably too complex and there's something wrong with it internally that i don't understand. oh well, i will use another method!
26th Feb 2021, 6:48 PM
madeline
madeline - avatar
0
hahaha, "True"
26th Feb 2021, 6:50 PM
madeline
madeline - avatar