aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-322/mahnkong/perl/ch-1.pl17
-rw-r--r--challenge-322/mahnkong/perl/ch-2.pl21
2 files changed, 38 insertions, 0 deletions
diff --git a/challenge-322/mahnkong/perl/ch-1.pl b/challenge-322/mahnkong/perl/ch-1.pl
new file mode 100644
index 0000000000..97880ccd48
--- /dev/null
+++ b/challenge-322/mahnkong/perl/ch-1.pl
@@ -0,0 +1,17 @@
+use strict;
+use warnings;
+use feature 'signatures';
+use Test::More 'no_plan';
+
+sub run($str, $group_length) {
+ my $stripped = reverse($str) =~ s/-//gr;
+ my @parts;
+ for (my $i = 0; $i < length($stripped); $i += $group_length) {
+ push @parts, substr($stripped, $i, $group_length);
+ }
+ return reverse(join('-', @parts));
+}
+
+is(run("ABC-D-E-F", 3), "ABC-DEF", "Example 1");
+is(run("A-BC-D-E", 2), "A-BC-DE", "Example 2");
+is(run("-A-B-CD-E", 4), "A-BCDE", "Example 3");
diff --git a/challenge-322/mahnkong/perl/ch-2.pl b/challenge-322/mahnkong/perl/ch-2.pl
new file mode 100644
index 0000000000..587ed37256
--- /dev/null
+++ b/challenge-322/mahnkong/perl/ch-2.pl
@@ -0,0 +1,21 @@
+use strict;
+use warnings;
+use feature 'signatures';
+use Test::More 'no_plan';
+
+sub run(@ints) {
+ my %ints;
+ foreach my $i (sort { $a <=> $b } @ints) {
+ $ints{$i} = scalar(keys(%ints))+1 unless exists $ints{$i};
+ }
+ my @result;
+ foreach my $i (@ints) {
+ push @result, $ints{$i};
+ }
+
+ return \@result;
+}
+
+is_deeply(run(55, 22, 44, 33), [4, 1, 3, 2], "Example 1");
+is_deeply(run(10, 10, 10), [1, 1, 1], "Example 2");
+is_deeply(run(5, 1, 1, 4, 3), [4, 1, 1, 3, 2], "Example 3");