Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: John Guttag's book - 2nd exercise. My attempt in Perl.

by kcott (Archbishop)
on May 21, 2017 at 06:29 UTC ( [id://1190783]=note: print w/replies, xml ) Need Help??


in reply to John Guttag's book - 2nd exercise. My attempt in Perl.

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1190783]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-19 17:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found