aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-12-24 01:47:51 +0000
committerGitHub <noreply@github.com>2020-12-24 01:47:51 +0000
commit3787ff8861abfb0a87c7de645b4464dc58c8d199 (patch)
treef8ff7244a00b75e13334095bd0d1072c66640faf
parent29f744c336da20257f6d80c336b5acbd6e8b2fc6 (diff)
parentd1f9eb35b8b956289fd0b597ebcd4674774eb3d4 (diff)
downloadperlweeklychallenge-club-3787ff8861abfb0a87c7de645b4464dc58c8d199.tar.gz
perlweeklychallenge-club-3787ff8861abfb0a87c7de645b4464dc58c8d199.tar.bz2
perlweeklychallenge-club-3787ff8861abfb0a87c7de645b4464dc58c8d199.zip
Merge pull request #3061 from pauloscustodio/082-perl
Add Perl solution to challenge 082
-rw-r--r--challenge-082/paulo-custodio/README1
-rw-r--r--challenge-082/paulo-custodio/perl/ch-1.pl51
-rw-r--r--challenge-082/paulo-custodio/perl/ch-2.pl54
-rw-r--r--challenge-082/paulo-custodio/test.pl23
4 files changed, 129 insertions, 0 deletions
diff --git a/challenge-082/paulo-custodio/README b/challenge-082/paulo-custodio/README
new file mode 100644
index 0000000000..87dc0b2fbd
--- /dev/null
+++ b/challenge-082/paulo-custodio/README
@@ -0,0 +1 @@
+Solution by Paulo Custodio
diff --git a/challenge-082/paulo-custodio/perl/ch-1.pl b/challenge-082/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..cebd3818a8
--- /dev/null
+++ b/challenge-082/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,51 @@
+#!/usr/bin/env perl
+
+# 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
+
+use strict;
+use warnings;
+use 5.030;
+
+my($M, $N) = @ARGV;
+my @common = common_factors($M, $N);
+say "(", join(", ", @common), ")";
+
+sub common_factors {
+ my($a, $b) = @_;
+ my @common;
+ for (my $i = 1; 2*$i <= $a || 2*$i <= $b; $i++) {
+ if (($a % $i)==0 && ($b % $i)==0) {
+ push @common, $i;
+ }
+ }
+ return @common;
+}
diff --git a/challenge-082/paulo-custodio/perl/ch-2.pl b/challenge-082/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..1fb9380284
--- /dev/null
+++ b/challenge-082/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,54 @@
+#!/usr/bin/env perl
+
+# 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
+
+use strict;
+use warnings;
+use 5.030;
+
+say interleaved(@ARGV);
+
+
+sub interleaved {
+ my($a, $b, $c) = @_;
+ for my $i (0 .. length($a)) {
+ if (substr($a,0,$i).$b.substr($a,$i) eq $c) {
+ return 1;
+ }
+ }
+ return 0;
+}
diff --git a/challenge-082/paulo-custodio/test.pl b/challenge-082/paulo-custodio/test.pl
new file mode 100644
index 0000000000..0eae993f89
--- /dev/null
+++ b/challenge-082/paulo-custodio/test.pl
@@ -0,0 +1,23 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use Test::More;
+use 5.030;
+
+is capture("perl/ch-1.pl 12 18"), "(1, 2, 3, 6)\n";
+is capture("perl/ch-1.pl 18 23"), "(1)\n";
+
+is capture("perl/ch-2.pl XY X XXY"), "1\n";
+is capture("perl/ch-2.pl XY Z XYZ"), "1\n";
+is capture("perl/ch-2.pl XXY XXZ XXXXZY"), "1\n";
+is capture("perl/ch-2.pl YX X XXY"), "0\n";
+
+done_testing;
+
+sub capture {
+ my($cmd) = @_;
+ my $out = `$cmd`;
+ $out =~ s/[ \t\v\f\r]*\n/\n/g;
+ return $out;
+}