aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2020-10-07 21:18:53 +0200
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2020-10-07 21:18:53 +0200
commita5b8062f2f92319cae1408e2bc41f02791ba27d1 (patch)
tree64fb1de4ce97514c5025e269c1c9f81d82066590
parent45bc85340b1d0295a7ea66b20623f3f1fd20f1ad (diff)
parent90730d8ef9b174c6d736b63be07ebfd17e3632bd (diff)
downloadperlweeklychallenge-club-a5b8062f2f92319cae1408e2bc41f02791ba27d1.tar.gz
perlweeklychallenge-club-a5b8062f2f92319cae1408e2bc41f02791ba27d1.tar.bz2
perlweeklychallenge-club-a5b8062f2f92319cae1408e2bc41f02791ba27d1.zip
Solutions to challenge 081
-rwxr-xr-xchallenge-081/jo-37/perl/ch-1.pl31
-rwxr-xr-xchallenge-081/jo-37/perl/ch-2.pl29
2 files changed, 60 insertions, 0 deletions
diff --git a/challenge-081/jo-37/perl/ch-1.pl b/challenge-081/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..61941ab415
--- /dev/null
+++ b/challenge-081/jo-37/perl/ch-1.pl
@@ -0,0 +1,31 @@
+#!/usr/bin/perl
+
+use Test2::V0;
+
+# Find common base strings in two given strings.
+sub cbs {
+
+ # Combine both strings by joining them with a newline.
+ # The strings must not contain newlines.
+ local $_ = shift . "\n" . shift;
+
+ # Collect all common base strings.
+ # Note: "dot" does not match a newline here.
+ my @base;
+ m{
+ ^ (.+?) \1*+ \n \1++ \z # capture base string for both
+ (?{push @base, $1}) # collect captured base string
+ (*FAIL) # force backtracking
+ }x;
+
+ @base;
+}
+
+is [cbs("abcdabcd", "abcdabcdabcdabcd")], ["abcd", "abcdabcd"],
+ "first example";
+
+is [cbs("aaa", "aa")], ["a"], "second example";
+
+is [cbs("abcabc", "abcdabcdabcd")], [], "no common base strings";
+
+done_testing;
diff --git a/challenge-081/jo-37/perl/ch-2.pl b/challenge-081/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..50f80268d7
--- /dev/null
+++ b/challenge-081/jo-37/perl/ch-2.pl
@@ -0,0 +1,29 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use List::Util qw(uniqnum pairgrep pairkeys);
+
+# Use the input data provided by the DATA filehandle if no file name
+# is given.
+*ARGV = *DATA{IO} unless @ARGV;
+
+# Override some defaults: line endings, field separator and slurp mode
+local ($\, $,, $/) = ("\n\n", ' ');
+
+# Build a hash of word/frequency pairs from input data. Incorporate
+# specified exceptions into the split expression.
+my %freq;
+$freq{$_}++ foreach split qr{[\."),]*\s+[("]*|--|'s\s+}, <>;
+
+# For each frequency, extract the corresponding words from %freq, sort
+# and print them.
+# Note: "pairkeys" needs to be protected from being interpreted as a
+# comparator sub name by sort.
+print $_, sort +(pairkeys pairgrep {$b == $_} %freq)
+ foreach uniqnum sort {$a <=> $b} values %freq;
+
+__DATA__
+West Side Story
+
+The award-winning adaptation of the classic romantic tragedy "Romeo and Juliet". The feuding families become two warring New York City gangs, the white Jets led by Riff and the Latino Sharks, led by Bernardo. Their hatred escalates to a point where neither can coexist with any form of understanding. But when Riff's best friend (and former Jet) Tony and Bernardo's younger sister Maria meet at a dance, no one can do anything to stop their love. Maria and Tony begin meeting in secret, planning to run away. Then the Sharks and Jets plan a rumble under the highway--whoever wins gains control of the streets. Maria sends Tony to stop it, hoping it can end the violence. It goes terribly wrong, and before the lovers know what's happened, tragedy strikes and doesn't stop until the climactic and heartbreaking ending.