diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-11-12 14:40:50 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-11-12 14:40:50 +0000 |
| commit | e2ee87081513efe219682ada6b7fb01de87c0335 (patch) | |
| tree | 315776fd11e29626b5aa6a90e5de5933984932be | |
| parent | 7eedbc877a5e2e6750bd0c302f5880d05a3505a5 (diff) | |
| download | perlweeklychallenge-club-e2ee87081513efe219682ada6b7fb01de87c0335.tar.gz perlweeklychallenge-club-e2ee87081513efe219682ada6b7fb01de87c0335.tar.bz2 perlweeklychallenge-club-e2ee87081513efe219682ada6b7fb01de87c0335.zip | |
Add Python solution to challenge 81
| -rw-r--r-- | challenge-081/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-081/paulo-custodio/perl/ch-1.pl | 4 | ||||
| -rw-r--r-- | challenge-081/paulo-custodio/perl/ch-2.pl | 4 | ||||
| -rw-r--r-- | challenge-081/paulo-custodio/python/ch-1.py | 43 | ||||
| -rw-r--r-- | challenge-081/paulo-custodio/python/ch-2.py | 67 | ||||
| -rw-r--r-- | challenge-081/paulo-custodio/t/test-1.yaml | 10 | ||||
| -rw-r--r-- | challenge-081/paulo-custodio/t/test-2.yaml | 15 | ||||
| -rw-r--r-- | challenge-081/paulo-custodio/test.pl | 35 |
8 files changed, 141 insertions, 39 deletions
diff --git a/challenge-081/paulo-custodio/Makefile b/challenge-081/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-081/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-081/paulo-custodio/perl/ch-1.pl b/challenge-081/paulo-custodio/perl/ch-1.pl index 761c27d74d..317e2f90ee 100644 --- a/challenge-081/paulo-custodio/perl/ch-1.pl +++ b/challenge-081/paulo-custodio/perl/ch-1.pl @@ -1,8 +1,8 @@ -#!/usr/bin/perl +#!/usr/bin/env perl # Challenge 081 # -# TASK #1 › Common Base String +# TASK #1 > Common Base String # Submitted by: Mohammad S Anwar # You are given 2 strings, $A and $B. # diff --git a/challenge-081/paulo-custodio/perl/ch-2.pl b/challenge-081/paulo-custodio/perl/ch-2.pl index 11d77685f7..d1184a5dff 100644 --- a/challenge-081/paulo-custodio/perl/ch-2.pl +++ b/challenge-081/paulo-custodio/perl/ch-2.pl @@ -1,8 +1,8 @@ -#!/usr/bin/perl +#!/usr/bin/env perl # Challenge 081 # -# TASK #2 › Frequency Sort +# TASK #2 > Frequency Sort # Submitted by: Mohammad S Anwar # You are given file named input. # diff --git a/challenge-081/paulo-custodio/python/ch-1.py b/challenge-081/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..be33af8625 --- /dev/null +++ b/challenge-081/paulo-custodio/python/ch-1.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 + +# 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") + +import sys + +def base_strings(s): + bases = [] + for i in range(1, len(s)+1): + if len(s)%i==0: + base = s[:i] + if base*int(len(s)/i) == s: + bases.append(base) + return bases + +base_a = set(base_strings(sys.argv[1])) +base_b = set(base_strings(sys.argv[2])) +result = sorted(list(base_a.intersection(base_b))) +print("(" + ", ".join(['"'+x+'"' for x in result]) + ")") diff --git a/challenge-081/paulo-custodio/python/ch-2.py b/challenge-081/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..930f64f8bc --- /dev/null +++ b/challenge-081/paulo-custodio/python/ch-2.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python3 + +# 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 + +import fileinput +import re + +def read_input(): + lines = [] + for line in fileinput.input(): + lines.append(line) + return lines + +def get_word_freq(lines): + words = {} + for line in lines: + line = re.sub(r"""[."(),]|--|'s""", " ", line) + for word in line.split(): + if word in words: + words[word] += 1 + else: + words[word] = 1 + return words + +def order_by_freq(words): + freqs = [] + for word, freq in words.items(): + while len(freqs) <= freq: + freqs.append([]) + freqs[freq].append(word) + freqs[freq].sort() + return freqs + +def print_freqs(freqs): + for freq in range(1, len(freqs)): + if freqs[freq]: + print(freq, " ".join([x for x in freqs[freq]])) + print() + +print_freqs(order_by_freq(get_word_freq(read_input()))) diff --git a/challenge-081/paulo-custodio/t/test-1.yaml b/challenge-081/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..0a9859e8e7 --- /dev/null +++ b/challenge-081/paulo-custodio/t/test-1.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: abcdabcd abcdabcdabcdabcd + input: + output: ("abcd", "abcdabcd") +- setup: + cleanup: + args: aaa aa + input: + output: ("a") diff --git a/challenge-081/paulo-custodio/t/test-2.yaml b/challenge-081/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..2bd0de5422 --- /dev/null +++ b/challenge-081/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: input + input: + 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 + | diff --git a/challenge-081/paulo-custodio/test.pl b/challenge-081/paulo-custodio/test.pl deleted file mode 100644 index ff97302435..0000000000 --- a/challenge-081/paulo-custodio/test.pl +++ /dev/null @@ -1,35 +0,0 @@ -#!/usr/bin/perl - -use Modern::Perl; -use Test::More; - -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; -} |
