diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-01-10 06:44:03 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-01-10 06:44:03 +0000 |
| commit | a6823a41d7419c1ee4bc9d24d254e834c7451141 (patch) | |
| tree | 97b934cda0d5662deb88ca31d99438fc286cee97 /challenge-094 | |
| parent | 9ef0bba33de25ff6fcc15f5da5c18b3689b5716a (diff) | |
| parent | 964da3a5cbf41aa63379a48f3c64d6d18c98f847 (diff) | |
| download | perlweeklychallenge-club-a6823a41d7419c1ee4bc9d24d254e834c7451141.tar.gz perlweeklychallenge-club-a6823a41d7419c1ee4bc9d24d254e834c7451141.tar.bz2 perlweeklychallenge-club-a6823a41d7419c1ee4bc9d24d254e834c7451141.zip | |
Merge pull request #3191 from LubosKolouch/master
Python solutions
Diffstat (limited to 'challenge-094')
| -rw-r--r-- | challenge-094/lubos-kolouch/perl/ch-1.pl | 2 | ||||
| -rw-r--r-- | challenge-094/lubos-kolouch/perl/ch-2.pl | 22 | ||||
| -rw-r--r-- | challenge-094/lubos-kolouch/python/ch-1.py | 42 | ||||
| -rw-r--r-- | challenge-094/lubos-kolouch/python/ch-2.py | 106 |
4 files changed, 159 insertions, 13 deletions
diff --git a/challenge-094/lubos-kolouch/perl/ch-1.pl b/challenge-094/lubos-kolouch/perl/ch-1.pl index 30da78e34b..e9ac3abddd 100644 --- a/challenge-094/lubos-kolouch/perl/ch-1.pl +++ b/challenge-094/lubos-kolouch/perl/ch-1.pl @@ -17,8 +17,6 @@ use strict; use warnings; -use Data::Dumper; - sub get_anagrams { my $what = shift; diff --git a/challenge-094/lubos-kolouch/perl/ch-2.pl b/challenge-094/lubos-kolouch/perl/ch-2.pl index a6178f221c..e1c5fd8dd5 100644 --- a/challenge-094/lubos-kolouch/perl/ch-2.pl +++ b/challenge-094/lubos-kolouch/perl/ch-2.pl @@ -54,15 +54,15 @@ no Moose; use Test::More; # TEST 1 -my $root = Point->new( value => 1); -my $elem1 = Point->new( value => 2); +my $root = Point->new(value => 1); +my $elem1 = Point->new(value => 2); my $pos = $elem1; $root->left($elem1); $pos = $root->left; -$elem1 = Point->new( value => 3); -my $elem2 = Point->new( value => 4); +$elem1 = Point->new(value => 3); +my $elem2 = Point->new(value => 4); $pos->left($elem1); $pos->right($elem2); @@ -73,21 +73,21 @@ is_deeply($arr, [1, 2, 3, 4]); # TEST 2 -$root = Point->new( value => 1); -$elem1 = Point->new( value => 2); -$elem2 = Point->new( value => 3); +$root = Point->new(value => 1); +$elem1 = Point->new(value => 2); +$elem2 = Point->new(value => 3); $root->left($elem1); $root->right($elem2); $pos = $root->left; -$elem1 = Point->new( value => 4); -$elem2 = Point->new( value => 5); +$elem1 = Point->new(value => 4); +$elem2 = Point->new(value => 5); $pos->left($elem1); $pos->right($elem2); $pos = $pos->right; -$elem1 = Point->new( value => 6); -$elem2 = Point->new( value => 7); +$elem1 = Point->new(value => 6); +$elem2 = Point->new(value => 7); $pos->left($elem1); $pos->right($elem2); diff --git a/challenge-094/lubos-kolouch/python/ch-1.py b/challenge-094/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..6324f74934 --- /dev/null +++ b/challenge-094/lubos-kolouch/python/ch-1.py @@ -0,0 +1,42 @@ +#!env python +""" +# =============================================================================== +# +# FILE: ch-1.py +# +# USAGE: ./ch-1.py +# +# DESCRIPTION: Perl Weekly Challenge 094 +# Task 1 +# Group Anagrams +# +# AUTHOR: Lubos Kolouch +# VERSION: 1.0 +# CREATED: 01/06/2021 08:43:01 PM +# =============================================================================== +""" +from collections import defaultdict + + +def get_anagrams(what): + """ Sort and group """ + + group = defaultdict(list) + + for elem in what: + joined = ''.join(sorted(elem)) + group[joined].append(elem) + + return list(group.values()) + + +def test_case1(): + """ Test case 1 from the web """ + result = get_anagrams(["opt", "bat", "saw", "tab", "pot", "top", "was"]) + assert result == [['opt', 'pot', 'top'], ['bat', 'tab'], ['saw', 'was']] + + +def test_case2(): + """ Test case 2 from the web """ + result = get_anagrams(["x"]) + assert result == [['x']] diff --git a/challenge-094/lubos-kolouch/python/ch-2.py b/challenge-094/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..a0717945cc --- /dev/null +++ b/challenge-094/lubos-kolouch/python/ch-2.py @@ -0,0 +1,106 @@ +#!env python +""" +# =============================================================================== +# +# FILE: ch_2.py +# +# USAGE: ./ch_2.py +# +# DESCRIPTION: Perl Weekly Challenge #094 +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-094/ +# Binary Tree to Linked List +# +# AUTHOR: Lubos Kolouch +# CREATED: 01/06/2021 02:39:16 PM +#=============================================================================== +""" + + +class Point: + """ Class to hold each tree node """ + + def __init__(self, value): + self.value = value + self.left = None + self.right = None + + +class SumPath: + """ Class to walk the tree """ + + def __init__(self): + self.total_path = [] + + def walk_path(self, root): + """ Walk the tree and build the total path """ + + self.total_path.append(root.value) + + if root.left: + self.walk_path(root.left) + + if root.right: + self.walk_path(root.right) + + return self.total_path + + +def test_case1(): + """ Test 1 """ + root = Point(1) + elem1 = Point(2) + + pos = elem1 + root.left = elem1 + + pos = root.left + elem1 = Point(3) + elem2 = Point(4) + + pos.left = elem1 + pos.right = elem2 + + sumpath = SumPath() + arr = sumpath.walk_path(root=root) + assert arr == [1, 2, 3, 4] + + +def test_case2(): + """ Test 2 """ + + root = Point(1) + elem1 = Point(2) + elem2 = Point(3) + root.left = elem1 + root.right = elem2 + + pos = root.left + elem1 = Point(4) + elem2 = Point(5) + pos.left = elem1 + pos.right = elem2 + + pos = pos.right + elem1 = Point(6) + elem2 = Point(7) + + pos.left = elem1 + pos.right = elem2 + + sumpath = SumPath() + arr = sumpath.walk_path(root=root) + + assert arr == [1, 2, 4, 5, 6, 7, 3] + return arr + + +def main(): + """ Print the task from the web """ + + arr = test_case2() + res_str = ' -> '.join(list(map(str, arr))) + print(res_str) + + +if __name__ == "__main__": + main() |
