aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-10-19 00:41:14 +0100
committerGitHub <noreply@github.com>2020-10-19 00:41:14 +0100
commit2b9c4d0c8778cae02921b1ed497ee83e551141f3 (patch)
treeabb006b7e4f1bc2bd43ce0d3f6800934ec8ad112
parent72a35bac7fc77652e0219cb2b48a3891a2be06e0 (diff)
parent3816d67aecb8ceb438d13e82208638931f3ad182 (diff)
downloadperlweeklychallenge-club-2b9c4d0c8778cae02921b1ed497ee83e551141f3.tar.gz
perlweeklychallenge-club-2b9c4d0c8778cae02921b1ed497ee83e551141f3.tar.bz2
perlweeklychallenge-club-2b9c4d0c8778cae02921b1ed497ee83e551141f3.zip
Merge pull request #2549 from ddobbelaere/solution-082
Add solutions for challenge 082.
-rw-r--r--challenge-082/ddobbelaere/README1
-rw-r--r--challenge-082/ddobbelaere/perl/ch-1.pl68
-rw-r--r--challenge-082/ddobbelaere/perl/ch-2.pl78
3 files changed, 147 insertions, 0 deletions
diff --git a/challenge-082/ddobbelaere/README b/challenge-082/ddobbelaere/README
new file mode 100644
index 0000000000..36f8cdcd67
--- /dev/null
+++ b/challenge-082/ddobbelaere/README
@@ -0,0 +1 @@
+Solution by Dieter Dobbelaere
diff --git a/challenge-082/ddobbelaere/perl/ch-1.pl b/challenge-082/ddobbelaere/perl/ch-1.pl
new file mode 100644
index 0000000000..b7d7385f30
--- /dev/null
+++ b/challenge-082/ddobbelaere/perl/ch-1.pl
@@ -0,0 +1,68 @@
+#!/usr/bin/perl
+
+=begin
+https://perlweeklychallenge.org/blog/perl-weekly-challenge-082/
+
+TASK #1 › Common Factors
+Submitted by: Niels van Dijke
+
+You are given 2 positive numbers $M and $N.
+
+Write a script to list all common factors of the given numbers.
+
+Example 1:
+
+Input:
+ $M = 12
+ $N = 18
+
+Output:
+ (1, 2, 3, 6)
+
+Explanation:
+ Factors of 12: 1, 2, 3, 4, 6
+ Factors of 18: 1, 2, 3, 6, 9
+
+Example 2:
+
+Input:
+ $M = 18
+ $N = 23
+
+Output:
+ (1)
+
+Explanation:
+ Factors of 18: 1, 2, 3, 6, 9
+ Factors of 23: 1
+=cut
+
+use v5.30;
+use warnings;
+use experimental 'smartmatch';
+
+sub common_factors {
+ my ($M, $N) = @_;
+ grep {!($N%$_ || $M%$_)} (1..$M);
+}
+
+# Tests.
+while (<DATA>) {
+ next if /^#/;
+ /\(([^\)]*)\) \(([^\)]*)\)/;
+ my ($M, $N) = split /,/, $1;
+ my @expected = split /,/, $2;
+
+ my @actual = common_factors($M, $N);
+ @expected ~~ @actual || die "Error for (M,N)=($M,$N). Expected @expected, got @actual.";
+}
+
+# Console interface.
+say join ', ', common_factors(@ARGV) if @ARGV;
+
+__DATA__
+# Test data.
+(12, 18) (1, 2, 3, 6)
+(18, 12) (1, 2, 3, 6)
+(18, 23) (1)
+(23, 18) (1) \ No newline at end of file
diff --git a/challenge-082/ddobbelaere/perl/ch-2.pl b/challenge-082/ddobbelaere/perl/ch-2.pl
new file mode 100644
index 0000000000..1bd52312a6
--- /dev/null
+++ b/challenge-082/ddobbelaere/perl/ch-2.pl
@@ -0,0 +1,78 @@
+#!/usr/bin/perl
+
+=begin
+https://perlweeklychallenge.org/blog/perl-weekly-challenge-082/
+
+TASK #2 › Interleave String
+Submitted by: Mohammad S Anwar
+
+You are given 3 strings; $A, $B and $C.
+
+Write a script to check if $C is created by interleave $A and $B.
+
+Print 1 if check is success otherwise 0.
+Example 1:
+
+Input:
+ $A = "XY"
+ $B = "X"
+ $C = "XXY"
+
+Output: 1
+
+EXPLANATION
+
+"X" (from $B) + "XY" (from $A) = $C
+
+Example 2:
+
+Input:
+ $A = "XXY"
+ $B = "XXZ"
+ $C = "XXXXZY"
+
+Output: 1
+
+EXPLANATION
+
+"XX" (from $A) + "XXZ" (from $B) + "Y" (from $A) = $C
+
+Example 3:
+
+Input:
+ $A = "YX"
+ $B = "X"
+ $C = "XXY"
+
+Output: 0
+=cut
+
+use v5.30;
+use warnings;
+
+sub can_interleave {
+ my ($A, $B, $C) = @_;
+ ($A eq '' && $B eq '' && $C eq '')
+ || (length $A && substr($A,-1) eq substr($C,-1) && can_interleave(substr($A,0,-1), $B, substr($C,0,-1)))
+ || (length $B && substr($B,-1) eq substr($C,-1) && can_interleave($A, substr($B,0,-1), substr($C,0,-1)));
+}
+
+# Tests.
+while (<DATA>) {
+ next if /^#/;
+ /(\S*)\s*(\S*)\s*(\S*)\s*([01])/;
+ my ($A, $B, $C, $expected) = ($1, $2, $3, $4);
+
+ $expected == can_interleave($A, $B, $C) || die "Error for (A,B,C)=$A,$B,$C. Expected $expected.";
+}
+
+# Console interface.
+say 0+can_interleave(@ARGV) if @ARGV;
+
+__DATA__
+# Test data of the form $A $B $C $output.
+XY X XXY 1
+XXY XXZ XXXXZY 1
+YX X XXY 0
+ABCDEF ABCCDEF AABBCCDEFCDEF 1
+ABCCDEF ABCDEF AABBCCDEFCDEF 1 \ No newline at end of file