diff options
| -rw-r--r-- | challenge-263/kjetillll/perl/ch-1.pl | 26 | ||||
| -rw-r--r-- | challenge-263/kjetillll/perl/ch-2.pl | 30 |
2 files changed, 56 insertions, 0 deletions
diff --git a/challenge-263/kjetillll/perl/ch-1.pl b/challenge-263/kjetillll/perl/ch-1.pl new file mode 100644 index 0000000000..e2cc2dda88 --- /dev/null +++ b/challenge-263/kjetillll/perl/ch-1.pl @@ -0,0 +1,26 @@ +use strict; use warnings; use Test::More; + +sub target_index { my $k = pop; my @s = sort {$a<=>$b} @_; grep $s[$_]==$k, 0..$#s } + +my @tests = ( + { + list => [1, 5, 3, 2, 4, 2], + k => 2, + want => [1, 2] + }, + { + list => [1, 2, 4, 3, 5], + k => 6, + want => [] + }, + { + list => [5, 3, 2, 4, 2, 1], + k => 4, + want => [4] + } +); +for my $test (@tests) { + my @got = target_index( @{$$test{list}}, $$test{k} ); + is_deeply(\@got, $$test{want}); +} +done_testing; diff --git a/challenge-263/kjetillll/perl/ch-2.pl b/challenge-263/kjetillll/perl/ch-2.pl new file mode 100644 index 0000000000..bb681736e7 --- /dev/null +++ b/challenge-263/kjetillll/perl/ch-2.pl @@ -0,0 +1,30 @@ +use strict; use warnings; use Test::More; + +sub merge_items { + my %idsum; + $idsum{ $$_[0] } += $$_[1] for map @$_, @_; + [ map [ $_, $idsum{$_} ], sort {$a<=>$b} keys %idsum ] +} + +my @tests = ( + { + items1 => [ [1,1], [2,1], [3,2] ], + items2 => [ [2,2], [1,3] ], + want => [ [1,4], [2,3], [3,2] ] + }, + { + items1 => [ [1,2], [2,3], [1,3], [3,2] ], + items2 => [ [3,1], [1,3] ], + want => [ [1,8], [2,3], [3,3] ] + }, + { + items1 => [ [1,1], [2,2], [3,3] ], + items2 => [ [2,3], [2,4] ], + want => [ [1,1], [2,9], [3,3] ] + } +); +for my $test (@tests) { + my $got = merge_items( @$test{'items1', 'items2'} ); + is_deeply( $got, $$test{want} ); +} +done_testing; |
