aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-10-13 06:31:18 +0100
committerGitHub <noreply@github.com>2020-10-13 06:31:18 +0100
commitfa02b7ce5bb2ddf0c10a2e69a8f710dcebbfc00a (patch)
tree00a9553d1d75ea898591d648ce4579e0d12b48b3
parent97ae2a0907840749a9b130352d0d523308208896 (diff)
parent42583b076195a2114e9635352d77898bf8345ba2 (diff)
downloadperlweeklychallenge-club-fa02b7ce5bb2ddf0c10a2e69a8f710dcebbfc00a.tar.gz
perlweeklychallenge-club-fa02b7ce5bb2ddf0c10a2e69a8f710dcebbfc00a.tar.bz2
perlweeklychallenge-club-fa02b7ce5bb2ddf0c10a2e69a8f710dcebbfc00a.zip
Merge pull request #2506 from choroba/ech082
Add solutions to 082 by E. Choroba
-rwxr-xr-xchallenge-082/e-choroba/perl5/ch-1.pl23
-rwxr-xr-xchallenge-082/e-choroba/perl5/ch-2.pl42
2 files changed, 65 insertions, 0 deletions
diff --git a/challenge-082/e-choroba/perl5/ch-1.pl b/challenge-082/e-choroba/perl5/ch-1.pl
new file mode 100755
index 0000000000..bac0ff5500
--- /dev/null
+++ b/challenge-082/e-choroba/perl5/ch-1.pl
@@ -0,0 +1,23 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+
+sub common_factors {
+ my ($m, $n) = @_;
+ ($m, $n) = ($n, $m) if $n < $m;
+ my @r;
+ for my $i (1 .. $m) {
+ push @r, $i if 0 == $m % $i
+ && 0 == $n % $i;
+ }
+ return \@r
+}
+
+use Test::More;
+
+is_deeply common_factors(12, 18), [1, 2, 3, 6], 'Example 1';
+is_deeply common_factors(18, 23), [1], 'Example 2';
+is_deeply common_factors(123_456_789, 987_654_321), [1, 3, 9], 'large numbers';
+
+done_testing();
+
diff --git a/challenge-082/e-choroba/perl5/ch-2.pl b/challenge-082/e-choroba/perl5/ch-2.pl
new file mode 100755
index 0000000000..e61bbe3dd4
--- /dev/null
+++ b/challenge-082/e-choroba/perl5/ch-2.pl
@@ -0,0 +1,42 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+
+sub interleave_string {
+ my ($x, $y, $z) = @_;
+ return 0 if length($z) != length($x) + length($y);
+
+ return 1 if "" eq $z;
+
+ my ($first_x, $first_y, $first_z) = map { substr $_, 0, 1 } $x, $y, $z;
+
+ my $maybe_x = $first_x eq $first_z;
+ my $maybe_y = $first_y eq $first_z;
+
+ my $rest_x = $maybe_x ? substr $x, 1 : "";
+ my $rest_y = $maybe_y ? substr $y, 1 : "";
+ my $rest_z = substr $z, 1;
+
+ if ($maybe_x && $maybe_y) {
+ return interleave_string($rest_x, $y, $rest_z)
+ || interleave_string($x, $rest_y, $rest_z)
+ }
+
+ return interleave_string($rest_x, $y, $rest_z) if $maybe_x;
+
+ return interleave_string($x, $rest_y, $rest_z) if $maybe_y;
+
+ return 0
+}
+
+use Test::More;
+
+is interleave_string('XY', 'X', 'XXY'), 1, 'Example 1';
+is interleave_string('XXY', 'XXZ', 'XXXXZY'), 1, 'Example 2';
+is interleave_string('YX', 'X', 'XXY'), 0, 'Example 3';
+
+is interleave_string('ABC', 'BD', $_), 1, $_
+ for qw( ABDBC ABCBD ABBCD ABBDC BABCD BABDC BADBC BDABC );
+
+
+done_testing();