> While this works fine for all the test cases I tried

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,

In reply to Re: John Guttag's book - 2nd exercise. My attempt in Perl. by choroba
in thread John Guttag's book - 2nd exercise. My attempt in Perl. by pritesh_ugrankar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.