aboutsummaryrefslogtreecommitdiff
path: root/challenge-082
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2020-10-17 15:42:52 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2020-10-17 15:42:52 +0100
commite8d83a10c2044eba1be500684d2caa4577400c85 (patch)
tree7abf6aa54f53823e9237104bc5fd0dbd4a51e403 /challenge-082
parent1da19f820e6c7dc5844eb1c6ea53c11be9fc3877 (diff)
downloadperlweeklychallenge-club-e8d83a10c2044eba1be500684d2caa4577400c85.tar.gz
perlweeklychallenge-club-e8d83a10c2044eba1be500684d2caa4577400c85.tar.bz2
perlweeklychallenge-club-e8d83a10c2044eba1be500684d2caa4577400c85.zip
- Added solutions by Pete Houston.
Diffstat (limited to 'challenge-082')
-rw-r--r--challenge-082/pete-houston/perl/ch-1.pl38
-rw-r--r--challenge-082/pete-houston/perl/ch-2.pl50
2 files changed, 88 insertions, 0 deletions
diff --git a/challenge-082/pete-houston/perl/ch-1.pl b/challenge-082/pete-houston/perl/ch-1.pl
new file mode 100644
index 0000000000..52857706a6
--- /dev/null
+++ b/challenge-082/pete-houston/perl/ch-1.pl
@@ -0,0 +1,38 @@
+#!/usr/bin/env perl
+#===============================================================================
+#
+# FILE: 8201.pl
+#
+# USAGE: ./8201.pl A B
+#
+# DESCRIPTION: Display all common divisors of natural numbers A and B
+#
+# REQUIREMENTS: Params::Util, List::Util
+# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk
+# ORGANIZATION: Openstrike
+# VERSION: 1.0
+# CREATED: 17/10/20
+#===============================================================================
+
+use strict;
+use warnings;
+
+use Params::Util '_POSINT';
+use List::Util qw/min none/;
+
+# Validate input
+for (@ARGV) {
+ die "$_ must be a natural number" unless _POSINT ($_);
+}
+
+# Start at highest possible potential divisor
+my $i = min (@ARGV);
+my @div;
+
+# Iterate downwards to 1
+while ($i > 0) {
+ push @div, $i if none { $_ % $i } @ARGV;
+ $i--;
+}
+
+print "@div\n";
diff --git a/challenge-082/pete-houston/perl/ch-2.pl b/challenge-082/pete-houston/perl/ch-2.pl
new file mode 100644
index 0000000000..5b80c7bd22
--- /dev/null
+++ b/challenge-082/pete-houston/perl/ch-2.pl
@@ -0,0 +1,50 @@
+#!/usr/bin/env perl
+#===============================================================================
+#
+# FILE: 8202.pl
+#
+# USAGE: ./8202.pl substr1 substr2 str
+#
+# DESCRIPTION: Report if str can be made by "interleaving" the substrs
+#
+# NOTES: Prints 1 if the string can be made, otherwise 0
+# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk
+# ORGANIZATION: Openstrike
+# VERSION: 1.0
+# CREATED: 17/10/20
+#===============================================================================
+
+use strict;
+use warnings;
+
+my @sub = @ARGV;
+my $str = pop @sub;
+
+# No point going further if this basic test fails
+nope () if length ($str) ne length ($sub[0] . $sub[1]);
+
+# Try tucking first substring into second, then vice-versa
+for my $i (0, 1) {
+ my $j = 1 - $i;
+ my $jpos = index ($str, $sub[$j]);
+ while ($jpos > -1) {
+ # $sub[$j] is contained within $str, so strip it and see if we
+ # are left with $sub[$i]
+ my $copy = $str;
+ substr ($copy, $jpos, length ($sub[$j]), '');
+ yep () if $copy eq $sub[$i];
+ # No joy, so keep looking
+ $jpos = index ($str, $sub[$j], $jpos + 1);
+ }
+}
+nope ();
+
+sub yep {
+ print "1\n";
+ exit;
+}
+
+sub nope {
+ print "0\n";
+ exit;
+}