python recursive function

Reedick, Andrew jr9445 at ATT.COM
Fri Jan 11 16:51:01 CET 2008



> -----Original Message-----
> From: python-list-bounces+jr9445=att.com at python.org [mailto:python-
> list-bounces+jr9445=att.com at python.org] On Behalf Of Tom_chicollegeboy
> Sent: Friday, January 11, 2008 3:30 AM
> To: python-list at python.org
> Subject: python recursive function
> 
> Now, you are to write a program that, if I give you n bears, returns
> true if it is at all possible for you to win the game. Your program
> must use recursion to check all possible ways in which you can apply
> the rules.
> 

>     if n==42:
>         return True

>     return False

You have to check for all possible paths.  Returning True/False is
futile since the recursive chains will be returning a mix of true and
false.  Use a global variable to indicate if a solution is found.  (Or
pass the flag in using a list, since lists are passed by reference (if n
== 42: found_it[0] = True; return.)

There's also another teaching exercise in here.  Do you follow the
literal directions ('check all possible ways') and generate all possible
paths?  Or do you 'interpret' that to mean try all possible paths until
you find a solution?  (i.e. do you short circuit the recursion once you
have a solution?)

One of the most difficult things about programming is conveying the
requirements from Human A to Human Programmer B.  This is especially
difficult since business people and techies speak different languages
and, more importantly, think differently (different assumptions,
different paradigms, different levels of hand-waving away of details,
etc..)  And don't get me started about the people in marketing...



*****

The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. GA623





More information about the Python-list mailing list