Skip to content

Commit 817e4ed

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank] Interview Preparation Kit: Search: Swap Nodes [Algo]. New tests added: build and plain tree.
1 parent a49e108 commit 817e4ed

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

src/hackerrank/interview_preparation_kit/search/swap_nodes_algo.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def build_tree(indexes: List[List[int]]) -> Node:
9191
return root
9292

9393

94-
def plain_tree(root: Node) -> List[int]:
94+
def flatten_tree(root: Node) -> List[int]:
9595
node_collector: Dict[int, list[Node]] = {}
9696

9797
node_collector = traverse_in_order_collector(
@@ -136,7 +136,7 @@ def swap_nodes(indexes: List[List[int]], queries: List[int]) -> List[List[int]]:
136136

137137
node_collector = dict(sorted(node_collector.items()))
138138

139-
plain = plain_tree(tree) # original
139+
plain = flatten_tree(tree) # original
140140

141141
LOGGER.debug('Plain tree: %s', plain)
142142

@@ -146,7 +146,7 @@ def swap_nodes(indexes: List[List[int]], queries: List[int]) -> List[List[int]]:
146146
for node in node_list:
147147
swap_branch(node)
148148

149-
plain = plain_tree(tree)
149+
plain = flatten_tree(tree)
150150
output.append(plain)
151151

152152
return output

src/hackerrank/interview_preparation_kit/search/swap_nodes_algo.testcases.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,34 @@
22
{
33
"title": "Sample 0",
44
"nodes": [[2, 3], [-1, -1], [-1, -1]],
5+
"flattened": [2, 1, 3],
56
"queries": [1, 1],
6-
"answer": [[3, 1, 2], [2, 1, 3]]
7+
"expected": [[3, 1, 2], [2, 1, 3]]
78
},
89
{
910
"title": "Sample 1",
1011
"nodes": [[2, 3], [-1, 4], [-1, 5], [-1, -1], [-1, -1]],
12+
"flattened": [2, 4, 1, 3, 5],
1113
"queries": [2],
12-
"answer": [[4, 2, 1, 5, 3]]
14+
"expected": [[4, 2, 1, 5, 3]]
1315
},
1416
{
1517
"title": "Sample 2",
1618
"nodes": [[2, 3], [4, -1], [5, -1], [6, -1], [7, 8], [-1, 9],
1719
[-1, -1], [10, 11], [-1, -1], [-1, -1], [-1, -1]],
20+
"flattened": [6, 9, 4, 2, 1, 7, 5, 10, 8, 11, 3],
1821
"queries": [2, 4],
19-
"answer": [[2, 9, 6, 4, 1, 3, 7, 5, 11, 8, 10],
22+
"expected": [[2, 9, 6, 4, 1, 3, 7, 5, 11, 8, 10],
2023
[2, 6, 9, 4, 1, 3, 7, 5, 10, 8, 11]]
2124
},
2225
{
2326
"title": "Sample Test Case 1",
2427
"nodes": [[2, 3], [4, 5], [6, -1], [-1, 7], [8, 9], [10, 11], [12, 13],
2528
[-1, 14], [-1, -1], [15, -1], [16, 17], [-1, -1], [-1, -1],
2629
[-1, -1], [-1, -1], [-1, -1], [-1, -1]],
30+
"flattened": [4, 12, 7, 13, 2, 8, 14, 5, 9, 1, 15, 10, 6, 16, 11, 17, 3],
2731
"queries": [2, 3],
28-
"answer": [[14, 8, 5, 9, 2, 4, 13, 7, 12, 1, 3, 10, 15, 6, 17, 11, 16],
32+
"expected": [[14, 8, 5, 9, 2, 4, 13, 7, 12, 1, 3, 10, 15, 6, 17, 11, 16],
2933
[9, 5, 14, 8, 2, 13, 7, 12, 4, 1, 3, 17, 11, 16, 6, 10, 15]]
3034
}
3135
]

src/hackerrank/interview_preparation_kit/search/swap_nodes_algo_test.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,57 @@
22
from pathlib import Path
33

44
from ....hackerrank.lib.loader import load_test_cases
5-
from .swap_nodes_algo import swap_nodes, build_tree, plain_tree, swap_branch
5+
from ....hackerrank.lib.node import Node
6+
from .swap_nodes_algo import swap_nodes, build_tree, flatten_tree, swap_branch
67

78
FILE_PATH = str(Path(__file__).resolve().parent)
89
TEST_CASES = load_test_cases(FILE_PATH + '/swap_nodes_algo.testcases.json')
910

1011

1112
class TestSwapNodesAlgo(unittest.TestCase):
12-
1313
def test_swap_nodes(self):
1414

1515
for _, _tt in enumerate(TEST_CASES):
1616

1717
self.assertEqual(
18-
swap_nodes(_tt['nodes'], _tt['queries']), _tt['answer'],
18+
swap_nodes(_tt['nodes'], _tt['queries']), _tt['expected'],
1919
f"{_} | swap_nodes({_tt['nodes'], _tt['queries']}) must be "
20-
f"=> {_tt['answer']}")
20+
f"=> {_tt['expected']}")
2121

2222
def test_swap_branch(self):
2323

2424
t_input = None
2525
t_result = swap_branch(t_input)
26-
answer = None
26+
expected = None
2727

2828
self.assertEqual(
2929
t_result,
30-
answer,
30+
expected,
3131
f"swap_branch({t_input}) must be "
32-
f"=> {answer}"
32+
f"=> {expected}"
3333
)
3434

35+
def test_build_tree_and_plain(self):
36+
37+
for _, _tt in enumerate(TEST_CASES):
38+
39+
t_to_test: Node = build_tree(_tt['nodes'])
40+
t_result: list[int] = flatten_tree(t_to_test)
41+
42+
self.assertEqual(
43+
t_result, _tt['flattened'],
44+
f"{_} | flatten_tree({_tt['nodes']}) must be {_tt['expected']}")
45+
3546
def test_build_tree_empty(self):
3647

3748
t_input = []
3849
t_to_test = build_tree(t_input)
39-
t_result = plain_tree(t_to_test)
40-
answer = [1]
50+
t_result = flatten_tree(t_to_test)
51+
expected = [1]
4152

4253
self.assertEqual(
4354
t_result,
44-
answer,
55+
expected,
4556
f"build_tree({t_input}) must be "
46-
f"=> {answer}"
57+
f"=> {expected}"
4758
)

0 commit comments

Comments
 (0)