From 15344124d3e067484077c69389975e9345a267d8 Mon Sep 17 00:00:00 2001 From: Andreas Mahnke Date: Tue, 4 Feb 2025 16:57:45 +0100 Subject: Challenge 307 --- challenge-307/mahnkong/perl/ch-1.pl | 18 ++++++++++++++++++ challenge-307/mahnkong/perl/ch-2.pl | 21 +++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 challenge-307/mahnkong/perl/ch-1.pl create mode 100644 challenge-307/mahnkong/perl/ch-2.pl 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"); -- cgit