Greg Buchholz's Journal
http://use.perl.org/~Greg+Buchholz/journal/
Greg Buchholz's use Perl Journal
en-us
use Perl; is Copyright 1998-2006, Chris Nandor. Stories, comments, journals, and other submissions posted on use Perl; are Copyright their respective owners.
2012-01-25T02:42:37+00:00
pudge
pudge@perl.org
Technology
hourly
1
1970-01-01T00:00+00:00
Greg Buchholz's Journal
http://use.perl.org/images/topics/useperl.gif
http://use.perl.org/~Greg+Buchholz/journal/
-
Deal or No Deal
http://use.perl.org/~Greg+Buchholz/journal/28953?from=rss
Here's a small CGI program to calculate the expected value of the remaining cases on the TV show <a href="http://www.nbc.com/Deal_or_No_Deal/">Deal or No Deal</a> (Oh, and the all important <a href="http://sleepingsquirrel.org/perl/deal.png">screen shot</a>).<blockquote><div><p> <tt>#!/usr/bin/perl<br>use CGI;<br> <br>$q = new CGI;<br>print $q->header;<br>print $q->start_html(-title=>'Deal or No Deal',-BGCOLOR=>'white'),<br> $q->h1('--Deal or No Deal--'),<br> $q->startform();<br> <br>@left = qw/ 0.01 1 5 10 20 50<br> 75 100 200 300 400 500 750 <nobr> <wbr></nobr>/;<br> <br>@right= qw/1,000 5,000 10,000 25,000 50,000 75,000<br> 100,000 200,000 300,000 400,000 500,000 750,000 1,000,000/;<br> <br>my $i = 0;<br>print "<table>\n";<br>for (@left)<br>{<br> print $q->Tr($q->th([<br> $q->checkbox(-name=>"cl$i",<br> -checked=>'checked',<br> -value=>'YES',<br> -label=>'').<br> $q->textfield(-name=>"l$i",<br> -default=>$left[$i],<br> -size=>9,<br> -override=>0),<br> <br> $q->textfield(-name=>"r$i",<br> -default=>$right[$i],<br> -size=>9,<br> -override=>0).<br> $q->checkbox(-name=>"cr$i",<br> -checked=>'checked',<br> -value=>'YES',<br> -label=>'')<br> ])), "\n";<br> $i++;<br>}<br>print "</table>";<br>print $q->submit(-name=>'Calculate'),<br> $q->endform()<br> <nobr> <wbr></nobr>;<br> <br>if (@all = $q->param())<br>{<br> my $acc = 0;<br> my $count = 0;<br> for (@all)<br> {<br> if(/c((?:l|r)\d+)/)<br> {<br> my $val = $q->param($1);<br> $val =~ s/[^.0-9]//g;<br> $count++;<br> $sum += $val;<br> }<br> }<br> print "<p>Expected Value: ".'$';<br> printf "%.2f<hr>", $sum/$count;<br>}<br>print $q->end_html;</tt></p></div> </blockquote>
Greg Buchholz
2006-03-10T17:14:08+00:00
journal
-
unfold
http://use.perl.org/~Greg+Buchholz/journal/26747?from=rss
I came across <a href="http://perlmonks.org/?node_id=180961">Limbic~Region</a>'s <a href="http://www.perl.com/pub/a/2005/06/16/iterators.html">article</a> on iterators and thought I write up some quick perl examples using the more generic <a href="http://www.google.com/search?q=haskell+unfold">unfold</a> (code reuse in action). Fold is the list deconstructor, while unfold is its dual, the list generator.<blockquote><div><p> <tt>#!/usr/bin/perl -w<br> <br>use strict;<br> <br>#Demonstrations of iterators using 'unfold'<br> <br>#the natural numbers 1,2,3...<br>my $nats = unfold(\&false, #loop forever<br> \&id, #identity function<br> sub{$_[0]+1}, #increment value<br> 1 ); #initial seed<br> <br># 0,2,4,6,8<br>my $evens_5 = unfold( sub{$_[0]>8}, \&id, sub{$_[0]+2}, 0);<br> <br>#Factorials<br>my $facs = unfold(\&false,<br> \&fst,<br> sub{[fst($_[0])*snd($_[0]),snd($_[0])+1]},<br> [1,1]);<br> <br>#Fibonacci's famous sequence<br>my $fibs = unfold(\&false, \&fst, sub{[snd($_[0]),fst($_[0])+snd($_[0])]},[0,1]);<br> <br>#Enumeration of DNA sequences from...<br># http://www.perl.com/pub/a/2005/06/16/iterators.html<br>#<br>my $dna = unfold(sub{toDNA($_[0]) eq "CTTTT"},<br> sub{my $x = toDNA($_[0]); $x=~s/^.(.*)$/$1/; $x},<br> sub{$_[0]+1},<br> fromDNA("CAAAA"));<br> <br>#print out some samples...<br>my (@n, @e, @a, @f);<br>push @n, $nats->() for (1..10);<br>push @e, $evens_5->() for (1..5);<br>push @a, $facs->() for (1..10);<br>push @f, $fibs->() for (1..10);<br>print "@n\n@e\n@a\n@f\n";<br> <br>print $dna->()<nobr> <wbr></nobr>."\n" for (1..10);<br> <br># --Haskell version of unfold...<br>#<br># unfold p f g x =<br># if p x<br># then []<br># else f x : unfold p f g (g x)<br> <br>sub unfold<br>{ # function $p is a predicate to indicate when to stop iterator<br> # function $f takes the seed ($x) and formats it before returning it<br> # function $g massages $x for the next iteration<br> # $x is the initial value<br> <br> my ($p, $f, $g, $x) = @_;<br> sub{ ($p->($x)) ? undef : do{my $val = $f->($x); $x=$g->($x); $val}}<br>}<br> <br>#Helper functions<br>sub fst { $_[0]->[0] }<br>sub snd { $_[0]->[1] }<br>sub false { 0 }<br>sub id { $_[0] }<br> <br># These subs convert back and forth between strings of "ACGT" and<br># integers, using base-4 arithmetic. Something with pack/unpack<br># might be cleaner.<br> <br>sub fromDNA<br>{<br> my $s = shift;<br> $s =~ tr/ACGT/0-3/;<br> <br> my $n=0;<br> $n = $n*4+$_ for (split(//, $s));<br> <br> return $n;<br>}<br> <br>sub toDNA<br>{<br> my $n = shift;<br> my $acc = "";<br> <br> while($n)<br> {<br> my $d = $n % 4;<br> $n = int($n/4);<br> $acc = "$d" . $acc;<br> }<br> <br> ($acc="$acc") =~ tr/0-3/ACGT/;<br> <br> return $acc;<br>}</tt></p></div> </blockquote>
Greg Buchholz
2005-09-16T23:26:27+00:00
journal