-1

I need to compare the return value of a recursive function with an integer. Doing recursively the sum of all elements of a list , I have to compare the final sum with an integer "n" giving back TRUE if sum == n , FALSE if sum != n . In addiction to the function have to return FALSE if i'm giving an empty list . Here I report the code to clarify the situation :)

def function(list_of_numbers,int):

   if not list:

     return false # I have to return false if list is empty.

   if len(l) > 0:

     return l[0] + function(list_of_numbers[1:],int) # recursive sum of element

   # and here i'm stuck !
Kartoch
  • 7,610
  • 9
  • 40
  • 68
  • 1
    What is the question ? – Kartoch Dec 18 '14 at 16:01
  • I'm confused. What is your function supposed to return? The sum of some ints? Or a boolean value? – Kevin Dec 18 '14 at 16:03
  • I think you should use two functions. The function ends once a return statement is reached, so the function would just return the sum. – David Reeve Dec 18 '14 at 16:03
  • `l`, `list`, `list_of_numbers`; which one is it? And what is `int` supposed to be/do? Please provide runnable code. –  Dec 18 '14 at 16:06
  • You shouldn't let a (recursive) function return two types of values (see Kevin's comment). You could return both though, a bool and an integer. If this is an assignment ("function have to return FALSE if i'm giving an empty list" and "compare the return value of a recursive function with an integer."), that seems an odd fit, to have a function either return False or an integer. Better raise some Exception if an empty listed is given as argument. –  Dec 18 '14 at 16:08

1 Answers1

1

when not l we either got passed an empty list or have reached our base case so compare n to our test number, if you want an empty list to return True change test=-1 to test=0 :

def function(l, n=0, test=-1):
    if not l:
        return n == test
    else:
        n += l[0]
        return function(l[1:], n, test)


In [2]: function([1,2,3],test=6)
Out[2]: True

In [3]: function([1,2,3],test=5)
Out[3]: False

In [4]: function([1,2,3])
Out[4]: False

In [5]: function([])
Out[5]: False

If you want an empty list to return False you can check how many times the function has been called and either compare n to test or return False :

def function(l, n=0, test=0, calls=0):
    if not l:
        return n == test if calls > 0 else False
    else:
        n += l[0]
        calls += 1
        return function(l[1:], n, test,calls)

If you just want to pass a single argument the number to test against:

def function(l, test, n=0, calls=0):
    if not l and calls == 0: # changed for jython
        return False
    if not l:
        return n == test 
    else:
        n += l[0]
        calls += 1
        return function(l[1:],test,n, calls)
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • I had an idea similar to this, except it only had two arguments. The one line version being: `function = lambda l, value: value == 0 if not l else function(l[1:], value-l[0])` – Kevin Dec 18 '14 at 16:15
  • `function([], test=0)` returns True, which seems to be contradictory with the requirements. – njzk2 Dec 18 '14 at 16:20
  • @njzk2, there are different ways of interpreting the question, I would not put much heed in the OP's code considering it would not even run but I added a case to handle when a list is empty and have it return False – Padraic Cunningham Dec 18 '14 at 16:31
  • @Kevin, probably a nicer implementation than mine for a straight forward implementation – Padraic Cunningham Dec 18 '14 at 16:32
  • to clarify : if I call my function like this --> function([1,2,3],6) this must return TRUE (1+2+3 = 6 , 6 = 6) function([1,2,3],5) this must return FALSE (1+2+3 = 6 , 5 != 6) function([],4) this must return FALSE (list is empty) – Roberto Pavia Dec 18 '14 at 16:40
  • @RobertoPavia, that is what the function does – Padraic Cunningham Dec 18 '14 at 16:42
  • @PadraicCunningham But I need to give only 2 parameter to my function like --> function(list,int) (this exercise has to be done without using test or calls variables) – Roberto Pavia Dec 18 '14 at 16:48
  • @RobertoPavia, the last example uses only two arguments, you don't have to pass anything else – Padraic Cunningham Dec 18 '14 at 16:49
  • @PadraicCunningham Forgive me for stubbornness but i'm writing this code in JES (jython) and it doesn't work ! There is a problem with " return n == test if calls > 0 else False " . I think syntax problem – Roberto Pavia Dec 18 '14 at 16:53
  • @PadraicCunningham ok! now works ! I'm gonna try it again to understand better .. Thanks Friend :) – Roberto Pavia Dec 18 '14 at 17:02
  • no worries, add some print statements and it will make it easier to understand what is happening – Padraic Cunningham Dec 18 '14 at 17:03
  • can you please explain what "return something == something else"? is something I never seen before – Roberto Pavia Dec 18 '14 at 17:57
  • it is just using the comparative operator, it will return True or False based on whether the elements are equal or not – Padraic Cunningham Dec 18 '14 at 18:02
  • It was a plasure , i've learn more with you than with my professor .. thanks man :) – Roberto Pavia Dec 18 '14 at 20:31