aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Mahnke <andreas.mahnke@leuphana.de>2025-02-04 16:57:45 +0100
committerAndreas Mahnke <andreas.mahnke@leuphana.de>2025-02-04 16:57:45 +0100
commit15344124d3e067484077c69389975e9345a267d8 (patch)
treeb1fa9b83831081aef4f03f0ae8f0b32c996c5f5d
parent0a2c7d626adb83617b1f4560ee5aaeae07a68989 (diff)
downloadperlweeklychallenge-club-15344124d3e067484077c69389975e9345a267d8.tar.gz
perlweeklychallenge-club-15344124d3e067484077c69389975e9345a267d8.tar.bz2
perlweeklychallenge-club-15344124d3e067484077c69389975e9345a267d8.zip
Challenge 307
-rw-r--r--challenge-307/mahnkong/perl/ch-1.pl18
-rw-r--r--challenge-307/mahnkong/perl/ch-2.pl21
2 files changed, 39 insertions, 0 deletions
diff --git a/challenge-307/mahnkong/perl/ch-1.pl b/challenge-307/mahnkong/perl/ch-1.pl
new file mode 100644
index 0000000000..3db4d28ea8
--- /dev/null
+++ b/challenge-307/mahnkong/perl/ch-1.pl
@@ -0,0 +1,18 @@
+use strict;
+use warnings;
+use feature 'signatures';
+use Test::More 'no_plan';
+
+sub run(@ints) {
+ my @sorted = sort { $a <=> $b } @ints;
+ my $diffing_indices = [];
+
+ for (my $i = 0; $i < scalar(@ints); $i++) {
+ push @$diffing_indices, $i if ($ints[$i] != $sorted[$i]);
+ }
+ return $diffing_indices;
+}
+
+is_deeply(run(5, 2, 4, 3, 1), [0, 2, 3, 4], "Example 1");
+is_deeply(run(1, 2, 1, 1, 3), [1, 3], "Example 2");
+is_deeply(run(3, 1, 3, 2, 3), [0, 1, 3], "Example 3");
diff --git a/challenge-307/mahnkong/perl/ch-2.pl b/challenge-307/mahnkong/perl/ch-2.pl
new file mode 100644
index 0000000000..34733a264c
--- /dev/null
+++ b/challenge-307/mahnkong/perl/ch-2.pl
@@ -0,0 +1,21 @@
+use strict;
+use warnings;
+use feature 'signatures';
+use Test::More 'no_plan';
+
+sub run(@words) {
+ @words = reverse(@words);
+ while (1) {
+ my $max_index = $#words;
+ for (my $i = $max_index; $i > 0; $i--) {
+ if (join('', sort(split(//, $words[$i]))) eq join('', sort(split(//, $words[$i-1])))) {
+ splice(@words, $i, 1);
+ }
+ }
+ last if $max_index == $#words;
+ }
+ return scalar(@words);
+}
+
+is(run("acca", "dog", "god", "perl", "repl"), 3, "Example 1");
+is(run("abba", "baba", "aabb", "ab", "ab"), 2, "Example 2");