Re: python recursive function

2008-01-11 Thread Steven D'Aprano
On Fri, 11 Jan 2008 06:30:04 -0600, Nick Craig-Wood wrote: > HYRY <[EMAIL PROTECTED]> wrote: >> def bears (n): >> if n==42: >> return True ... >> return False > > Almost but you missed a case... Why are you people doing the OP's homework for him? And then DEBUGGING it as wel

RE: python recursive function

2008-01-11 Thread Reedick, Andrew
> -Original Message- > From: [EMAIL PROTECTED] [mailto:python- > [EMAIL PROTECTED] On Behalf Of Tom_chicollegeboy > Sent: Friday, January 11, 2008 3:30 AM > To: python-list@python.org > Subject: python recursive function > > Now, you are to write a program tha

Re: python recursive function

2008-01-11 Thread Fidtz
On 11 Jan, 08:30, Tom_chicollegeboy <[EMAIL PROTECTED]> wrote: > here is what I have to do: > > This question involves a game with teddy bears. The game starts when I > give you some bears. You then start giving me back some bears, but you > must follow these rules (where n is the number of bears t

Re: python recursive function

2008-01-11 Thread Nick Craig-Wood
HYRY <[EMAIL PROTECTED]> wrote: > def bears (n): > if n==42: > return True > if n%5==0: > if bears(n-42): > return True > if n%2==0: > if bears(n/2): > return True > if n%3==0 or n%4==0: > one = (n%10) > two

Re: python recursive function

2008-01-11 Thread Bruno Desthuilliers
Duncan Booth a écrit : > Bruno Desthuilliers <[EMAIL PROTECTED]> > wrote: > >> You want: >>return bears(n - 42) > > Actually, no he doesn't. He needs to explore all options when the first > attempt fails. Possibly - I didn't bother checking the algorithm correctness, just pointed

Re: python recursive function

2008-01-11 Thread HYRY
> def bears (n): > if n==42: > return True > if n%5==0: > bears(n-42) > if n%2==0: > bears(n/2) > if n%3==0 or n%4==0: > one = (n%10) > two = ((n%100)/10) > if one!=0 and two!=0: > bears(n-(one*two)) > return False > >

Re: python recursive function

2008-01-11 Thread cokofreedom
> Stylistically I prefer 'if not n % 5', looks neater. > As for your assignment, the hardest task will be creating an effective > method of ensuring you recurse through all possibilities. I was chatting to a friend about the 'if not n % 5' and while I am happy to use it saying that when 5 % 5 is F

Re: python recursive function

2008-01-11 Thread Chris
On Jan 11, 10:30 am, Tom_chicollegeboy <[EMAIL PROTECTED]> wrote: > here is what I have to do: > > This question involves a game with teddy bears. The game starts when I > give you some bears. You then start giving me back some bears, but you > must follow these rules (where n is the number of bear

Re: python recursive function

2008-01-11 Thread Duncan Booth
Bruno Desthuilliers <[EMAIL PROTECTED]> wrote: > You want: >return bears(n - 42) Actually, no he doesn't. He needs to explore all options when the first attempt fails. But I'm not going to write his homework for him. -- http://mail.python.org/mailman/listinfo/python-list

Re: python recursive function

2008-01-11 Thread cokofreedom
On Jan 11, 9:46 am, Gary Herron <[EMAIL PROTECTED]> wrote: > Tom_chicollegeboy wrote: > > here is what I have to do: > > > This question involves a game with teddy bears. The game starts when I > > give you some bears. You then start giving me back some bears, but you > > must follow these rules (w

Re: python recursive function

2008-01-11 Thread Bruno Desthuilliers
Gary Herron a écrit : > Tom_chicollegeboy wrote: >> here is what I have to do: >> >> This question involves a game with teddy bears. The game starts when I >> give you some bears. You then start giving me back some bears, but you >> must follow these rules (where n is the number of bears that you >

Re: python recursive function

2008-01-11 Thread Bruno Desthuilliers
Tom_chicollegeboy a écrit : (snip) > As you see my program must use recursion. It's conceptually easier to express using recursions - but every recursion-based algorithm can be rewritten to use iteration (and vice-versa). > I came up with this idea but I am not sure if its right or are there >

Re: python recursive function

2008-01-11 Thread Gary Herron
Tom_chicollegeboy wrote: > here is what I have to do: > > This question involves a game with teddy bears. The game starts when I > give you some bears. You then start giving me back some bears, but you > must follow these rules (where n is the number of bears that you > have): > This sounds very

python recursive function

2008-01-11 Thread Tom_chicollegeboy
here is what I have to do: This question involves a game with teddy bears. The game starts when I give you some bears. You then start giving me back some bears, but you must follow these rules (where n is the number of bears that you have): If n is even, then you may give back exactly n/2 bears.