[Tutor] -Recursive Functions

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Sun Jun 1 03:02:03 2003


On Sat, 31 May 2003, evros loizides wrote:

> * Your program should accept a string using the raw_input function. It
> should define a RECURSIVE function stringToNum(aString) than accepts a
> string parameter and returns the sum of the digit characters that occur
> in the string.  Thus, e.g., if the input is 'he5llo7 9world', the return
> value would be 21

Hi Evros,

Ok, this is definitely a homework problem. Please understand that our
options for helping you are greatly restricted --- part of the value of
the problem is figuring out how to solve it by yourself.

But a small suggestion: have you tried to solve a simpler-but-related
problem?  A simpler problem will give you practice so that you have a
better feel for how these recursive functions work.

For example, can you write a recursive function that will work on an input
that consists of just digits?  This problem is similar enough to the
original that once you get this, you can probably modify it to do the real
problem.

Alan Gauld has a good section on Recursion on his web tutor, so it should
be useful to go through it at least once:

    http://www.freenetpages.co.uk/hp/alan.gauld/tutrecur.htm

Good luck to you.