aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-10-07 09:39:04 +0100
committerGitHub <noreply@github.com>2020-10-07 09:39:04 +0100
commita45fdeecfa5f4c15459608c43215b2d320c336ef (patch)
tree654dd8a8a8ed79895c06f83c9cafa635ed19df46
parent904f1b93bd123ca444804fd8b22d6d7357de7238 (diff)
parent2a164009eaff581dc487a5fc955c7421a8ba7a31 (diff)
downloadperlweeklychallenge-club-a45fdeecfa5f4c15459608c43215b2d320c336ef.tar.gz
perlweeklychallenge-club-a45fdeecfa5f4c15459608c43215b2d320c336ef.tar.bz2
perlweeklychallenge-club-a45fdeecfa5f4c15459608c43215b2d320c336ef.zip
Merge pull request #2469 from jacoby/master
Challenge 81
-rwxr-xr-xchallenge-081/dave-jacoby/perl/ch-1.pl54
-rwxr-xr-xchallenge-081/dave-jacoby/perl/ch-2.pl34
-rw-r--r--challenge-081/dave-jacoby/perl/input3
3 files changed, 91 insertions, 0 deletions
diff --git a/challenge-081/dave-jacoby/perl/ch-1.pl b/challenge-081/dave-jacoby/perl/ch-1.pl
new file mode 100755
index 0000000000..44dccb6c91
--- /dev/null
+++ b/challenge-081/dave-jacoby/perl/ch-1.pl
@@ -0,0 +1,54 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature qw{ say signatures state };
+no warnings qw{ experimental };
+
+my $comment = <<'DOC';
+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.
+
+Example 1:
+Input:
+ $A = "abcdabcd"
+ $B = "abcdabcdabcdabcd"
+
+Output:
+ ("abcd", "abcdabcd")
+Example 2:
+Input:
+ $A = "aaa"
+ $B = "aa"
+
+Output:
+ ("a")
+DOC
+
+common_base( "abcdabcd", "abcdabcdabcdabcd" );
+common_base( "aaa", "aa" );
+
+sub common_base ( @words ) {
+ my ( $aa, $bb ) = sort { length $a <=> length $b } @words;
+ my %output;
+
+ for my $i ( 0 .. length $aa ) {
+ for my $j ( 1 .. ( length $aa ) - $i ) {
+ my $aaa = $aa;
+ my $bbb = $bb;
+ my $sub = substr( $aa, $i, $j );
+ my $pad = ' ' x $i;
+ $aaa =~ s/$sub//gmix;
+ $bbb =~ s/$sub//gmix;
+ next unless $aaa eq '' && $bbb eq '';
+ # say qq{ $pad$sub\t$aaa\t$bbb};
+ $output{$sub} = 1 if $aaa eq '' && $bbb eq '';
+ }
+
+ }
+ say join ', ', keys %output;
+}
+
diff --git a/challenge-081/dave-jacoby/perl/ch-2.pl b/challenge-081/dave-jacoby/perl/ch-2.pl
new file mode 100755
index 0000000000..bca7719a96
--- /dev/null
+++ b/challenge-081/dave-jacoby/perl/ch-2.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature qw{ say signatures state };
+no warnings qw{ experimental };
+
+use List::Util qw{max};
+
+my $file = $ARGV[0];
+$file = defined $file && -f $file ? $file : 'input';
+
+frequency_sort($file);
+
+sub frequency_sort( $file ) {
+ if ( -f $file && open my $fh, '<', $file ) {
+ my $corpus = join '', <$fh>;
+ $corpus =~ s/[,\.\(\)\"]/ /g;
+ $corpus =~ s/\'s/ /g;
+ $corpus =~ s/--/ /g;
+ my %words;
+ for my $word ( split /\s+/, $corpus ) {
+ $words{$word}++;
+ }
+ my $max = max values %words;
+
+ for my $c ( 1 .. $max ) {
+ my @words = sort grep { $words{$_} == $c } keys %words;
+ say join ' ', $c, @words, "\n" if scalar @words;
+ }
+
+ close $fh;
+ }
+}
diff --git a/challenge-081/dave-jacoby/perl/input b/challenge-081/dave-jacoby/perl/input
new file mode 100644
index 0000000000..37001629ad
--- /dev/null
+++ b/challenge-081/dave-jacoby/perl/input
@@ -0,0 +1,3 @@
+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.