Perl: the Markov chain saw PerlMonks

### YAGG (Yet Another Golf Game)

by Necos (Friar)
 on Feb 12, 2002 at 04:35 UTC Need Help??

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;

for (1..200) {
push(@results,\$flip[rand(2)]);
}

foreach (@results)
{
if (\$_ =~/H/)
{
print \$_;
}

else
{
print " ";
}
}

print "\n\n";
{
}
print "The tally of heads (in a row) looks like:\n";
print "\n";
print "The number of heads total is:\n";
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
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

Thanks for the practice Necos but there are too many requirements here for a Golf game. ;-)

Anyone thinking of posting a Golf game should have a look at chipmunk's Tips on Writing a Golf Challenge.

Anyway, 90 chars:

```    #!/usr/bin/perl

\$s.=rand()<.5?H:\$"for+a..gr;
\$_=\$s;
\$c=y/H/H/;
s/(H+| +)/"|".length\$1/ge;
print"\$s\n\$_|\n\$c\n"

--
John.

Nice round. I did notice an error however. The output for the H's in a row doesn't match the H count. I changed the regex and all is fine now. I also removed the y/H/H/;
```#!/usr/bin/perl

\$s.=rand()<.5?H:\$"for+a..gr;
\$_=\$s;
s/(H+) +/"|".length\$1/ge;
print"\$s\n\$_|\n\$c\n"

You're right. :-) I thought that the runs of tails had to be included as well.

This is closer to what is required, but fudges the pipes a little, 88 chars:

```    \$s.=rand()<.5?H:\$"for+a..gr;
\$_=\$s;
\$c=200-tr/ /|/s;
s/(H+)/length\$1/ge;
print"\$s\n\$_\n\$c\n"

--
John.

This introduces a different error: if the last coin flip is a head, it doesn't get substituted, because it's not followed by a space. So, change the + to a *:
```#!/usr/bin/perl

\$s.=rand()<.5?H:\$"for+a..gr;
\$_=\$s;
s/(H+) */"|".length\$1/ge;
print"\$s\n\$_|\n\$c\n"
Re: YAGG (Yet Another Golf Game)
by trs80 (Priest) on Feb 12, 2002 at 05:32 UTC
```for(1..200){\$c=['H','T']->[rand(2)];\$r{\$t}++if\$c ne 'T';
\$t++if\$c eq 'T';}for(keys%r){push@g,\$r{\$_}}print join('|',@g),"\n";
Here is some late night code, should be able to trim it down tomorrow. I may have missed the requirements as well. Thanks for the post.
Re: YAGG (Yet Another Golf Game)
by chipmunk (Parson) on Feb 13, 2002 at 06:33 UTC
This solution comes in at 89 characters:
```@h=0;print map(rand>.5?++\$H&&++\$h[-1]&&H:++\$#h&&\$",a..gr),\$/,
join('|',grep\$_,@h),\$/,\$H,\$/
Could you clarify the exact output format that the code should produce?
Just a the number of heads that occur in a row, follwed by the number of heads total. Look at the sample output in the root node for a better example. The only real requirement in the output is that the flip data, tally, and total need to be separated by newlines.

Theodore Charles III
Los Angeles Senior High
4650 W. Olympic Blvd.
Los Angeles, CA 90019
323-937-3210 ext. 224
email->secon_kun@hotmail.com

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://144781]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (1)
As of 2021-12-09 01:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
R or B?

Results (36 votes). Check out past polls.

Notices?