1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Recursion practise question help (Python)

  1. Oct 11, 2011 #1
    1. The problem statement, all variables and given/known data

    Code (Text):
    def rev(n):
        '''Return the result of reversing the digits in integer n. For example,
        rev(512) should return 215.'''
    2. Relevant equations

    3. The attempt at a solution

    Code (Text):
    def rev(n):
        '''Return the result of reversing the digits in integer n. For example,
        rev(512) should return 215.'''
        n = str(n)[-1] + str(n)[:-1]

    I'm not sure how to check that it is finished and stop?
  2. jcsd
  3. Oct 11, 2011 #2
    Hi there, checking the solution shouldn't be a problem. You would just have to print the number (n) after and see if it worked right?

    I'm also not sure your code will work for integers with more than 3 digits. I'm a little rusty in python but it seems like if n = 1234 that your code might output 4123.


    Try this?

    Code (Text):

    def rev(n):
         reversed = str(n)[-1] + str(n)[:-1]
         return reversed

    n = 5112
    print rev(n)
    So I haven't changed your code at all, I've just added an extra digit onto the number.
    inside the function, the return reversed will return the string hopefully switched the way that you want it. So basically when you call the function like I did when I said "print rev(n)", the rev(n) will be the returned value.
  4. Oct 11, 2011 #3
    Yes I haven't finished the function because I don't know how to recurse it.
    The part I posted only swaps the last digit to the front.
    I didn't put in the recursive step yet because I don't know how to do it.
  5. Oct 11, 2011 #4
    ahh i see what you're trying to do. You want to keep swapping the last digit to the front until the number is reversed. Not a bad strategy. There are a handful of ways to do it, but I think in any case you will need the "len" function. len(n) will return the length of the integer. It should be helpful in your recursion.
  6. Oct 11, 2011 #5
    So I need to cut the number to smaller pieces each time?
    How can I swap the digits keep I keep cutting it?
    This is confusing because I'm only allowed to use one variable n.
  7. Oct 11, 2011 #6
    Can you give me all of the bounds of your assignment? There are most likely python objects that can do this problem and save you from having to use recursion yourself, but I assume you're not allowed to do that.
  8. Oct 11, 2011 #7
    I don't know how to do it if n is the only variable I can use. I would need to either use higher level objects from a library, or use another variable as a counter in my recursion. I have no way to tell the recursion to stop unless I can define another variable that I can control.
  9. Oct 11, 2011 #8
    The only instruction is

    '''Write the following methods recursively.'''
  10. Oct 11, 2011 #9
    so you aren't restricted to only using 1 variable then. It should be fairly easy. Explain to me your logic, the steps that you plan to go through in your code, and I can try to help you put that logic into code form.
  11. Oct 11, 2011 #10


    Staff: Mentor

    I'm not very knowledgeable about python, but I think this would work.

    I'm assuming that the parameter n is an int.
    1) Calculate digit = n % 10. This gives you the current digit in the 1's place.
    2) Print digit.
    3) Calculate a new value for n: n = n/10. Note that this is integer division, so for example 12/10 == 1 and 8/10 == 0.
    4) If n > 0, call rev(n). If n == 0, end the recursion.
  12. Oct 11, 2011 #11
    Thats a cool way to do it. This way you actually don't have to use any other variable other than n. I didn't even think about that. However, I don't think this is conceptually the easiest code to write. It's based more on nuances of programming rather than pure logic.
  13. Oct 11, 2011 #12


    Staff: Mentor

    Actually, the logic is very pure.

    At each step, you take off the last digit, print it, and then work on the remaining number, which has one less digit. This approach comes natural to C and assembly coders.
  14. Oct 11, 2011 #13
    So I can only print the last digit each time it recurses?
    How can I return the result as one whole string?
    Last edited: Oct 11, 2011
  15. Oct 11, 2011 #14
    You could simply create another variable and add the digit on each time.

    You could also create an indexed list and concatenate it.

    So instead of the print digit, it would be digit = digit + newdigit.
    the only limitation there is that it would have to be a string.
  16. Oct 11, 2011 #15
    I understand how to do it with more variables.
    I'm asking for the case of only using n as I think that was what Mark was trying to do.
  17. Oct 11, 2011 #16
    technically he uses a "Calculate Digit" variable. You could probably take that out, but there is no way for you to save a set of numbers without a new variable. The only option you have for keeping a single variable is to find a recursive process that retains all of the information that it started with. For example, the first idea that you had where you keep moving the last digit to the first digit would allow you to just print n at the end. The only problem you have there is to figure out how to break the loop without an index.
  18. Oct 11, 2011 #17


    Staff: Mentor

    Where does it say you are allowed to use only one variable? I don't see that restriction in what you have for the problem statement. Your function should have only a single parameter, n, but that doesn't mean you can't have other local variables.
  19. Oct 11, 2011 #18
    So I've clarified this and n is supposed to be the only parameter.
    Here is the solution:

    Code (Text):
     def rev(n):
        '''Return the result of reversing the digits in integer n. For example,
        rev(512) should return 215.'''
        n = str(n)
        if not n:
           return (n)
           return rev(n[1:]) + n[0]
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Discussions: Recursion practise question help (Python)
  1. Python question (Replies: 2)

  2. Practise exam question (Replies: 2)

  3. Python question (Replies: 1)