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:
-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;
Here is some sample output from a coworker's WinXP box (using ActivePerl 522):
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
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.
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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.