aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-212/carlos-oliveira/perl/ch-1.pl34
-rw-r--r--challenge-212/carlos-oliveira/perl/ch-2.pl34
2 files changed, 68 insertions, 0 deletions
diff --git a/challenge-212/carlos-oliveira/perl/ch-1.pl b/challenge-212/carlos-oliveira/perl/ch-1.pl
new file mode 100644
index 0000000000..ebadf61ced
--- /dev/null
+++ b/challenge-212/carlos-oliveira/perl/ch-1.pl
@@ -0,0 +1,34 @@
+use strict;
+use warnings;
+use v5.36;
+
+use Test::More;
+
+# alphabet code with 'a' as 0
+sub normalize_letter_code ($letter) {
+ return ord($letter) - ord('a');
+}
+
+# return to its ASCII char form
+sub denormalize_letter_code ($normalized_letter_code) {
+ return chr($normalized_letter_code + ord('a'));
+}
+
+my $alphabet_length = normalize_letter_code('z') + 1;
+
+sub jump_letters ($word, @jumps) {
+ die '$word must have as much letters as @jumps\' size!' unless length $word == @jumps;
+ my $jumpsIndex = 0;
+ return join '', map {
+ my $is_upper_case = uc($_) eq $_;
+ my $letter = lc($_);
+ my $new_letter = denormalize_letter_code((normalize_letter_code($letter) + $jumps[$jumpsIndex++]) % $alphabet_length);
+ $new_letter = uc($new_letter) if $is_upper_case;
+ $new_letter;
+ } split //, $word
+}
+
+is jump_letters('Perl', (2, 22, 19, 9)), 'Raku';
+is jump_letters('Raku', (24, 4, 7, 17)), 'Perl';
+
+done_testing;
diff --git a/challenge-212/carlos-oliveira/perl/ch-2.pl b/challenge-212/carlos-oliveira/perl/ch-2.pl
new file mode 100644
index 0000000000..f9faf1d3b3
--- /dev/null
+++ b/challenge-212/carlos-oliveira/perl/ch-2.pl
@@ -0,0 +1,34 @@
+use strict;
+use warnings;
+use v5.36;
+
+use Test::More;
+use List::UtilsBy qw(extract_first_by);
+
+sub arrange_groups ($size, @list) {
+ return -1 if @list % $size;
+ my @sorted_list = sort @list;
+ my @groups;
+ for my $first_in_group (@sorted_list) {
+ my @group = ($first_in_group);
+ for my $next_in_group ($first_in_group + 1 .. $first_in_group + $size - 1) {
+ my $val = extract_first_by { $_ == $next_in_group } @sorted_list;
+ return -1 unless $val;
+ push @group, $val;
+ }
+ push @groups, \@group;
+ }
+ return @groups;
+}
+
+is_deeply [arrange_groups (3, (1, 2, 3, 5, 1, 2, 7, 6, 3))],
+ [ [1 ,2, 3], [1, 2, 3], [5, 6, 7] ];
+
+is_deeply [arrange_groups (2, (1, 2, 3))], [-1];
+
+is_deeply [arrange_groups (3, (1, 2, 4, 3, 5, 3))],
+ [ [1 ,2, 3], [3, 4, 5] ];
+
+is_deeply [arrange_groups (3, (1, 5, 2, 6, 4, 7))], [-1];
+
+done_testing;