Re: [Tutor] learning recursion

2014-02-10 Thread Russel Winder
On Sun, 2014-02-09 at 13:56 -0500, Dave Angel wrote: […] > Not as bad as attributing that code to me. Apologies for that implication, bad editing on my part, I should have retained the link to the author. -- Russel. = Dr

Re: [Tutor] learning recursion

2014-02-09 Thread Dave Angel
Russel Winder Wrote in message: > On Thu, 2014-02-06 at 18:06 -0500, Dave Angel wrote: > […] >> >> Code: >> def fib2(n): >> if n==1: >> return 1 >> >> elif n==2: >> return 1 >> else: >> return fib2(n-2) +fib2(n-1) > […] > > I suggest it

Re: [Tutor] learning recursion

2014-02-09 Thread Russel Winder
On Thu, 2014-02-06 at 18:06 -0500, Dave Angel wrote: […] > > Code: > def fib2(n): > if n==1: > return 1 > > elif n==2: > return 1 > else: > return fib2(n-2) +fib2(n-1) […] I suggest it also be pointed out that this form is algorithmical

Re: [Tutor] learning recursion

2014-02-08 Thread Mark Lawrence
On 08/02/2014 10:06, rakesh sharma wrote: Please do not top post on this list. I got my definition wrong the code should be more like this def fib(n): if n==0: return What does the above line return? As others have already said what happens if n is negative? elif n==1: return 1 elif n=

Re: [Tutor] learning recursion

2014-02-08 Thread rakesh sharma
ubject: Re: [Tutor] learning recursion > From: denis.heidtm...@gmail.com > To: rakeshsharm...@hotmail.com > CC: da...@davea.name; tutor@python.org > > On Fri, Feb 7, 2014 at 9:05 PM, rakesh sharma > wrote: > > Hi > > > > Shouldn't your code be like thi

Re: [Tutor] learning recursion

2014-02-08 Thread rakesh sharma
Hi I guess I need to revisit my maths lessons.Thank you for correcting me. thanks,rakesh > From: d...@hashcollision.org > Date: Sat, 8 Feb 2014 00:20:19 -0800 > Subject: Re: [Tutor] learning recursion > To: rakeshsharm...@hotmail.com > CC: da...@davea.name; tutor@python.org >

Re: [Tutor] learning recursion

2014-02-08 Thread Danny Yoo
On Fri, Feb 7, 2014 at 9:05 PM, rakesh sharma wrote: > Hi > > Shouldn't your code be like this > > def fib(n): > if n==0: > return 0 > else: > return n + fib(n-1) Hi Rakesh, Unfortunately, no, because this computes a slightly different function: the triangular numbers! :P That is, your funct

Re: [Tutor] learning recursion

2014-02-07 Thread Denis Heidtmann
On Fri, Feb 7, 2014 at 9:05 PM, rakesh sharma wrote: > Hi > > Shouldn't your code be like this > > def fib(n): > if n==0: > return 0 > else: > return n + fib(n-1) > > this works > for i in range(4): > print fib(i) > > 0 > 1 > 3 > 6 interesting, but the Fibonacci sequence is 1,1,2,3,5,8,

Re: [Tutor] learning recursion

2014-02-07 Thread rakesh sharma
014 18:06:41 -0500 > Subject: Re: [Tutor] learning recursion > > Denis Heidtmann Wrote in message: > > > > > Please post in text, not html. Your posting program loses the > indentation in the text view, which is what most people > see. > > Code: > def

Re: [Tutor] learning recursion

2014-02-06 Thread Dave Angel
Denis Heidtmann Wrote in message: > > Please post in text, not html. Your posting program loses the indentation in the text view, which is what most people see. Code: def fib2(n): if n==1: return 1 elif n==2: return 1 else:

Re: [Tutor] learning recursion

2014-02-06 Thread Devin Jeanpierre
On Thu, Feb 6, 2014 at 11:28 AM, Denis Heidtmann wrote: for i in range(4): > ... print fib2(i) > ... > > The above results in an error: Because fib2(0) recurses infinitely, and i's first value is 0. -- Devin ___ Tutor maillist - Tutor@python

Re: [Tutor] learning recursion

2014-02-06 Thread Danny Yoo
Ah. Consider what range(4) looks like. It's similar to the sequence: [0, 1, 2, 3] What happens when you do fib2(0)? :P Here's your program (modified with a correction) on repl.it, for convenience: http://repl.it/O30 ___ Tutor maillist -

Re: [Tutor] learning recursion

2014-02-06 Thread Neil Cerutti
On 2014-02-06, Denis Heidtmann wrote: > Running python 2.7 on Ubuntu 12.04 > > Code: > def fib2(n): > if n==1: > return 1 > elif n==2: > return 1 > else: > return fib2(n-2) +fib2(n-1) Something ate your leading spaces. Keep in mind that that makes most Python code unrecoverably corrupt. When shar

Re: [Tutor] learning recursion

2014-02-06 Thread Peter Otten
Denis Heidtmann wrote: > Running python 2.7 on Ubuntu 12.04 > > Code: > def fib2(n): > if n==1: > return 1 > elif n==2: > return 1 > else: > return fib2(n-2) +fib2(n-1) > > The above works: > fib2(7) > 13 fib2(4) > 3 > for i in range(4): > ... print fib2(i) > ... > > The ab

[Tutor] learning recursion

2014-02-06 Thread Denis Heidtmann
Running python 2.7 on Ubuntu 12.04 Code: def fib2(n): if n==1: return 1 elif n==2: return 1 else: return fib2(n-2) +fib2(n-1) The above works: >>> fib2(7) 13 >>> fib2(4) 3 >>> for i in range(4): ... print fib2(i) ... The above results in an error: Traceback (most recent call last): File