aboutsummaryrefslogtreecommitdiff
path: root/challenge-081
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-10-11 04:43:58 +0100
committerGitHub <noreply@github.com>2020-10-11 04:43:58 +0100
commit2222d42f6654a6e59c2dff233a924dabe3c583f2 (patch)
treee2db1daaee886ad763beb6a2f255dfc147c1c1d8 /challenge-081
parent1eb42f282637a8fcddb97ceb1df1818b92513626 (diff)
parent608a79ccac777fc88bf78912acecb88418970bb4 (diff)
downloadperlweeklychallenge-club-2222d42f6654a6e59c2dff233a924dabe3c583f2.tar.gz
perlweeklychallenge-club-2222d42f6654a6e59c2dff233a924dabe3c583f2.tar.bz2
perlweeklychallenge-club-2222d42f6654a6e59c2dff233a924dabe3c583f2.zip
Merge pull request #2484 from waltman/branch-for-challenge-081
Branch for challenge 081
Diffstat (limited to 'challenge-081')
-rw-r--r--challenge-081/walt-mankowski/blog.txt1
-rw-r--r--challenge-081/walt-mankowski/perl/ch-1.pl29
-rw-r--r--challenge-081/walt-mankowski/perl/ch-2.pl44
-rw-r--r--challenge-081/walt-mankowski/perl/input.txt15
-rw-r--r--challenge-081/walt-mankowski/python/.python-version2
-rw-r--r--challenge-081/walt-mankowski/python/ch-1.py13
-rw-r--r--challenge-081/walt-mankowski/python/ch-2.py27
7 files changed, 130 insertions, 1 deletions
diff --git a/challenge-081/walt-mankowski/blog.txt b/challenge-081/walt-mankowski/blog.txt
new file mode 100644
index 0000000000..c57c79e7ed
--- /dev/null
+++ b/challenge-081/walt-mankowski/blog.txt
@@ -0,0 +1 @@
+http://www.mawode.com/blog/blog/2020/10/10/perl-weekly-challenge-81/
diff --git a/challenge-081/walt-mankowski/perl/ch-1.pl b/challenge-081/walt-mankowski/perl/ch-1.pl
new file mode 100644
index 0000000000..130a121916
--- /dev/null
+++ b/challenge-081/walt-mankowski/perl/ch-1.pl
@@ -0,0 +1,29 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use feature qw(:5.32);
+use experimental qw(signatures);
+use List::Util qw(min);
+
+# 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.
+
+my ($A, $B) = @ARGV;
+my $max_base_len = min(length($A), length($B));
+
+for my $i (1 .. $max_base_len) {
+ my $prefix = substr($A, 0, $i);
+ if (is_base($prefix, $A) && is_base($prefix, $B)) {
+ say $prefix;
+ }
+}
+
+sub is_base($prefix, $s) {
+ my $cnt = length($s) / length($prefix);
+ return $prefix x $cnt eq $s;
+}
+
diff --git a/challenge-081/walt-mankowski/perl/ch-2.pl b/challenge-081/walt-mankowski/perl/ch-2.pl
new file mode 100644
index 0000000000..68e2900945
--- /dev/null
+++ b/challenge-081/walt-mankowski/perl/ch-2.pl
@@ -0,0 +1,44 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use feature qw(:5.32);
+use experimental qw(signatures);
+
+# 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.
+#
+# For the sake of this task, please ignore the following in the input file:
+#
+# . " ( ) , 's --
+
+my %h;
+
+while (<>) {
+ chomp;
+ s/--/ /;
+ my @tok = split / /;
+ for my $tok (@tok) {
+ $tok =~ s/[."(),]//g;
+ $tok =~ s/'s$//;
+ $h{$tok}++;
+ }
+}
+
+my $last = 0;
+for my $k (sort { $h{$a} <=> $h{$b} || $a cmp $b } keys %h) {
+ if ($h{$k} != $last) {
+ print "\n" unless $last == 0;
+ print $h{$k};
+ $last = $h{$k};
+ }
+ print " $k";
+}
+
+print "\n";
+
diff --git a/challenge-081/walt-mankowski/perl/input.txt b/challenge-081/walt-mankowski/perl/input.txt
new file mode 100644
index 0000000000..649cf57770
--- /dev/null
+++ b/challenge-081/walt-mankowski/perl/input.txt
@@ -0,0 +1,15 @@
+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/walt-mankowski/python/.python-version b/challenge-081/walt-mankowski/python/.python-version
index 0cbfaed0d9..a5c4c76339 100644
--- a/challenge-081/walt-mankowski/python/.python-version
+++ b/challenge-081/walt-mankowski/python/.python-version
@@ -1 +1 @@
-3.8.5
+3.9.0
diff --git a/challenge-081/walt-mankowski/python/ch-1.py b/challenge-081/walt-mankowski/python/ch-1.py
new file mode 100644
index 0000000000..a268b371f5
--- /dev/null
+++ b/challenge-081/walt-mankowski/python/ch-1.py
@@ -0,0 +1,13 @@
+from sys import argv
+
+def is_base(prefix, s):
+ cnt = int(len(s) / len(prefix))
+ return prefix * cnt == s
+
+a, b = argv[1:]
+max_base_len = min(len(a), len(b))
+
+for i in range(1, max_base_len+1):
+ prefix = a[0:i]
+ if is_base(prefix, a) and is_base(prefix, b):
+ print(prefix)
diff --git a/challenge-081/walt-mankowski/python/ch-2.py b/challenge-081/walt-mankowski/python/ch-2.py
new file mode 100644
index 0000000000..b348f03819
--- /dev/null
+++ b/challenge-081/walt-mankowski/python/ch-2.py
@@ -0,0 +1,27 @@
+from sys import argv
+from collections import defaultdict
+import re
+
+d = defaultdict(int)
+fname = argv[1]
+with open(fname) as f:
+ for line in f:
+ line = line.rstrip()
+ line = re.sub('--', ' ', line)
+ toks = line.split()
+ for tok in toks:
+ tok = re.sub('[."(),]', '', tok);
+ tok = re.sub("'s$", '', tok)
+ d[tok] += 1
+
+last = 0
+for k in sorted(d, key=lambda k:(d[k], k)):
+ if d[k] != last:
+ if last != 0:
+ print()
+ print(d[k], end='')
+ last = d[k]
+ print(f" {k}", end = '')
+
+print()
+