Recursive function

Discussion in 'Python' started by Ana Dionísio, Mar 5, 2013.

  1. Hello!

    I have to make a script that calculates temperature, but one of the
    parameters is the temperature in the iteration before, for example:
    temp = (temp_-1)+1

    it = 0
    temp = 3

    it = 1
    temp = 3+1

    it = 2
    temp = 4+1

    How can I do this in a simple way?

    Thanks a lot!
    Ana Dionísio, Mar 5, 2013
    #1
    1. Advertising

  2. Ana Dionísio

    Dave Angel Guest

    On 03/05/2013 10:32 AM, Ana Dionísio wrote:
    > Hello!
    >
    > I have to make a script that calculates temperature, but one of the
    > parameters is the temperature in the iteration before, for example:
    > temp = (temp_-1)+1
    >
    > it = 0
    > temp = 3
    >
    > it = 1
    > temp = 3+1
    >
    > it = 2
    > temp = 4+1
    >
    > How can I do this in a simple way?
    >
    > Thanks a lot!
    >


    Recursion only works when you have a termination condition. Otherwise,
    it just loops endlessly (or more specifically until the stack runs out
    of room). Still, you don't have a problem that even implies recursion,
    just iteration.

    For your simple case, there's a simple formula:

    def find_temp(it):
    return it+3

    Methinks you have simplified the problem too far to make any sense.

    import itertools
    temp = 3
    for it in itertools.count():
    temp += 1
    print it, temp

    Warning: that loop will never terminate.

    Is this what you were looking for? It uses recursion, and I used <=0
    for the termination condition.

    def find_temp2(it):
    if it <=0: return 3
    return find_temp(it-1)


    --
    DaveA
    Dave Angel, Mar 5, 2013
    #2
    1. Advertising

  3. 2013/3/5 Ana Dionísio <>:
    > Hello!
    >
    > I have to make a script that calculates temperature, but one of the
    > parameters is the temperature in the iteration before, for example:
    > temp = (temp_-1)+1
    >
    > it = 0
    > temp = 3
    >
    > it = 1
    > temp = 3+1
    >
    > it = 2
    > temp = 4+1
    >
    > How can I do this in a simple way?
    >
    > Thanks a lot!
    > --
    > http://mail.python.org/mailman/listinfo/python-list


    Hi,
    it is not quite clear from the examples, what should be achieved (I
    guess, the actual computation is probably mor complex).
    I'd probably approach an iterative computation iteratively, rather
    than recursively;
    e.g. simply:

    def compute_iteratively(starting_value, number_of_iterations):
    tmp = starting_value
    for i in range(number_of_iterations):
    tmp = tmp + 1
    return tmp

    print(compute_iteratively(starting_value=7, number_of_iterations=3))

    hth,
    vbr
    Vlastimil Brom, Mar 5, 2013
    #3
  4. Yes, I simplified it a lot.

    I need to run it 24 times. What I don't really understand is how to put the final temperature (result) in it = 0 in temp_-1 in it =1
    Ana Dionísio, Mar 5, 2013
    #4
  5. Yes, I simplified it a lot.

    I need to run it 24 times. What I don't really understand is how to put the final temperature (result) in it = 0 in temp_-1 in it =1
    Ana Dionísio, Mar 5, 2013
    #5
  6. Ana Dionísio

    Neil Cerutti Guest

    On 2013-03-05, Ana Dion?sio <> wrote:
    > Yes, I simplified it a lot.
    >
    > I need to run it 24 times. What I don't really understand is
    > how to put the final temperature (result) in it = 0 in temp_-1
    > in it =1


    One way to compose a function that recurses by calling itself is
    to first write the simplest version of the function you can think
    of, the version that solves the most trivial form for the
    problem.

    For example, lets say I want to write a recursive function to
    reverse a string (you would never do this except as an exercise),
    I'd first write a version that can successfully reverse an empty
    string.

    def reverse0(s):
    if len(s) == 0:
    return s

    The next most complex problem is a string with one character.
    I'll write that next. They key is to write it in terms of the
    function I already wrote, reverse0.

    def reverse1(s):
    if len(s) == 1:
    return s + reverse0('')

    At this point it becomes clear that reverse0 is not needed. I'll
    just use a new version of reverse1 instead:

    def reverse1(s):
    if len(s) <= 1:
    return s

    The next vesion will be able to handle two characters. I'll write
    it to use my previously composed reverse1 function.

    def reverse2(s):
    if len(s) == 2:
    return s[-1] + reverse1(s[:-1])

    And so on:

    def reverse3(s):
    if len(s) == 3:
    return s[-1] + reverse2(s[:-1])

    def reverse4(s):
    if len(s) == 4:
    return s[-1] + reverse3(s[:-1])

    By this time a pattern has emerged; every version of reverse
    after reverse1 is exactly the same. I can now write my recursive
    reverse function.

    def reverse_any(s):
    if len(s) <= 1:
    return s
    else:
    return s[-1] + reverse_any(s[:-1])

    Try this exercise with your conversion problem and see if you can
    make progress.

    --
    Neil Cerutti
    Neil Cerutti, Mar 5, 2013
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tyron

    Recursive function

    Tyron, Apr 16, 2004, in forum: VHDL
    Replies:
    1
    Views:
    618
    valentin tihomirov
    Apr 16, 2004
  2. n00m
    Replies:
    12
    Views:
    1,102
  3. vamsi
    Replies:
    21
    Views:
    2,050
    Keith Thompson
    Mar 9, 2009
  4. Mark Piffer
    Replies:
    9
    Views:
    898
    luserXtrog
    May 15, 2009
  5. Alok
    Replies:
    3
    Views:
    239
Loading...

Share This Page