Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #40562

Re: Recursive function

From Neil Cerutti <neilc@norwich.edu>
Newsgroups comp.lang.python
Subject Re: Recursive function
Date 2013-03-05 19:05 +0000
Organization Norwich University
Message-ID <apmtviFrhbcU1@mid.individual.net> (permalink)
References <9ff73ed4-24cd-4a61-bb54-a67dd4a96ed0@r9g2000vbh.googlegroups.com> <mailman.2891.1362499338.2939.python-list@python.org> <mailman.2893.1362500115.2939.python-list@python.org>

Show all headers | View raw


On 2013-03-05, Ana Dion?sio <anadionisio257@gmail.com> 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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Recursive function Ana Dionísio <anadionisio257@gmail.com> - 2013-03-05 07:32 -0800
  Re: Recursive function Dave Angel <davea@davea.name> - 2013-03-05 11:02 -0500
    Re: Recursive function Ana Dionísio <anadionisio257@gmail.com> - 2013-03-05 08:15 -0800
    Re: Recursive function Ana Dionísio <anadionisio257@gmail.com> - 2013-03-05 08:15 -0800
      Re: Recursive function Neil Cerutti <neilc@norwich.edu> - 2013-03-05 19:05 +0000
  Re: Recursive function Vlastimil Brom <vlastimil.brom@gmail.com> - 2013-03-05 17:06 +0100

csiph-web