diff options
| author | pme <hauptadler@gmail.com> | 2024-05-07 18:07:38 +0200 |
|---|---|---|
| committer | pme <hauptadler@gmail.com> | 2024-05-07 18:07:38 +0200 |
| commit | 15e1e3faccb6873a292cc855b1b09f0b1f4ddb25 (patch) | |
| tree | ab475fec5dab2cfeb75229b058f131303b60fb5f | |
| parent | c1756b0e7aed0ad70fa63feb2565c69215c9d426 (diff) | |
| download | perlweeklychallenge-club-15e1e3faccb6873a292cc855b1b09f0b1f4ddb25.tar.gz perlweeklychallenge-club-15e1e3faccb6873a292cc855b1b09f0b1f4ddb25.tar.bz2 perlweeklychallenge-club-15e1e3faccb6873a292cc855b1b09f0b1f4ddb25.zip | |
challenge-268
| -rwxr-xr-x | challenge-268/peter-meszaros/perl/ch-1.pl | 75 | ||||
| -rwxr-xr-x | challenge-268/peter-meszaros/perl/ch-2.pl | 64 |
2 files changed, 139 insertions, 0 deletions
diff --git a/challenge-268/peter-meszaros/perl/ch-1.pl b/challenge-268/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..8a9c68632a --- /dev/null +++ b/challenge-268/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,75 @@ +#!/usr/bin/env perl +# +=head1 Task 1: Magic Number + +You are given two arrays of integers of same size, @x and @y. + +Write a script to find the magic number that when added to each elements of one +of the array gives the second array. Elements order is not important. + +=head2 Example 1 + + Input: @x = (3, 7, 5) + @y = (9, 5, 7) + Output: 2 + + The magic number is 2. + @x = (3, 7, 5) + + 2 2 2 + @y = (5, 9, 7) + +=head2 Example 2 + + Input: @x = (1, 2, 1) + @y = (5, 4, 4) + Output: 3 + + The magic number is 3. + @x = (1, 2, 1) + + 3 3 3 + @y = (5, 4, 4) + +=head2 Example 3 + + Input: @x = (2) + @y = (5) + Output: 3 + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [[[3, 7, 5], [9, 5, 7]], 2], + [[[1, 2, 1], [5, 4, 4]], 3], + [[[2], [5]], 3], +]; + +sub magic_number +{ + my $l1 = $_[0]->[0]; + my $l2 = $_[0]->[1]; + + return undef unless @$l1 == @$l2; + + my @l1 = sort {$a <=> $b} @$l1; + my @l2 = sort {$a <=> $b} @$l2; + + my $diff = $l2[0] - $l1[0]; + + for (my $i=1; $i<@l1; $i++) { + return undef if $l2[$i] - $l1[$i] != $diff; + } + return $diff; +} + +for (@$cases) { + is(magic_number($_->[0]), $_->[1], $_->[2]); +} + +done_testing(); + +exit 0; diff --git a/challenge-268/peter-meszaros/perl/ch-2.pl b/challenge-268/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..924ddeb1e7 --- /dev/null +++ b/challenge-268/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,64 @@ +#!/usr/bin/env perl +# +=head1 Task 2: Number Game + +You are given an array of integers, @ints, with even number of elements. + +Write a script to create a new array made up of elements of the given array. +Pick the two smallest integers and add it to new array in decreasing order i.e. +high to low. Keep doing until the given array is empty. + +=head2 Example 1 + + Input: @ints = (2, 5, 3, 4) + Output: (3, 2, 5, 4) + + Round 1: we picked (2, 3) and push it to the new array (3, 2) + Round 2: we picked the remaining (4, 5) and push it to the new array (5, 4) + +=head2 Example 2 + + Input: @ints = (9, 4, 1, 3, 6, 4, 6, 1) + Output: (1, 1, 4, 3, 6, 4, 9, 6) + +=head2 Example 3 + + Input: @ints = (1, 2, 2, 3) + Output: (2, 1, 3, 2) + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [[2, 5, 3, 4], [3, 2, 5, 4]], + [[9, 4, 1, 3, 6, 4, 6, 1], [1, 1, 4, 3, 6, 4, 9, 6]], + [[1, 2, 2, 3], [2, 1, 3, 2]], +]; + +sub number_game +{ + my $l = shift; + + return undef if @$l % 2; + + my @l = sort {$a <=> $b} @$l; + my @res; + + for (my $i=0; $i<@l; $i+=2 ) { + push @res, @l[$i+1, $i]; + } + + return \@res; +} + +for (@$cases) { + is(number_game($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; + |
