aboutsummaryrefslogtreecommitdiff
path: root/challenge-081
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-081')
-rw-r--r--challenge-081/pete-houston/perl/ch-1.pl40
-rw-r--r--challenge-081/pete-houston/perl/ch-2.pl40
2 files changed, 80 insertions, 0 deletions
diff --git a/challenge-081/pete-houston/perl/ch-1.pl b/challenge-081/pete-houston/perl/ch-1.pl
new file mode 100644
index 0000000000..1fa096d9b1
--- /dev/null
+++ b/challenge-081/pete-houston/perl/ch-1.pl
@@ -0,0 +1,40 @@
+#!/usr/bin/env perl
+#===============================================================================
+#
+# FILE: 8101.pl
+#
+# USAGE: ./8101.pl string1 string2
+#
+# DESCRIPTION: Find complete common substrings
+#
+# REQUIREMENTS: Math::Prime::Util
+# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk
+# ORGANIZATION: Openstrike
+# VERSION: 1.0
+# CREATED: 05/10/20
+#===============================================================================
+
+use strict;
+use warnings;
+use Math::Prime::Util 'gcd';
+
+# Find longest possible substring
+my @str = @ARGV[0, 1];
+my @len = map { length } @str;
+my $longest = gcd (@len);
+
+# Count up
+my $ans;
+LENGTH:
+for my $i (1 .. $longest) {
+ my $substr = substr ($str[0], 0, $i);
+ last unless substr ($str[1], 0, $i) eq $substr;
+ for my $j (0, 1) {
+ next LENGTH if $len[$j] % $i;
+ next LENGTH if $str[$j] ne $substr x ($len[$j] / $i);
+ }
+ $ans = $substr;
+}
+
+$ans //= 'No common base string';
+print "$ans\n";
diff --git a/challenge-081/pete-houston/perl/ch-2.pl b/challenge-081/pete-houston/perl/ch-2.pl
new file mode 100644
index 0000000000..c527ee6509
--- /dev/null
+++ b/challenge-081/pete-houston/perl/ch-2.pl
@@ -0,0 +1,40 @@
+#!/usr/bin/env perl
+#===============================================================================
+#
+# FILE: 8102.pl
+#
+# USAGE: ./8102.pl [ infile ]
+#
+# DESCRIPTION: Word frequencies
+#
+# OPTIONS: Read data from STDIN if input filename is missing
+# REQUIREMENTS: List::MoreUtils 0.42 or newer
+# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk
+# ORGANIZATION: Openstrike
+# VERSION: 1.0
+# CREATED: 05/10/20
+#===============================================================================
+
+use strict;
+use warnings;
+
+use List::MoreUtils 0.420 'occurrences';
+
+# Optionally open the file
+my $fh = \*STDIN;
+if (defined $ARGV[0]) {
+ open $fh, '<', $ARGV[0] or die "Cannot open $ARGV[0]: $!";
+}
+
+# Convert the input into an array of words
+my @words;
+while (<$fh>) {
+ s/[."(),]|'s|--//g;
+ push @words, split;
+}
+
+# Find and display the frequencies
+my @occ = occurrences (@words);
+for my $i (0 .. $#occ) {
+ print "$i: " . join (' ', sort @{$occ[$i]}) . "\n" if defined $occ[$i];
+}