aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2020-09-29 08:41:04 +0100
committerdrbaggy <js5@sanger.ac.uk>2020-09-29 08:41:04 +0100
commit3d582d8fd55cef900675a2aa5885d18ab7b8ed82 (patch)
tree32466643a4f30b9750a46ffd7d9e1f2d744daced
parent3204a3bed980f50cbfa1eece85d8612a442c7b88 (diff)
downloadperlweeklychallenge-club-3d582d8fd55cef900675a2aa5885d18ab7b8ed82.tar.gz
perlweeklychallenge-club-3d582d8fd55cef900675a2aa5885d18ab7b8ed82.tar.bz2
perlweeklychallenge-club-3d582d8fd55cef900675a2aa5885d18ab7b8ed82.zip
re-written with test-more tests and added additional tests to ensure that the ch-2.pl was working correctly as the examples in the blog could be solved with code which terminated prematurely [you need to repeat the application of rule 2 until you add no new candies] - and looking at some of the solutions this is the case
-rw-r--r--challenge-080/james-smith/perl/ch-1.pl14
-rw-r--r--challenge-080/james-smith/perl/ch-2.pl18
2 files changed, 21 insertions, 11 deletions
diff --git a/challenge-080/james-smith/perl/ch-1.pl b/challenge-080/james-smith/perl/ch-1.pl
index 09526718d6..b0c63324fa 100644
--- a/challenge-080/james-smith/perl/ch-1.pl
+++ b/challenge-080/james-smith/perl/ch-1.pl
@@ -5,13 +5,19 @@ use warnings;
use feature qw(say);
-say smallest_number_sort(qw(200 1 -2 2 5 1000 -6 3000 ),1e6..1e7,6001..9001,3,4,3401..5900);
-say smallest_number_sort(qw(5 2 -2 0));
-say smallest_number_sort(qw(1 8 -1));
-say smallest_number_sort(qw(2 0 -1));
+use Test::More;
+
+is( smallest_number_sort( qw(200 1 -2 2 5 1000 -6 3000 ),6001..9001,3,4,3401..5900 ), 6 );
+is( smallest_number_sort( qw(5 2 -2 0) ), 1 );
+is( smallest_number_sort( qw(1 8 -1) ), 2 );
+is( smallest_number_sort( qw(-10 -8 -1) ), 1 );
+is( smallest_number_sort( qw(2 0 -1) ), 1 );
+
+done_testing;
sub smallest_number_sort {
my @q = sort { $a <=> $b } grep {$_>0} @_; ## Need +ve in order!
+ return 1 unless @q; ## No positive integers - avoids warn in next line
for( $_=1; $_ == shift @q; $_++ ) {} ## Loop through from 1.. exit loop if the array
## value isn't equal to index (1-based)
return $_; ## return value...
diff --git a/challenge-080/james-smith/perl/ch-2.pl b/challenge-080/james-smith/perl/ch-2.pl
index 9116f85539..a8eacfb22e 100644
--- a/challenge-080/james-smith/perl/ch-2.pl
+++ b/challenge-080/james-smith/perl/ch-2.pl
@@ -5,20 +5,24 @@ use warnings;
use feature qw(say);
-say candies( qw(1 2 2) );
-say candies( qw(1 4 3 2) );
+use Test::More;
+
+is( candies( qw(1 2 2) ), 4 );
+is( candies( qw(1 4 3 2) ), 7 );
+is( candies( qw(5 4 3 2 1) ), 15 );
+is( candies( qw(2 1 2 1 2 1 2) ), 11 );
+
+done_testing;
sub candies {
my @ranks = @_;
- my $prev_count = @candies = map { 1 } @ranks; ## First pass we set everything to 1!
+ my $prev_count = my @candies = map { 1 } @ranks; ## First pass we set everything to 1!
my $flag;
do {
my $count = 0;
foreach( 0..(@ranks-2) ) { ## Loop through comparing element to next one - increase as approprite
-
- $candies[$_] = $candies[$_+1]+1 if ($ranks[$_] > $ranks[$_+1]) && ($candies[$_] <= $candies[$_+1]);
- $candies[$_+1] = $candies[$_ ]+1 if ($ranks[$_] < $ranks[$_+1]) && ($candies[$_] => $candies[$_+1]);
-
+ $candies[$_+1] = $candies[$_ ]+1 if $ranks[$_] < $ranks[$_+1] && $candies[$_] >= $candies[$_+1];
+ $candies[$_] = $candies[$_+1]+1 if $ranks[$_] > $ranks[$_+1] && $candies[$_] <= $candies[$_+1];
$count += $candies[$_ ]; ## by the time we get here we would have done both comparisons that
## cause this entry to be updated..
}