#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Wrong result from recursive func. in Python


    Dear all,

    I am quite new in Python. I want to write very simple function which gets an integer "c" and a list of integers "li". I would like to recursively find first two items where sum of them will be equal to "c". I have written this function in Python to find second option "n" and then I can easily find the first number by finding "c-n".

    My code is:

    Code:
    def shop(c, li):
            if ((c-li[0]) in li[1:]):
                    return li[0];
            else:
                    shop(c, li[1:]);
    The problem is when I call function as below:

    Code:
    c=100;
    li=[5, 75, 25];
    
    ii=shop(c, li);
    
    print ii, "\n";
    The result is always "None", however it should return "25". When I put a "print" statement before "return" I can see that the function has found the item correctly, but I am not sure why it is not returned.

    Anybody know why this happens?

    Thanks a lot!
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480

    Repair


    Code:
    def shop(c, li):
        if ((c-li[0]) in li[1:]):
            return li[0]
        else:
            return shop(c, li[1:])
    Python functions, by default, return None. When the test failed (in other words the value is not in list) the else block evaluated, and then the function returned None.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0
    Thanks a lot for your answer. But when I call the function as I mentioned before, at the first round the test if failed, since 100-5 is not in the list. But at the second round, when the function is called recursively, it should return a number since 100-75 exists in the list. Am i right or am I missing something?



    Originally Posted by b49P23TIvg
    Code:
    def shop(c, li):
        if ((c-li[0]) in li[1:]):
            return li[0]
        else:
            return shop(c, li[1:])
    Python functions, by default, return None. When the test failed (in other words the value is not in list) the else block evaluated, and then the function returned None.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0
    Hi again,

    I solved the problem by rewriting the function as:

    Code:
    def shop(c, li):
            return li[0] if (c-li[0] in li[1:]) else shop(c, li[1:])
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    Hmm. I see they added the ternary if expression to python2.

    Your new statement demonstrates, in my opinion, good style because there's one return statement.

    Do you still want an explanation of the failure in your first attempt?
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo