forked from uchicago-cs/python-practice-problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
min_depth_leaf.py
113 lines (81 loc) · 3.82 KB
/
min_depth_leaf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
from tree import Tree
def min_depth_leaf(tree):
"""
Computes the minimum depth of a leaf in the tree (length of shortest
path from the root to a leaf).
Input:
tree: a Tree instance.
Returns: (integer) the minimum depth of of a leaf in the tree.
"""
pass
#############################################################
### ###
### Testing code. ###
### !!! DO NOT MODIFY ANY CODE BELOW THIS POINT !!! ###
### ###
#############################################################
import sys
import pytest
import util
sys.path.append('../')
import test_utils as utils
import tree_test_utils as tree_utils
def do_test_min_depth_leaf(trees_and_original_trees, tree_name, expected):
trees, original_trees = trees_and_original_trees
recreate_msg = tree_utils.gen_recreate_msg_with_trees('min_depth_leaf', tree_name)
actual = min_depth_leaf(trees[tree_name])
utils.check_none(actual, recreate_msg)
utils.check_type(actual, expected, recreate_msg)
utils.check_equals(actual, expected, recreate_msg)
tree_utils.check_tree_unmodified(trees[tree_name], original_trees[tree_name],
recreate_msg)
def test_min_depth_leaf_1(trees_min_depth_leaf):
do_test_min_depth_leaf(trees_min_depth_leaf, "tree_1", 1)
def test_min_depth_leaf_2(trees_min_depth_leaf):
do_test_min_depth_leaf(trees_min_depth_leaf, "tree_2", 1)
def test_min_depth_leaf_3(trees_min_depth_leaf):
do_test_min_depth_leaf(trees_min_depth_leaf, "tree_3", 5)
def test_min_depth_leaf_4(trees_min_depth_leaf):
do_test_min_depth_leaf(trees_min_depth_leaf, "tree_4", 4)
def test_min_depth_leaf_5(trees_min_depth_leaf):
do_test_min_depth_leaf(trees_min_depth_leaf, "tree_5", 3)
def test_min_depth_leaf_6(trees_min_depth_leaf):
do_test_min_depth_leaf(trees_min_depth_leaf, "tree_6", 2)
def test_min_depth_leaf_7(trees_min_depth_leaf):
do_test_min_depth_leaf(trees_min_depth_leaf, "tree_7", 2)
def test_min_depth_leaf_8(trees_min_depth_leaf):
do_test_min_depth_leaf(trees_min_depth_leaf, "tree_8", 2)
def test_min_depth_leaf_9(trees_min_depth_leaf):
do_test_min_depth_leaf(trees_min_depth_leaf, "tree_9", 49)
def test_min_depth_leaf_10(trees_min_depth_leaf):
do_test_min_depth_leaf(trees_min_depth_leaf, "tree_10", 2)
def test_min_depth_leaf_11(trees_min_depth_leaf):
do_test_min_depth_leaf(trees_min_depth_leaf, "tree_11", 6)
def test_min_depth_leaf_12(trees_min_depth_leaf):
do_test_min_depth_leaf(trees_min_depth_leaf, "tree_12", 2)
def test_min_depth_leaf_13(trees_min_depth_leaf):
do_test_min_depth_leaf(trees_min_depth_leaf, "tree_13", 4)
def test_min_depth_leaf_14(trees_min_depth_leaf):
do_test_min_depth_leaf(trees_min_depth_leaf, "tree_14", 4)
def test_min_depth_leaf_15(trees_min_depth_leaf):
do_test_min_depth_leaf(trees_min_depth_leaf, "tree_15", 3)
def test_min_depth_leaf_16(trees_min_depth_leaf):
do_test_min_depth_leaf(trees_min_depth_leaf, "tree_16", 4)
def test_min_depth_leaf_17(trees_min_depth_leaf):
do_test_min_depth_leaf(trees_min_depth_leaf, "tree_17", 2)
def test_min_depth_leaf_18(trees_min_depth_leaf):
do_test_min_depth_leaf(trees_min_depth_leaf, "tree_18", 6)
def test_min_depth_leaf_19(trees_min_depth_leaf):
do_test_min_depth_leaf(trees_min_depth_leaf, "tree_19", 3)
def test_min_depth_leaf_20(trees_min_depth_leaf):
do_test_min_depth_leaf(trees_min_depth_leaf, "tree_20", 2)
@pytest.fixture(scope="session")
def trees_min_depth_leaf():
"""
Fixture for loading the trees for min_depth_leaf
"""
return get_trees()
def get_trees():
trees = util.load_trees("sample_trees.json")
original_trees = util.load_trees("sample_trees.json")
return trees, original_trees