-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding solution to BinarySearchTreeLca
- Loading branch information
Showing
3 changed files
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -109,3 +109,4 @@ ENV/ | |
|
||
/junit-report.xml | ||
/release.sh | ||
/debugger.py |
16 changes: 16 additions & 0 deletions
16
py_algorithms/challenges/coderbyte/binary_search_tree_lca.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import ast | ||
|
||
|
||
class BinarySearchTreeLca: | ||
def __call__(self, str_arr: str) -> str: | ||
tree_nodes = ast.literal_eval(str_arr[0]) | ||
_left = int(str_arr[1]) | ||
_right = int(str_arr[2]) | ||
_max = max(_left, _right) | ||
_min = min(_left, _right) | ||
_low = min(tree_nodes.index(_min), tree_nodes.index(_max)) | ||
xs = [] | ||
for x in tree_nodes[0:_low + 1]: | ||
if x <= _max: | ||
xs.append(x) | ||
return max(xs) |
13 changes: 13 additions & 0 deletions
13
tests/py_algorithms/challenges/coderbyte/binary_search_tree_lca_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from py_algorithms.challenges.coderbyte.binary_search_tree_lca import BinarySearchTreeLca | ||
|
||
|
||
class TestBinarySearchTreeLca: | ||
def test_impl(self): | ||
inputs = { | ||
5: ["[10, 5, 1, 7, 40, 50]", "1", "7"], | ||
12: ["[3, 2, 1, 12, 4, 5, 13]", "5", "13"], | ||
10: ["[10, 5, 1, 7, 40, 50]", "5", "10"], | ||
} | ||
f = BinarySearchTreeLca() | ||
for key in inputs: | ||
assert f(inputs[key]) == key |