From 4b6202a6507cb1e1e3854fa643fc25714dc930a2 Mon Sep 17 00:00:00 2001 From: Walt Mankowski Date: Tue, 6 Oct 2020 19:34:55 -0400 Subject: perl solution to challenge 81 task 1 --- challenge-081/walt-mankowski/perl/ch-1.pl | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 challenge-081/walt-mankowski/perl/ch-1.pl 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..9a91c9d8db --- /dev/null +++ b/challenge-081/walt-mankowski/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!/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) { + if (is_base(substr($A, 0, $i), $A) && is_base(substr($B, 0, $i), $B)) { + say substr($A, 0, $i); + } +} + +sub is_base($prefix, $s) { + my $cnt = length($s) / length($prefix); + return $prefix x $cnt eq $s; +} + -- cgit From 6c58dba68670dfc7c703bb4a39455196eb3dbd97 Mon Sep 17 00:00:00 2001 From: Walt Mankowski Date: Tue, 6 Oct 2020 19:40:07 -0400 Subject: cleaned up the code a bit --- challenge-081/walt-mankowski/perl/ch-1.pl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/challenge-081/walt-mankowski/perl/ch-1.pl b/challenge-081/walt-mankowski/perl/ch-1.pl index 9a91c9d8db..130a121916 100644 --- a/challenge-081/walt-mankowski/perl/ch-1.pl +++ b/challenge-081/walt-mankowski/perl/ch-1.pl @@ -16,8 +16,9 @@ my ($A, $B) = @ARGV; my $max_base_len = min(length($A), length($B)); for my $i (1 .. $max_base_len) { - if (is_base(substr($A, 0, $i), $A) && is_base(substr($B, 0, $i), $B)) { - say substr($A, 0, $i); + my $prefix = substr($A, 0, $i); + if (is_base($prefix, $A) && is_base($prefix, $B)) { + say $prefix; } } -- cgit From fb9bee1b7c989e55ca10b43bc2553700f558aabd Mon Sep 17 00:00:00 2001 From: Walt Mankowski Date: Tue, 6 Oct 2020 19:57:20 -0400 Subject: perl code for challenge 81 task 2 --- challenge-081/walt-mankowski/perl/ch-2.pl | 44 +++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 challenge-081/walt-mankowski/perl/ch-2.pl 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"; + -- cgit From 7af4d2a5a2c40a060f78972bd974af2ee836c631 Mon Sep 17 00:00:00 2001 From: Walt Mankowski Date: Tue, 6 Oct 2020 19:57:27 -0400 Subject: input file for challenge 81 task 2 --- challenge-081/walt-mankowski/perl/input.txt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 challenge-081/walt-mankowski/perl/input.txt 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. -- cgit From 293c72bd0be6f09dba088d564d92d6820357bb3e Mon Sep 17 00:00:00 2001 From: Walt Mankowski Date: Tue, 6 Oct 2020 21:08:46 -0400 Subject: python solutions to challenge 81 --- challenge-081/walt-mankowski/python/ch-1.py | 13 +++++++++++++ challenge-081/walt-mankowski/python/ch-2.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 challenge-081/walt-mankowski/python/ch-1.py create mode 100644 challenge-081/walt-mankowski/python/ch-2.py 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..2b6c400b45 --- /dev/null +++ b/challenge-081/walt-mankowski/python/ch-2.py @@ -0,0 +1,29 @@ +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) + if line == '': + continue + 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() + -- cgit From 132c71e855a2ba84207bed6ee1e531e6f66f9e6f Mon Sep 17 00:00:00 2001 From: Walt Mankowski Date: Sat, 10 Oct 2020 19:04:15 -0400 Subject: use split() instead of split('') --- challenge-081/walt-mankowski/python/ch-2.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/challenge-081/walt-mankowski/python/ch-2.py b/challenge-081/walt-mankowski/python/ch-2.py index 2b6c400b45..b348f03819 100644 --- a/challenge-081/walt-mankowski/python/ch-2.py +++ b/challenge-081/walt-mankowski/python/ch-2.py @@ -8,9 +8,7 @@ with open(fname) as f: for line in f: line = line.rstrip() line = re.sub('--', ' ', line) - if line == '': - continue - toks = line.split(' ') + toks = line.split() for tok in toks: tok = re.sub('[."(),]', '', tok); tok = re.sub("'s$", '', tok) -- cgit From ff5306fe3afe46444c7bd08d15a7760948037705 Mon Sep 17 00:00:00 2001 From: Walt Mankowski Date: Sat, 10 Oct 2020 20:33:32 -0400 Subject: updated to python 3.9.0 --- challenge-081/walt-mankowski/python/.python-version | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 -- cgit From 608a79ccac777fc88bf78912acecb88418970bb4 Mon Sep 17 00:00:00 2001 From: Walt Mankowski Date: Sat, 10 Oct 2020 20:33:55 -0400 Subject: added link to this week's blog --- challenge-081/walt-mankowski/blog.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-081/walt-mankowski/blog.txt 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/ -- cgit