Re: [Tutor] Finding the streaks in heads/tails list

2008-10-03 Thread nathan virgil
You need to store the count before resetting it since you want

 to know the largest value of count over the list. Or at least keep a
 separate max variable that you update if count  max.



Fairly easy:

if Cur_Count  Max_Count:
 Max_Count = Cur_Count

See? Just two extra lines of code.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Finding the streaks in heads/tails list

2008-10-01 Thread Kent Johnson
On Wed, Oct 1, 2008 at 4:56 PM, Alec Henriksen [EMAIL PROTECTED] wrote:
 Hello,

 I thought it'd be cool to write a program for my logic/critical thinking
 class, and right now we're evaluating randomness - and the deception of
 it. A previous post inspired it - coin flipping.

 So, I've written a program that flips a coin 1000 times and records it
 all in a dictionary, like this:

 # 0 = heads, 1 = tails
 flips = [0,0,0,1,0,1,0,0,1,1,1,1,1,0,0,1,0,1,0,1,0,1]

 What I want to do, is find out the largest streak of digits. In the
 above example, the streak would be 5, because there are 5 tails flips in
 a row.

 I've thought about this, and it seems like regular expressions would be
 needed.

Regular expressions are for processing strings, not loops.

I would loop through the list with a for loop, keeping track of the
last value seen and the current count. If the current value is the
same as the last, increment the count; if it is different, reset the
count.

You don't actually have to put the flips into a list, you could count
the runs directly as you make the flips.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Finding the streaks in heads/tails list

2008-10-01 Thread Danny Yoo
 Regular expressions are for processing strings, not loops.

From a theoretical point of view, this isn't quite true: regular
expressions can deal with sequences of things.  It's true that most
regular expression libraries know how to deal only with characters,
but that's a matter of specializing the library for efficiency, and
not a general property of regexes.

But what regular expressions (i.e. finite-state automata) can't do
very well is count with memory, and the task you're asking for is
fundamentally an anti-regexp one.


 I would loop through the list with a for loop, keeping track of the
 last value seen and the current count. If the current value is the
 same as the last, increment the count; if it is different, reset the
 count.

Agreed.  This seems direct.

If we want to be cute, we can also use the itertools.groupby()
function to do the clumping of identical sequential values for us.
For example:

#
 for group in itertools.groupby('caaabcc'):
... print group[0], len(list(group[1]))
...
a 4
b 4
c 1
a 3
b 1
a 4
c 2
#

See the standard library documentation for more details on itertools.groupby():

http://www.python.org/doc/lib/itertools-functions.html
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Finding the streaks in heads/tails list

2008-10-01 Thread Kent Johnson
On Wed, Oct 1, 2008 at 6:00 PM, Danny Yoo [EMAIL PROTECTED] wrote:
 Regular expressions are for processing strings, not loops.

 From a theoretical point of view, this isn't quite true: regular
 expressions can deal with sequences of things.

Sheesh! OK, *Python* regular expressions are for processing strings :-)

 If we want to be cute, we can also use the itertools.groupby()
 function to do the clumping of identical sequential values for us.
 For example:

 #
 for group in itertools.groupby('caaabcc'):
 ... print group[0], len(list(group[1]))

Hmm, I smell a one-liner here:
max(len(list(group[1])) for group in itertools.groupby('caaabcc'))

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Finding the streaks in heads/tails list

2008-10-01 Thread Alan Gauld


Kent Johnson [EMAIL PROTECTED] wrote

What I want to do, is find out the largest streak of digits. In 
the
above example, the streak would be 5, because there are 5 tails 
flips in

a row.



I would loop through the list with a for loop, keeping track of the
last value seen and the current count. If the current value is the
same as the last, increment the count; if it is different, reset the
count.


You need to store the count before resetting it since you want
to know the largest value of count over the list. Or at least keep a
separate max variable that you update if count  max.

But as Kent also said the easiest way is probably to just track the
runs and their count as the data is generated rather than waiting
till the end and post-processing the results.

HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor