Re: [Tutor] A recursion question

2011-11-19 Thread Kĩnũthia Mũchane
On 11/19/2011 04:59 PM, Dave Angel wrote: On 11/19/2011 01:36 AM, Kĩnũthia Mũchane wrote: On 11/19/2011 06:03 AM, Asokan Pichai wrote: Another way to do that is to avoid any intermediate variables altogether That may be easier to understand YMMV def counter(mylist, val): if len(mylist ==

Re: [Tutor] A recursion question

2011-11-19 Thread Dave Angel
On 11/19/2011 01:36 AM, Kĩnũthia Mũchane wrote: On 11/19/2011 06:03 AM, Asokan Pichai wrote: Another way to do that is to avoid any intermediate variables altogether That may be easier to understand YMMV def counter(mylist, val): if len(mylist == 0): return 0 if mylist[0]

Re: [Tutor] A recursion question

2011-11-18 Thread Kĩnũthia Mũchane
On 11/19/2011 06:03 AM, Asokan Pichai wrote: Another way to do that is to avoid any intermediate variables altogether That may be easier to understand YMMV def counter(mylist, val): if len(mylist == 0): return 0 if mylist[0] == val: return 1 + counter(mylist[1:]

Re: [Tutor] A recursion question

2011-11-18 Thread Asokan Pichai
Another way to do that is to avoid any intermediate variables altogether That may be easier to understand YMMV def counter(mylist, val): if len(mylist == 0): return 0 if mylist[0] == val: return 1 + counter(mylist[1:], val) else: return counter(myl

Re: [Tutor] A recursion question

2011-11-18 Thread Kĩnũthia Mũchane
On 11/18/2011 06:04 PM, Dave Angel wrote: On 11/18/2011 08:16 AM, Kĩnũthia Mũchane wrote: Well, it doesn't count the number of occurrences correctly if the list is empty. It should get zero, and it gets -1. But for any non-empty list, nothing ever looks at the -1 value, so it doesn't matte

Re: [Tutor] A recursion question

2011-11-18 Thread Dave Angel
On 11/18/2011 08:16 AM, Kĩnũthia Mũchane wrote: Hi, I am trying to do something which is really stupid :-) I would like to count the number of occurrences of a certain character in a list. This is more of an exercise in recursion rather than the underlying problem. I could have used a *for* l

Re: [Tutor] A recursion question

2011-11-18 Thread Christian Witts
On 2011/11/18 03:16 PM, Kĩnũthia Mũchane wrote: Hi, I am trying to do something which is really stupid :-) I would like to count the number of occurrences of a certain character in a list. This is more of an exercise in recursion rather than the underlying problem. I could have used a *for* l

[Tutor] A recursion question

2011-11-18 Thread Kĩnũthia Mũchane
Hi, I am trying to do something which is really stupid :-) I would like to count the number of occurrences of a certain character in a list. This is more of an exercise in recursion rather than the underlying problem. I could have used a *for* loop or simply the list *count* method. Here is the