Well, since my last golf game was so well received, I decided to add some more functionality to my little coin-flipping code, and count the number of times we flip heads in a row, as well as count the number of heads total.
Without further discussion, here is the code base that I came up with:
Let the games begin!
Theodore Charles III
Network Administrator
Los Angeles Senior High
4650 W. Olympic Blvd.
Los Angeles, CA 90019
323-937-3210 ext. 224
email->secon_kun@hotmail.com
Without further discussion, here is the code base that I came up with:
Here is some sample output from a coworker's WinXP box (using ActivePerl 522):-w; use strict; my @flip = ('H','T'); my @results; my @heads; for (1..200) { push(@results,$flip[rand(2)]); } my $head_count; foreach (@results) { if ($_ =~/H/) { print $_; $head_count++; } else { print " "; push(@heads,$head_count) if ($head_count != 0); $head_count= 0; } } $head_count = 0; print "\n\n"; foreach (@heads) { $head_count += $_; } print "The tally of heads (in a row) looks like:\n"; print join('|',@heads); print "\n"; print "The number of heads total is:\n"; print $head_count;
My code seems somewhat bloated, and I know it can be golfed some, but I'm not really a regex wiz yet, so this code is about as good as I'm going to get.HHHHH H HH H H H H H H H H HH H HH H H H + HHHH H H H HHHHHH HH HHHHH H HHH HHHHHH H HHH H HH HH + H H H HHHH HH HH H H HH H H HH HHH H HH H The tally of heads (in a row) looks like: 5|1|2|1|1|1|1|1|1|1|1|2|1|2|1|1|1|4|1|1|1|6|2|5|1|3|6|1|3|1|2|2|1|1|1| +4|2|2|1|1|2|1|1|2|3|1|2 The number of heads total is: 88
Let the games begin!
Theodore Charles III
Network Administrator
Los Angeles Senior High
4650 W. Olympic Blvd.
Los Angeles, CA 90019
323-937-3210 ext. 224
email->secon_kun@hotmail.com
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: YAGG (Yet Another Golf Game)
by jmcnamara (Monsignor) on Feb 12, 2002 at 09:58 UTC | |
by trs80 (Priest) on Feb 13, 2002 at 02:25 UTC | |
by jmcnamara (Monsignor) on Feb 13, 2002 at 09:20 UTC | |
by chipmunk (Parson) on Feb 13, 2002 at 06:27 UTC | |
Re: YAGG (Yet Another Golf Game)
by trs80 (Priest) on Feb 12, 2002 at 05:32 UTC | |
Re: YAGG (Yet Another Golf Game)
by chipmunk (Parson) on Feb 13, 2002 at 06:33 UTC | |
by Necos (Friar) on Feb 13, 2002 at 21:14 UTC |
Back to
Meditations