in reply to John Guttag's book - 2nd exercise. My attempt in Perl.
Does it? I'm getting
4 is the biggest odd number
for the very sample you gave. Maybe the second $z on line 26 should be $y ?
Update: Here's my solution. It first sorts the numbers, then finds the first odd one starting from the greatest one.
sub cmp3 { my ($x, $y, $z) = @_; ($y, $x) = ($x, $y) if $y < $x; ($z, $y) = ($y, $z) if $z < $y; ($y, $x) = ($x, $y) if $y < $x; if (1 == $z % 2) { return "$z is the biggest odd number\n" } elsif (1 == $y % 2) { return "$y is the biggest odd number\n" } elsif (1 == $x % 2) { return "$x is the biggest odd number\n" } else { return "All are even numbers\n" } }
Tested against yours with
for (1 .. 10000) { my ($x, $y, $z) = map int rand 20, 1 .. 3; my $orig = orig($x, $y, $z); my $cmp3 = cmp3($x, $y, $z); say "$x $y $z ($orig) [$cmp3]" if $orig ne $cmp3; }
after changing all your prints to returns and all "greatest" to "biggest".
($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
|
|---|