Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Friday March 10, 2006
12:14 PM

Deal or No Deal

[ #28953 ]
Here's a small CGI program to calculate the expected value of the remaining cases on the TV show Deal or No Deal (Oh, and the all important screen shot).

#!/usr/bin/perl
use CGI;

$q = new CGI;
print $q->header;
print $q->start_html(-title=>'Deal or No Deal',-BGCOLOR=>'white'),
      $q->h1('--Deal or No Deal--'),
      $q->startform();

@left =   qw/ 0.01       1       5      10      20     50
                75     100     200     300     400     500     750  /;

@right=   qw/1,000   5,000  10,000  25,000  50,000  75,000
           100,000 200,000 300,000 400,000 500,000 750,000 1,000,000/;

my $i = 0;
print "<table>\n";
for (@left)
{
    print $q->Tr($q->th([
                         $q->checkbox(-name=>"cl$i",
                                        -checked=>'checked',
                                      -value=>'YES',
                                      -label=>'').
                        $q->textfield(-name=>"l$i",
                                      -default=>$left[$i],
                                      -size=>9,
                                      -override=>0),

                        $q->textfield(-name=>"r$i",
                                      -default=>$right[$i],
                                      -size=>9,
                                      -override=>0).
                         $q->checkbox(-name=>"cr$i",
                                        -checked=>'checked',
                                      -value=>'YES',
                                      -label=>'')
                                      ])), "\n";
    $i++;
}
print "</table>";
print $q->submit(-name=>'Calculate'),
      $q->endform()
      ;

if (@all = $q->param())
{
    my $acc = 0;
    my $count = 0;
    for (@all)
    {
        if(/c((?:l|r)\d+)/)
        {
            my $val = $q->param($1);
            $val =~ s/[^.0-9]//g;
            $count++;
            $sum += $val;
        }
    }
    print "<p>Expected Value: ".'$';
    printf "%.2f<hr>", $sum/$count;
}
print $q->end_html;

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.