aboutsummaryrefslogtreecommitdiff
path: root/challenge-057/paulo-custodio/python
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-057/paulo-custodio/python')
-rw-r--r--challenge-057/paulo-custodio/python/ch-1.py87
-rw-r--r--challenge-057/paulo-custodio/python/ch-2.py34
2 files changed, 121 insertions, 0 deletions
diff --git a/challenge-057/paulo-custodio/python/ch-1.py b/challenge-057/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..f852b736bb
--- /dev/null
+++ b/challenge-057/paulo-custodio/python/ch-1.py
@@ -0,0 +1,87 @@
+#!/usr/bin/env python3
+
+# Challenge 057
+#
+# TASK #1 › Invert Tree
+# You are given a full binary tree of any height, similar to the one below:
+#
+#
+#
+# Write a script to invert the tree, by mirroring the children of every node,
+# from left to right. The expected output from the tree above would be:
+#
+#
+#
+# The input can be any sensible machine-readable binary tree format of your
+# choosing, and the output should be the same format.
+#
+# BONUS
+# In addition to the above, you may wish to pretty-print your binary tree in a
+# human readable text-based format similar to the following:
+#
+# 1
+# / \
+# 3 2
+# / \ / \
+# 7 6 5 4
+
+import fileinput
+import re
+import sys
+
+class Node:
+ def __init__(self, value):
+ self.value = value
+ self.left = None
+ self.right = None
+
+ def __repr__(self):
+ return "Node(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_tree(lines):
+ found = re.search(r'^[ ]+\d', lines[0])
+ col = found.span()[1] - 1
+ return parse_subtree(lines, 0, col)
+
+def invert_tree(tree):
+ if tree:
+ tree.left, tree.right = tree.right, tree.left
+ invert_tree(tree.left)
+ invert_tree(tree.right)
+
+def dump_tree(tree):
+ if tree:
+ print(tree.value, end='')
+ if tree.left or tree.right:
+ print("(", end='')
+ dump_tree(tree.left)
+ print("|", end='')
+ dump_tree(tree.right)
+ print(")", end='')
+
+tree = parse_tree(read_input())
+invert_tree(tree)
+dump_tree(tree)
diff --git a/challenge-057/paulo-custodio/python/ch-2.py b/challenge-057/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..5f833663b5
--- /dev/null
+++ b/challenge-057/paulo-custodio/python/ch-2.py
@@ -0,0 +1,34 @@
+#!/usr/bin/env python3
+
+# Challenge 057
+#
+# TASK #2 › Shortest Unique Prefix
+# Write a script to find the shortest unique prefix for each each word in the
+# given list. The prefixes will not necessarily be of the same length.
+#
+# Sample Input
+# [ "alphabet", "book", "carpet", "cadmium", "cadeau", "alpine" ]
+# Expected Output
+# [ "alph", "b", "car", "cadm", "cade", "alpi" ]
+
+import re
+import sys
+
+def shortest_prefix1(word, words):
+ for i in range(1, len(word)):
+ prefix = word[:i]
+ found = 0
+ for x in words:
+ if re.search(r'^'+prefix, x):
+ found += 1
+ if found == 1:
+ return prefix
+ return word
+
+def shortest_prefix(words):
+ prefixes = []
+ for word in words:
+ prefixes.append(shortest_prefix1(word, words))
+ return prefixes
+
+print(" ".join(shortest_prefix(sys.argv[1:])))