diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-10-09 23:28:55 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-10-09 23:28:55 +0100 |
| commit | e9750be9af0e7868c6bc54ae2b2aeafcead6bfec (patch) | |
| tree | a2179d806dfba177687a3b477e5ecb277b1face1 | |
| parent | de6bd836702f7165f9a03ba643c94b8a6f84f3f1 (diff) | |
| parent | 9b1d06e752e91bb03f909b4305e5c1a19d5f27a4 (diff) | |
| download | perlweeklychallenge-club-e9750be9af0e7868c6bc54ae2b2aeafcead6bfec.tar.gz perlweeklychallenge-club-e9750be9af0e7868c6bc54ae2b2aeafcead6bfec.tar.bz2 perlweeklychallenge-club-e9750be9af0e7868c6bc54ae2b2aeafcead6bfec.zip | |
Merge pull request #2481 from LubosKolouch/master
Python solution Task 1 LK
| -rw-r--r-- | challenge-081/lubos-kolouch/perl/ch-1.pl | 8 | ||||
| -rw-r--r-- | challenge-081/lubos-kolouch/perl/ch-2.pl | 5 | ||||
| -rw-r--r-- | challenge-081/lubos-kolouch/python/ch-1.py | 36 | ||||
| -rw-r--r-- | challenge-081/lubos-kolouch/python/ch-2.py | 55 |
4 files changed, 95 insertions, 9 deletions
diff --git a/challenge-081/lubos-kolouch/perl/ch-1.pl b/challenge-081/lubos-kolouch/perl/ch-1.pl index 9890053c01..fe05fd99fd 100644 --- a/challenge-081/lubos-kolouch/perl/ch-1.pl +++ b/challenge-081/lubos-kolouch/perl/ch-1.pl @@ -9,15 +9,9 @@ # https://perlweeklychallenge.org/blog/perl-weekly-challenge-081/ # Task 1 - Common base string # -# OPTIONS: --- -# REQUIREMENTS: --- -# BUGS: --- -# NOTES: --- -# AUTHOR: YOUR NAME (), -# ORGANIZATION: +# AUTHOR: Lubos Kolouch # VERSION: 1.0 # CREATED: 10/09/2020 10:43:10 AM -# REVISION: --- #=============================================================================== use strict; diff --git a/challenge-081/lubos-kolouch/perl/ch-2.pl b/challenge-081/lubos-kolouch/perl/ch-2.pl index c8d93d17f8..0a6e16b0ef 100644 --- a/challenge-081/lubos-kolouch/perl/ch-2.pl +++ b/challenge-081/lubos-kolouch/perl/ch-2.pl @@ -21,8 +21,9 @@ sub frequency_sort { my $arg = shift; # sanitize the input as per the challenge - $arg =~ s/[."\(\)--]/ /g; + $arg =~ s/[."\(\),]/ /g; $arg =~ s/(?:'s)/ /g; + $arg =~ s/(?:--)/ /g; my %count; # I'm sure it can be done in one-liner with map, but I took the longer approach @@ -51,6 +52,6 @@ sub frequency_sort { use Test::More; -is_deeply(frequency_sort("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." ),['1 But City It Jet Juliet Latino New Romeo Side Story Their Then West York adaptation any anything at award 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 winning 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']); +is_deeply(frequency_sort("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." ),['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']); done_testing; diff --git a/challenge-081/lubos-kolouch/python/ch-1.py b/challenge-081/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..e3b1b080ce --- /dev/null +++ b/challenge-081/lubos-kolouch/python/ch-1.py @@ -0,0 +1,36 @@ +#!/bin/env python +""" =============================================================================== +# +# FILE: ch-1.py +# +# USAGE: ./ch-1.py +# +# DESCRIPTION: Perl Weekly Challenge 081 +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-081/ +# Task 1 - Common base string +# +# AUTHOR: Lubos Kolouch +# CREATED: 10/09/2020 10:43:10 AM +#=============================================================================== +""" +import re + + +def get_common_strings(str1, str2): + """ Find and return the common strings """ + + result = [] + res1 = re.match(r'(.*)\1+', str1) + + result.append(res1.group(1)) + + res2 = re.match(r'(.*)\1+', str2) + if res1.group(1) != res2.group(1): + result.append(res2.group(1)) + + return result + + +assert get_common_strings('abcdabcd', 'abcdabcdabcdabcd') == ['abcd','abcdabcd'] +assert get_common_strings('aaa', 'aa') == ['a'] +assert get_common_strings('abc', 'aa') == ['','a'] diff --git a/challenge-081/lubos-kolouch/python/ch-2.py b/challenge-081/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..8160fc3593 --- /dev/null +++ b/challenge-081/lubos-kolouch/python/ch-2.py @@ -0,0 +1,55 @@ +#!/bin/env python +""" +#=============================================================================== +# +# FILE: ch-1.py +# +# USAGE: ./ch-1.py +# +# DESCRIPTION: Perl Weekly Challenge 081 +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-081/ +# Task 2 - Frequency Sort +# +# AUTHOR: Lubos Kolouch +# CREATED: 10/09/2020 10:43:10 AM +#=============================================================================== +""" +import re + + +def frequency_sort(arg): + """ Do the frequency sorting """ + + # sanitize the input as per the challenge + arg = re.sub(r'[."\(\),]', ' ', arg) + arg = re.sub(r'(?:\'s)', ' ', arg) + arg = re.sub(r'(?:--)', ' ', arg) + + count = {} + # I'm sure it can be done in one-liner with map, but I took the longer approach + # First split the array and create hash of counts of each word + for item in re.split(r'\s+', arg): + if not item: + continue + try: + count[item] += 1 + except KeyError: + count[item] = 1 + + rev_count = {} + # create reverse hash with counts as key and the words as array + for key in count.keys(): + try: + rev_count[count[key]].append(key) + except KeyError: + rev_count[count[key]] = [key] + + result = [] + for key in rev_count.keys(): + # sort the arrays and put them to the result output + result.append(str(key)+' '+' '.join(sorted(rev_count[key]))) + + return sorted(result) + + +assert frequency_sort("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.") == ["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'] |
