use strict; use warnings; my $input = input(); print $input->(0); while(){ my $output = $input->($_); last unless $output; print $output; } sub input { my @policy; my @damage = qw/ 0 33.21 57.36 72.45 138.87 253.59 298.88 /; my($text,@prompt,@result); while () { if(/TEXT1/../TEXT2/){$text .= $_ unless /[A-Z]+[12]/} if(/PROMPT1/../PROMPT2/){chomp; push @prompt, $_ unless /[A-Z]+[12]/} if(/RESULT1/../RESULT2/){chomp; push @result, $_ unless /[A-Z]+[12]/} } print $text; return sub { push @policy, shift; return $#policy < 6 ? $prompt[$#policy] : $#policy == 6 ? predict(\@damage,\@policy,\@result) : undef; }; } sub predict { my $slope = slope(shift, shift); my @r = @{$_[0]}; if ($slope < 0.625) { return "\n " . $r[0] . percent($slope); } elsif ($slope < 0.8) { return "\n " . $r[1] . percent($slope); } elsif ($slope < 1.25) { return "\n " . $r[2] . percent($slope); } elsif ($slope < 1.6) { return "\n " . $r[3] . percent($slope); } else { return "\n " . $r[4] . percent($slope); } } sub percent { if ($_[0] < 0.8) { return "\n You're paying " . int(100*(1-$_[0])) . "% less than you should.\n"; } elsif ($_[0] > 1.25) { return "\n You're paying " . int(100*($_[0]-1)) . "% more than you should.\n"; } else { return "\n You're paying " . int(100*$_[0]) . "% of the \"right\" price.\n"; } } sub sum {my $sum = 0; $sum += $_ for @_; $sum} sub mean {sum(@_)/scalar(@_)} sub sumn { my($n, $mean) = (shift, mean(@_)); sum(map{($_ - $mean)**$n} @_); } sub sumxy { my @x = @{$_[0]}; my @y = @{$_[1]}; return unless $#x == $#y; my ($xmean, $ymean) = (mean(@x), mean(@y)); my $sumxy = 0; $sumxy += ($x[$_] - $xmean)*($y[$_] - $ymean) for 0..$#x; return $sumxy; } sub slope { my($x, $y) = @_; sumxy($x, $y) / sumn(2, @{$x}); } __DATA__ TEXT1 By Federal Law, as a sysadmin, you're required to purchase Bug Insurance. The insurance policy will pay the plaintiff the damage incurred by each bug your system causes. Let $54.99 be the max possible damage claimable per bug, 5.49% the chances for each bug to independently occur. Using your "instinct" only, how much are you willing to pay for a policy that protect you against... TEXT2 PROMPT1 ... 11 bugs: ... 19 bugs: ... 24 bugs: ... 46 bugs: ... 84 bugs: ... 99 bugs: Press any key to quit. PROMPT2 RESULT1 You're a Gambler--code w/o your monitor on. You're a Risk-taker--code & pray, & never debug. You're a Stone-Cold Human Calculator--code in assembly. You're a Risk-Averser--even debug a plain text. You're a Wuss--even afraid of electrocution by your mouse. RESULT2