aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-10-20 16:54:43 +0100
committerGitHub <noreply@github.com>2025-10-20 16:54:43 +0100
commit95b9b178b6e519a51edb23d32fdf0577b9d32772 (patch)
tree5f4c2983d2c7ad7f4cf3c09da96f212adcd4f04c
parentcc5a259fc9b66953b482627a942554ad6dfa6b2a (diff)
parent545fa22fea378d0081ae92a9d41e999b1d0ed4fe (diff)
downloadperlweeklychallenge-club-95b9b178b6e519a51edb23d32fdf0577b9d32772.tar.gz
perlweeklychallenge-club-95b9b178b6e519a51edb23d32fdf0577b9d32772.tar.bz2
perlweeklychallenge-club-95b9b178b6e519a51edb23d32fdf0577b9d32772.zip
Merge pull request #12879 from mahnkong/challenge-344
Challenge 344
-rw-r--r--challenge-344/mahnkong/perl/ch-1.pl14
-rw-r--r--challenge-344/mahnkong/perl/ch-2.pl41
2 files changed, 55 insertions, 0 deletions
diff --git a/challenge-344/mahnkong/perl/ch-1.pl b/challenge-344/mahnkong/perl/ch-1.pl
new file mode 100644
index 0000000000..9f8f21e5ff
--- /dev/null
+++ b/challenge-344/mahnkong/perl/ch-1.pl
@@ -0,0 +1,14 @@
+use strict;
+use warnings;
+use feature 'signatures';
+use Test::More 'no_plan';
+
+sub run($ints, $x) {
+ return [ split("", join('', @$ints) + $x) ];
+}
+
+is_deeply(run([1,2,3,4], 12), [1,2,4,6], "Example 1");
+is_deeply(run([2,7,4], 181), [4,5,5], "Example 2");
+is_deeply(run([9,9,9], 1), [1,0,0,0], "Example 3");
+is_deeply(run([1,0,0,0,0], 9999), [1,9,9,9,9], "Example 4");
+is_deeply(run([0], 1000), [1,0,0,0], "Example 5");
diff --git a/challenge-344/mahnkong/perl/ch-2.pl b/challenge-344/mahnkong/perl/ch-2.pl
new file mode 100644
index 0000000000..1a4043198f
--- /dev/null
+++ b/challenge-344/mahnkong/perl/ch-2.pl
@@ -0,0 +1,41 @@
+use strict;
+use warnings;
+use feature 'signatures';
+use Test::More 'no_plan';
+
+sub permute(@items) {
+ return ([[ @items ]]) if @items <= 1;
+
+ my @result;
+
+ for my $i (0 .. $#items) {
+ my @rest = @items[0 .. $i-1, $i+1 .. $#items];
+ for my $p (@{ permute(@rest) }) {
+ push @result, [ $items[$i], @$p ];
+ }
+ }
+
+ return \@result;
+}
+
+sub run($source, $target) {
+ my @source_strings = map { join ("", @{$_}) } @$source;
+ my $target_string = join("", @$target);
+
+ my $permutations = permute(@source_strings);
+ foreach my $permutation (@$permutations) {
+ my $work = $target_string;
+ foreach my $s (@$permutation) {
+ $work =~ s/$s//;
+ }
+ return 1 if length($work) == 0;
+ }
+ return 0;
+}
+
+is(run([[2,3], [1], [4]], [1, 2, 3, 4]), 1, "Example 1");
+is(run([[1,3], [2,4]], [1, 2, 3, 4]), 0, "Example 2");
+is(run([[9,1], [5,8], [2]], [5, 8, 2, 9, 1]), 1, "Example 3");
+is(run([[1], [3]], [1, 2, 3]), 0, "Example 4");
+is(run([[7,4,6]], [7, 4, 6]), 1, "Example 5");
+is(run([[1,2,3],[4],[5,6],[1,2],[3,4,5]], [1,2,3,4,5,1,2,3,4,5,6]), 1, "Example from \@choroba");