diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-01-09 20:03:09 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-01-09 20:03:09 +0000 |
| commit | 7e468b1ececa0c32861a00a2e8bd1008cc2a9d00 (patch) | |
| tree | efc7e033010b64309ad8104987b68c73d41e9f66 | |
| parent | a08f8db21f0b49006795efdbe3a8c194616f0be7 (diff) | |
| download | perlweeklychallenge-club-7e468b1ececa0c32861a00a2e8bd1008cc2a9d00.tar.gz perlweeklychallenge-club-7e468b1ececa0c32861a00a2e8bd1008cc2a9d00.tar.bz2 perlweeklychallenge-club-7e468b1ececa0c32861a00a2e8bd1008cc2a9d00.zip | |
Add Python solution to challenge 094
| -rw-r--r-- | challenge-094/paulo-custodio/python/ch-1.py | 48 | ||||
| -rw-r--r-- | challenge-094/paulo-custodio/python/ch-2.py | 96 | ||||
| -rw-r--r-- | challenge-094/paulo-custodio/test.pl | 16 |
3 files changed, 153 insertions, 7 deletions
diff --git a/challenge-094/paulo-custodio/python/ch-1.py b/challenge-094/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..2fabd1115e --- /dev/null +++ b/challenge-094/paulo-custodio/python/ch-1.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python + +# Challenge 094 +# +# TASK #1 > Group Anagrams +# Submitted by: Mohammad S Anwar +# You are given an array of strings @S. +# +# Write a script to group Anagrams together in any random order. +# +# An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. +# +# Example 1: +# Input: ("opt", "bat", "saw", "tab", "pot", "top", "was") +# Output: [ ("bat", "tab"), +# ("saw", "was"), +# ("top", "pot", "opt") ] +# Example 2: +# Input: ("x") +# Output: [ ("x") ] + +import sys + +def make_key(str): + return ''.join(sorted(str)) + +def print_map(map): + output = "[ " + for value in map.values(): + output += "(" + for word in value: + output += '"' + word + '", ' + output = output[:-2] + "),\n " + output = output[:-4] + " ]" + print(output) + +def solve(): + map = {} + + for arg in sys.argv[1:]: + key = make_key(arg) + if key not in map: + map[key] = [] + map[key] += [arg] + + print_map(map) + +solve() diff --git a/challenge-094/paulo-custodio/python/ch-2.py b/challenge-094/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..61eb054d7f --- /dev/null +++ b/challenge-094/paulo-custodio/python/ch-2.py @@ -0,0 +1,96 @@ +#!/usr/bin/env python + +# Challenge 094 +# +# TASK #2 > Binary Tree to Linked List +# Submitted by: Mohammad S Anwar +# You are given a binary tree. +# +# Write a script to represent the given binary tree as an object and flatten +# it to a linked list object. Finally print the linked list object. +# +# Example: +# Input: +# +# 1 +# / \ +# 2 3 +# / \ +# 4 5 +# / \ +# 6 7 +# +# Output: +# +# 1 -> 2 -> 4 -> 5 -> 6 -> 7 -> 3 +# + +import fileinput +import re + +class Node: + def __init__(self, value): + self.value = value + self.left = None + self.right = None + + def __str__(self): + return "(value: {}, left: {}, right: {})" \ + .format(self.value, self.left, self.right) + +def read_input(): + lines = [] + for line in fileinput.input(): + lines.append(line) + return lines + +def parse_subtree(lines, row, col): + + def ch(row, col): + if row < 0 or row >= len(lines) or col < 0 or col >= len(lines[row]): + return ' ' + else: + return lines[row][col] + + tree = Node(int(lines[row][col])) + if ch(row + 1, col - 1) == '/': + tree.left = parse_subtree(lines, row + 2, col - 2) + if ch(row + 1, col + 1) == '\\': + tree.right = parse_subtree(lines, row + 2, col + 2) + + return tree + + +def parse(lines): + found = re.search("^[ ]+\d", lines[0]) + col = found.span()[1] - 1 + return parse_subtree(lines, 0, col) + +def flatten(tree): + if tree: + left = tree.left + right = tree.right + flatten(left) + flatten(right) + tree.left = None + tree.right = left + node = tree + while node.right: + node = node.right + node.right = right + +def print_list(node): + output = "" + while node: + output += str(node.value) + " -> " + node = node.right + output = output[:-4] + print(output) + +def solve(): + lines = read_input() + tree = parse(lines) + flatten(tree) + print_list(tree) + +solve() diff --git a/challenge-094/paulo-custodio/test.pl b/challenge-094/paulo-custodio/test.pl index b44edee588..1232740d4b 100644 --- a/challenge-094/paulo-custodio/test.pl +++ b/challenge-094/paulo-custodio/test.pl @@ -38,8 +38,9 @@ END END my($in, $out) = @$_; - is capture("gforth forth/ch-1.fs $in"), $out; - is capture( "basic/ch-1 $in"), $out; + is capture("python python/ch-1.py $in"), $out; + is capture( "gforth forth/ch-1.fs $in"), $out; + is capture( "basic/ch-1 $in"), $out; } @@ -67,11 +68,12 @@ END my($in, $out) = @$_; path($input)->spew($in); - is capture( "perl perl/ch-2.pl < $input"), "$out\n"; - is capture("gforth forth/ch-2.fs < $input"), "$out\n"; - is capture( "c/ch-2 < $input"), "$out\n"; - is capture( "cpp/ch-2 < $input"), "$out\n"; - is capture( "basic/ch-2 < $input"), "$out\n"; + is capture( "perl perl/ch-2.pl < $input"), "$out\n"; + is capture("python python/ch-2.py < $input"), "$out\n"; + is capture( "gforth forth/ch-2.fs < $input"), "$out\n"; + is capture( "c/ch-2 < $input"), "$out\n"; + is capture( "cpp/ch-2 < $input"), "$out\n"; + is capture( "basic/ch-2 < $input"), "$out\n"; } unlink $input; |
