G'day pritesh_ugrankar,

Here's one way to do it:

#!/usr/bin/env perl use strict; use warnings; use constant { INPUT => 0, EXPECT => 1, NO_ODDS => 'no odds found' }; use Test::More; my @tests = ( [[qw{-11 -13 4}], -11], [[qw{-12 -14 4}], NO_ODDS], [[qw{0 0 0}], NO_ODDS], [[qw{1 1 1}], 1], [[qw{1 0 1}], 1], [[qw{0 0 1}], 1], [[qw{2 3 2}], 3], [[qw{2 -3 2}], -3], [[qw{-2 -3 -4}], -3], [[qw{-1 -3 -5}], -1], [[qw{3 5 7}], 7], [[qw{7 5 3}], 7], [[qw{5 7 3}], 7], ); plan tests => scalar @tests; for my $test (@tests) { my ($x, $y, $z) = @{$test->[INPUT]}; my $u = ($x, $y)[$x % 2 ? ($y % 2 ? $x < $y : 0) : 1]; my $v = ($u, $z)[$u % 2 ? ($z % 2 ? $u < $z : 0) : 1]; ok((NO_ODDS, $v)[$v % 2] eq $test->[EXPECT]); }

See Test::More if you're not familiar with that module. It's very useful in situations like this where you want to test a solution against a variety of input data.

All those tests passed:

1..13 ok 1 ... 2 to 12 all 'ok' also ... ok 13

As you can hopefully see, it's very easy to add more test data by simply adding more elements to @tests.

— Ken


In reply to Re: John Guttag's book - 2nd exercise. My attempt in Perl. by kcott
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.