diff options
| -rwxr-xr-x | challenge-212/peter-meszaros/perl/ch-1.pl | 62 | ||||
| -rwxr-xr-x | challenge-212/peter-meszaros/perl/ch-2.pl | 74 |
2 files changed, 136 insertions, 0 deletions
diff --git a/challenge-212/peter-meszaros/perl/ch-1.pl b/challenge-212/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..a8420c4b9f --- /dev/null +++ b/challenge-212/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,62 @@ +#!/usr//bin/perl +# Example 1 +# +# Input: $word = 'Perl' and @jump = (2,22,19,9) +# Output: Raku +# +# 'P' jumps 2 place forward and becomes 'R'. +# 'e' jumps 22 place forward and becomes 'a'. (jump is cyclic i.e. after 'z' you go back to 'a') +# 'r' jumps 19 place forward and becomes 'k'. +# 'l' jumps 9 place forward and becomes 'u'. +# +# Example 2 +# +# Input: $word = 'Raku' and @jump = (24,4,7,17) +# Output: 'Perl' + + +use strict; +use warnings; +use Test::More; + +my $cases = [ + ['Perl', [2,22,19,9]], + ['Raku', [24,4,7,17]], +]; + +sub jumping_letters +{ + my ($word, $jump) = @_; + my $limits = [ + [65, 90], + [97, 122], + ]; + + return undef unless length($word) == @$jump; + + my $ret; + for (my $i=0; $i < @$jump; ++$i) { + my $chr = ord(substr($word, $i, 1)); + my $lim; + if ($chr >= 65 and $chr <= 90) { # 65-90 A-Z + $lim = 0; + } elsif ($chr >= 97 and $chr <= 122) { # 97-122 a-z + $lim = 1; + } else { + return undef; + } + $chr += $jump->[$i]; + my $overreach = $chr - $limits->[$lim]->[1]; + $chr = $limits->[$lim]->[0] + $overreach - 1 if $overreach > 0; + $ret .= chr($chr); + } + + return $ret; +} + +is(jumping_letters($cases->[0]->@*), 'Raku', 'per-to-raku'); +is(jumping_letters($cases->[1]->@*), 'Perl', 'raku-to-perl'); +done_testing(); + +exit 0; + diff --git a/challenge-212/peter-meszaros/perl/ch-2.pl b/challenge-212/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..09f8590a4f --- /dev/null +++ b/challenge-212/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,74 @@ +#!/usr//bin/perl +# Example 1: +# +# Input: @list = (1,2,3,5,1,2,7,6,3) and $size = 3 +# Output: (1,2,3), (1,2,3), (5,6,7) +# +# Example 2: +# +# Input: @list = (1,2,3) and $size = 2 +# Output: -1 +# +# Example 3: +# +# Input: @list = (1,2,4,3,5,3) and $size = 3 +# Output: (1,2,3), (3,4,5) +# +# Example 4: +# +# Input: @list = (1,5,2,6,4,7) and $size = 3 +# Output: -1 + +use strict; +use warnings; +use Test::More; +use Data::Dumper; +use List::MoreUtils qw/frequency/; + +my $cases = [ + [[1,2,3,5,1,2,7,6,3], 3], + [[1,2,3], 2], + [[1,2,4,3,5,3], 3], + [[1,5,2,6,4,7], 3], +]; + +sub rearrange_groups +{ + my ($list, $size) = @_; + + return -1 if @$list % $size; + my $l = @$list / $size; + + my @s = sort {$a <=> $b} @$list; + + my $res = []; + push @$res, [] for 1..$l; + + for my $i (@s) { + my $added = 0; + for my $j (0..($l-1)) { + my $prev = $res->[$j]->[-1]; + next if $res->[$j]->@* >= $size; + if ((not defined $prev) or ($i == ($prev+1))) { + push $res->[$j]->@*, $i; + $added = 1; + last; + } + } + return -1 unless $added; + } + + return $res; +} + +is_deeply(rearrange_groups($cases->[0]->@*), [[1,2,3], [1,2,3], [5,6,7]], '[1,2,3,5,1,2,7,6,3], 3'); +is_deeply(rearrange_groups($cases->[1]->@*), -1, '[1,2,3], 2'); +is_deeply(rearrange_groups($cases->[2]->@*), [[1,2,3], [3,4,5]], '[1,2,4,3,5,3], 3'); +is_deeply(rearrange_groups($cases->[3]->@*), -1, '[1,5,2,6,4,7], 3'); + +done_testing(); + + + +exit 0; + |
