Skip to content

Commit

Permalink
[Python] Challenge 4 (Unreviewed) (YearOfProgramming#358)
Browse files Browse the repository at this point in the history
* challenge 4 in python

* Added a function to make a tree 4 levels deep
  • Loading branch information
sarcodian authored and Remillardj committed Jan 18, 2017
1 parent 07f88a6 commit f2c3ffb
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
14 changes: 14 additions & 0 deletions challenge_4/python/sarcodian/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
So I took this out and after a nice long slack discussion, decided to put it back,
since what I lacked most was documentation, which was causing a missunderstanding.

So, instead of building a Node class, and recursing my way down, I built each node in
list notation using the format tree = [head, [left, [], []], [right, [], []]]

The empty lists are equvalent to None

If you want to add left.left, you simply add tree[1][1] = [left.left, [], []]
If you want to add left.right, you can add tree[1][2] = [left.right, [], []]
Same notation if you want to retrieve a node or branch.

TL:DR; I did not hard code a list and reverse it. You can build and manipulate the tree from
scratch and the reverse fuction will recurse down until the null values and flip them for you.
72 changes: 72 additions & 0 deletions challenge_4/python/sarcodian/src/challenge_4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
"""
Each root has two subtrees, each subtree may have a root
Each tree has the format ['parent',
['child1',
['child1.1'],['child1.2']],
['child2',
['child2.1'],['child2.2']]]
"""

import itertools

def reverse(tree):
subtree = tree.copy()
if len(subtree[1]) < 2 and len(subtree[2]) < 2:
return [subtree[0], subtree[2], subtree[1]]
elif len(subtree[1]) < 2:
return [subtree[0], [reverse(subtree[2])], subtree[1]]
elif len(subtree[2]) < 2:
return [subtree[0], subtree[2], [reverse(subtree[1])]]
else:
return [subtree[0], reverse(subtree[2]), reverse(subtree[1])]


test = [4,
[2,
[1], [3]],
[7,
[6], [9]]
]

def create_large_tree():
"""
Create a tree with up to 4 levels of nodes to show that implementation scales, enter less then 15
"""
value_of_nodes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 'a', 'b', 'c', 'd', 'e']
tree = ''
depth = 0
count = 0

while depth < 4:
if depth == 0:
tree = [value_of_nodes[0], [], []]
depth += 1
count += 1
elif depth == 1:
for i in [1,2]:
tree[i] = [value_of_nodes[count], [], []]
count += 1
depth += 1
elif depth == 2:
for i,j in itertools.product([1,2], repeat=depth):
tree[i][j] = [value_of_nodes[count], [], []]
count += 1
depth += 1
elif depth == 3:
for i, j, k in itertools.product([1,2], repeat=depth):
tree[i][j][k] = [value_of_nodes[count], [], []]
count += 1
depth += 1
return tree


print(test)
test_rev = reverse(test)
print(test_rev)
print(reverse(test_rev))

test2 = create_large_tree()
print(test2)
test_rev2 = reverse(test2)
print(test_rev2)
print(reverse(test_rev2))

0 comments on commit f2c3ffb

Please sign in to comment.