From 2a95c0c13dc52fe5230b464f3cc48a0b8d38d2bd Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Sun, 20 Oct 2019 11:52:39 +0200 Subject: Add tests for 2 --- challenge-030/lubos-kolouch/perl5/ch-2.pl | 38 +++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/challenge-030/lubos-kolouch/perl5/ch-2.pl b/challenge-030/lubos-kolouch/perl5/ch-2.pl index 45e3db63c7..30124e32b5 100644 --- a/challenge-030/lubos-kolouch/perl5/ch-2.pl +++ b/challenge-030/lubos-kolouch/perl5/ch-2.pl @@ -25,27 +25,41 @@ use warnings; use feature qw/say/; sub is_ok_triple { - my ($x,$y,$z) = @_; + my ( $x, $y, $z ) = @_; - return 0 unless $x+$y+$z == 12; - return 0 unless ($x % 2 ==0) or ($y % 2 == 0) or ($z % 2 == 0); + return 0 unless $x + $y + $z == 12; + return 0 unless ( $x % 2 == 0 ) or ( $y % 2 == 0 ) or ( $z % 2 == 0 ); return 1; } -my $max = 12; +sub main { + my $max = 12; + for my $x ( 1 .. 10 ) { # max 10+1+1 + for my $y ( 1 .. 10 ) { + last if ( $x + $y ) >= $max; -for my $x (1..10) { # max 10+1+1 - for my $y (1..10) { - last if ( $x + $y ) >= $max; + for my $z ( 1 .. 10 ) { + my $sum = $x + $y + $z; + last if $sum > $max; - for my $z (1..10) { - my $sum = $x + $y + $z; - last if $sum > $max; + say "$x $y $z" if is_ok_triple( $x, $y, $z ); - say "$x $y $z" if is_ok_triple($x,$y,$z); - + } } } } +main; + +# TESTS + +use Test::More; + +is(is_ok_triple(3,4,5),1,'Test 3 4 5'); +is(is_ok_triple(3,4,6),0,'Test 3 4 6'); + +# it seems there is no triple without even number so can't test +# but leave the code in in case the $max changes + +done_testing; -- cgit