aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNuno Vieira <nunovieira220@gmail.com>2020-10-16 21:28:06 +0100
committerNuno Vieira <nunovieira220@gmail.com>2020-10-16 21:28:06 +0100
commit6c79809ecc52ebb517c521d9095ae5785e9c1862 (patch)
treeb6a1ff08e7f1366797cfc963541cd582ace1122d
parentc5a68bd82df0591e7b379cdbf6b124bcf13d8bcc (diff)
downloadperlweeklychallenge-club-6c79809ecc52ebb517c521d9095ae5785e9c1862.tar.gz
perlweeklychallenge-club-6c79809ecc52ebb517c521d9095ae5785e9c1862.tar.bz2
perlweeklychallenge-club-6c79809ecc52ebb517c521d9095ae5785e9c1862.zip
Add nunovieira220 perl solution to challenge 082
-rw-r--r--challenge-082/nunovieira220/perl/ch-1.pl18
-rw-r--r--challenge-082/nunovieira220/perl/ch-2.pl39
2 files changed, 57 insertions, 0 deletions
diff --git a/challenge-082/nunovieira220/perl/ch-1.pl b/challenge-082/nunovieira220/perl/ch-1.pl
new file mode 100644
index 0000000000..c0026791b3
--- /dev/null
+++ b/challenge-082/nunovieira220/perl/ch-1.pl
@@ -0,0 +1,18 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use List::Util qw[min];
+
+# Input
+my $M = 12;
+my $N = 18;
+
+# Get common factors
+my @res = ();
+for (1..min($M, $N)) {
+ push @res, $_ if($M % $_ == 0 && $N % $_ == 0);
+}
+
+# Output
+print join(', ', @res); \ No newline at end of file
diff --git a/challenge-082/nunovieira220/perl/ch-2.pl b/challenge-082/nunovieira220/perl/ch-2.pl
new file mode 100644
index 0000000000..d0926b6a3b
--- /dev/null
+++ b/challenge-082/nunovieira220/perl/ch-2.pl
@@ -0,0 +1,39 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use feature qw(say);
+
+# Input
+my $A = "XXY";
+my $B = "XXZ";
+my $C = "XXXXZY";
+
+# Output
+say interleave($A, $B, $C);
+
+# Interleave
+sub interleave {
+ my ($A, $B, $C) = @_;
+
+ return 0 if(length($A) + length($B) != length($C));
+ return interleaveChecker($A, $B, $C);
+}
+
+# Interleave checker
+sub interleaveChecker {
+ my ($A, $B, $C) = @_;
+
+ return 1 if (!$C);
+
+ my $a = substr($A, 0, 1);
+ my $b = substr($B, 0, 1);
+ my $c = substr($C, 0, 1);
+ my $firstTree = 0;
+ my $secondTree = 0;
+
+ $firstTree = interleaveChecker(substr($A, 1, 1), $B, substr($C, 1, 1)) if($a eq $c);
+ $secondTree = interleaveChecker($A, substr($B, 1, 1), substr($C, 1, 1)) if($b eq $c);
+
+ return $firstTree + $secondTree > 0 || 0;
+} \ No newline at end of file