list - Python recursive no 'in' work around -


so recursive function in list , see if item repeated in list already. ex. l = [1,2,3,4,3] return true. i've completed im not allowed use 'in' function dont know work around.

edit: built in functions allowed use len, , index , splice operators.

def has_repeats(l):     if l == []:         return false     elif l[0] in l[1:]:         return true     else:         return has_repeats(l[1:])     return false 

you may consider using recursion compare list in reversed order indexes same, since index @ default returns first occurrence, this:

def has_repeats(l):     if len(l) <= 1:         return false     if l.index(l[-1]) != len(l) - 1:         return true     return has_repeats(l[:-1]) 

usage:

has_repeats([1, 2, 3, 4, 3]) true  has_repeats([1, 2, 3, 4, 0]) false 

so check index of last item len(l) - 1 same l.index should return first occurrence, if don't match, there duplicate before last item, , recursively.


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -