recursion in Python extra brackets -


not sure went wrong, gives me [-1, [-2, [-3, []]]] instead of [-1,-2,-3]

def search(list1,list2):     if list2 == []:         return list1     elif list2[0] == list1[0]:         return [-list1[0] , search(list1[1:],list2[1:])]  print search([1,2,3],[1,2,3]) 

i'm not entirely sure goal function aimed @ (why return negative of matching value?), can expected output concatenating lists in return statement:

def search(list1,list2):     if list2 == []:         return list1     elif list2[0] == list1[0]:         return [-list1[0]] + search(list1[1:],list2[1:])  print search([1,2,3],[1,2,3]) # output: # [-1, -2, -3] 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -