aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-02-04 17:50:05 +0000
committerGitHub <noreply@github.com>2025-02-04 17:50:05 +0000
commit73485c924d48b26fb6f700a80ef86b8ba84f94cc (patch)
tree7bbbf88b7e25c3de7f8819fb1e41f5edd00d1da8
parentda7e80045989d36984466bdbd93e72c625065dd8 (diff)
parent15344124d3e067484077c69389975e9345a267d8 (diff)
downloadperlweeklychallenge-club-73485c924d48b26fb6f700a80ef86b8ba84f94cc.tar.gz
perlweeklychallenge-club-73485c924d48b26fb6f700a80ef86b8ba84f94cc.tar.bz2
perlweeklychallenge-club-73485c924d48b26fb6f700a80ef86b8ba84f94cc.zip
Merge pull request #11532 from mahnkong/challenge-307
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");