From 324bcc1de2a7f771da7b70cdaef60403936adc4a Mon Sep 17 00:00:00 2001 From: Abigail Date: Sun, 11 Oct 2020 14:06:07 +0200 Subject: Perl solution week 81/part 1. --- challenge-081/abigail/input-1-1 | 2 ++ challenge-081/abigail/input-1-2 | 2 ++ challenge-081/abigail/output-1-1.exp | 2 ++ challenge-081/abigail/output-1-2.exp | 1 + challenge-081/abigail/perl/ch-1.pl | 37 ++++++++++++++++++++++++++++++++++++ 5 files changed, 44 insertions(+) create mode 100644 challenge-081/abigail/input-1-1 create mode 100644 challenge-081/abigail/input-1-2 create mode 100644 challenge-081/abigail/output-1-1.exp create mode 100644 challenge-081/abigail/output-1-2.exp create mode 100644 challenge-081/abigail/perl/ch-1.pl 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..840fc865f9 --- /dev/null +++ b/challenge-081/abigail/perl/ch-1.pl @@ -0,0 +1,37 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +chomp (my $str1 = <>); +chomp (my $str2 = <>); + +# +# Sort the strings by lenght, so $str1 isn't longer than $str2. +# +($str1, $str2) = ($str2, $str1) if length $str2 < length $str1; + +# +# Find a substring which cannot be part of either string, +# nor of its concatenation. +# +my $sep = "\x00" x (1 + length ($str1) + length ($str2)); + +# +# Now, use a regular expression to find common base strings. +# +$_ = "$str1$sep$str2"; +/^ (.+) \1* # Find base strings of $str1 + $sep # Match the separator + \1+ $ # Must be base string for $str2 + (?{say $1}) # Print it + (*FAIL) # Backtrack so we can try other base strings. +/x; + +__END__ -- cgit