aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-12-27 09:20:06 +0000
committerGitHub <noreply@github.com>2020-12-27 09:20:06 +0000
commit34eb5abf2986c82010670cc8a9b24ccc3a3fdfe4 (patch)
tree1c54c9d574f2778e130f69ee885408174a136ccd
parent7a018bb8fe34fe45405983f41a7abc3885518117 (diff)
parent370a9b2744f42c115edd878d5f4ff03b9a58930e (diff)
downloadperlweeklychallenge-club-34eb5abf2986c82010670cc8a9b24ccc3a3fdfe4.tar.gz
perlweeklychallenge-club-34eb5abf2986c82010670cc8a9b24ccc3a3fdfe4.tar.bz2
perlweeklychallenge-club-34eb5abf2986c82010670cc8a9b24ccc3a3fdfe4.zip
Merge pull request #3072 from pauloscustodio/081-perl
Add Perl solution to challenge 081
-rw-r--r--challenge-081/paulo-custodio/README1
-rw-r--r--challenge-081/paulo-custodio/input3
-rw-r--r--challenge-081/paulo-custodio/perl/ch-1.pl56
-rw-r--r--challenge-081/paulo-custodio/perl/ch-2.pl55
-rw-r--r--challenge-081/paulo-custodio/test.pl37
5 files changed, 152 insertions, 0 deletions
diff --git a/challenge-081/paulo-custodio/README b/challenge-081/paulo-custodio/README
new file mode 100644
index 0000000000..87dc0b2fbd
--- /dev/null
+++ b/challenge-081/paulo-custodio/README
@@ -0,0 +1 @@
+Solution by Paulo Custodio
diff --git a/challenge-081/paulo-custodio/input b/challenge-081/paulo-custodio/input
new file mode 100644
index 0000000000..37001629ad
--- /dev/null
+++ b/challenge-081/paulo-custodio/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.
diff --git a/challenge-081/paulo-custodio/perl/ch-1.pl b/challenge-081/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..749c460c91
--- /dev/null
+++ b/challenge-081/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,56 @@
+#!/usr/bin/env perl
+
+# Challenge 081
+#
+# TASK #1 › Common Base String
+# Submitted by: Mohammad S Anwar
+# 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")
+
+use strict;
+use warnings;
+use 5.030;
+use Array::Utils 'intersect';
+
+@ARGV==2 or die "Usage: ch-1.pl A B\n";
+my($A, $B) = @ARGV;
+my @base_a = base_strings($A);
+my @base_b = base_strings($B);
+my @result = intersect(@base_a, @base_b);
+say "(", join(", ", map {'"'.$_.'"'} @result), ")";
+
+
+sub base_strings {
+ my($str) = @_;
+ my @base;
+
+ my $len = length($str);
+ for my $i (1 .. $len) {
+ if ($len % $i == 0) { # $i is a divisor of total length
+ my $base = substr($str, 0, $i);
+ if (($base x ($len / $i)) eq $str) {
+ push @base, $base;
+ }
+ }
+ }
+ return @base;
+}
diff --git a/challenge-081/paulo-custodio/perl/ch-2.pl b/challenge-081/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..bc73f78b20
--- /dev/null
+++ b/challenge-081/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,55 @@
+#!/usr/bin/env perl
+
+# Challenge 081
+#
+# TASK #2 › Frequency Sort
+# Submitted by: Mohammad S Anwar
+# You are given file named input.
+#
+# Write a script to find the frequency of all the words.
+#
+# It should print the result as first column of each line should be the frequency of the the word followed by all the words of that frequency arranged in lexicographical order. Also sort the words in the ascending order of frequency.
+#
+# INPUT file
+# 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.
+# NOTE
+# For the sake of this task, please ignore the following in the input file:
+#
+# . " ( ) , 's --
+# OUTPUT
+# 1 But City It Jet Juliet Latino New Romeo Side Story Their Then West York adaptation any anything at award-winning away become before begin best classic climactic coexist control dance do doesn't end ending escalates families feuding form former friend gains gangs goes happened hatred heartbreaking highway hoping in know love lovers meet meeting neither no one plan planning point romantic rumble run secret sends sister streets strikes terribly their two under understanding until violence warring what when where white whoever wins with wrong younger
+#
+# 2 Bernardo Jets Riff Sharks The by it led tragedy
+#
+# 3 Maria Tony a can of stop
+#
+# 4 to
+#
+# 9 and the
+
+use strict;
+use warnings;
+use 5.030;
+
+# compute word frequency
+my %words;
+while (<>) {
+ s/[."(),]/ /g; s/--/ /g; s/'s/ /g;
+ $words{$_}++ for split ' ', $_;
+}
+
+# order by frequency
+my @freq;
+while (my($word, $n) = each %words) {
+ $freq[$n] ||= [];
+ push @{$freq[$n]}, $word;
+}
+
+# output
+for (1 .. $#freq) {
+ if ($freq[$_]) {
+ say join(" ", $_, sort(@{$freq[$_]})),"\n";
+ }
+}
diff --git a/challenge-081/paulo-custodio/test.pl b/challenge-081/paulo-custodio/test.pl
new file mode 100644
index 0000000000..0cc4257aad
--- /dev/null
+++ b/challenge-081/paulo-custodio/test.pl
@@ -0,0 +1,37 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use Test::More;
+use 5.030;
+
+is capture("perl/ch-1.pl abcdabcd abcdabcdabcdabcd"), <<END;
+("abcd", "abcdabcd")
+END
+is capture("perl/ch-1.pl aaa aa"), <<END;
+("a")
+END
+
+
+is capture("perl/ch-2.pl input"), <<END;
+1 But City It Jet Juliet Latino New Romeo Side Story Their Then West York adaptation any anything at award-winning away become before begin best classic climactic coexist control dance do doesn't end ending escalates families feuding form former friend gains gangs goes happened hatred heartbreaking highway hoping in know love lovers meet meeting neither no one plan planning point romantic rumble run secret sends sister streets strikes terribly their two under understanding until violence warring what when where white whoever wins with wrong younger
+
+2 Bernardo Jets Riff Sharks The by it led tragedy
+
+3 Maria Tony a can of stop
+
+4 to
+
+9 and the
+
+END
+
+
+done_testing;
+
+sub capture {
+ my($cmd) = @_;
+ my $out = `$cmd`;
+ $out =~ s/\r//g;
+ return $out;
+}