aboutsummaryrefslogtreecommitdiff
path: root/challenge-081
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2020-10-11 14:06:07 +0200
committerAbigail <abigail@abigail.be>2020-10-11 14:12:37 +0200
commitaaf1f7ffc97a980001aa9d9572f1e621736db7f9 (patch)
tree911a4e7adbb110b80f69480fa4c09086fb5dce39 /challenge-081
parent4349abc09357350a8212f95e27c3000aee78adbc (diff)
downloadperlweeklychallenge-club-aaf1f7ffc97a980001aa9d9572f1e621736db7f9.tar.gz
perlweeklychallenge-club-aaf1f7ffc97a980001aa9d9572f1e621736db7f9.tar.bz2
perlweeklychallenge-club-aaf1f7ffc97a980001aa9d9572f1e621736db7f9.zip
Perl solution week 81/part 1.
Diffstat (limited to 'challenge-081')
-rw-r--r--challenge-081/abigail/input-1-12
-rw-r--r--challenge-081/abigail/input-1-22
-rw-r--r--challenge-081/abigail/output-1-1.exp2
-rw-r--r--challenge-081/abigail/output-1-2.exp1
-rw-r--r--challenge-081/abigail/perl/ch-1.pl46
5 files changed, 53 insertions, 0 deletions
diff --git a/challenge-081/abigail/input-1-1 b/challenge-081/abigail/input-1-1
new file mode 100644
index 0000000000..bcb118a7c2
--- /dev/null
+++ b/challenge-081/abigail/input-1-1
@@ -0,0 +1,2 @@
+abcdabcd
+abcdabcdabcdabcd
diff --git a/challenge-081/abigail/input-1-2 b/challenge-081/abigail/input-1-2
new file mode 100644
index 0000000000..0917019ba6
--- /dev/null
+++ b/challenge-081/abigail/input-1-2
@@ -0,0 +1,2 @@
+aaa
+aa
diff --git a/challenge-081/abigail/output-1-1.exp b/challenge-081/abigail/output-1-1.exp
new file mode 100644
index 0000000000..ddf5cedb9f
--- /dev/null
+++ b/challenge-081/abigail/output-1-1.exp
@@ -0,0 +1,2 @@
+abcdabcd
+abcd
diff --git a/challenge-081/abigail/output-1-2.exp b/challenge-081/abigail/output-1-2.exp
new file mode 100644
index 0000000000..7898192261
--- /dev/null
+++ b/challenge-081/abigail/output-1-2.exp
@@ -0,0 +1 @@
+a
diff --git a/challenge-081/abigail/perl/ch-1.pl b/challenge-081/abigail/perl/ch-1.pl
new file mode 100644
index 0000000000..39e62fc635
--- /dev/null
+++ b/challenge-081/abigail/perl/ch-1.pl
@@ -0,0 +1,46 @@
+#!/opt/perl/bin/perl
+
+#
+# You are given 2 strings, $A and $B.
+#
+# Write a script to find out common base strings in $A and $B.
+#
+# A substring of a string $S is called base string if repeated
+# concatenation of the substring results in the string.
+#
+
+use 5.032;
+
+use strict;
+use warnings;
+no warnings 'syntax';
+
+use experimental 'signatures';
+use experimental 'lexical_subs';
+
+chomp (my $A = <>);
+chomp (my $B = <>);
+
+#
+# Sort the strings by lenght, so $A isn't longer than $B.
+#
+($A, $B) = ($B, $A) if length $B < length $A;
+
+#
+# Find a substring which cannot be part of either string,
+# nor of its concatenation.
+#
+my $sep = "\x00" x (1 + length ($A) + length ($B));
+
+#
+# Now, use a regular expression to find common base strings.
+#
+$_ = "$A$sep$B";
+/^ (.+) \1* # Find base strings of $A
+ $sep # Match the separator
+ \1+ $ # Must be base string for $B
+ (?{say $1}) # Print it
+ (*FAIL) # Backtrack so we can try other base strings.
+/x;
+
+__END__