Skip to content

Commit

Permalink
Adding solution to BinarySearchTreeLca
Browse files Browse the repository at this point in the history
  • Loading branch information
rlishtaba committed Oct 21, 2017
1 parent 28a6ad9 commit 0472840
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,4 @@ ENV/

/junit-report.xml
/release.sh
/debugger.py
16 changes: 16 additions & 0 deletions py_algorithms/challenges/coderbyte/binary_search_tree_lca.py
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)
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

0 comments on commit 0472840

Please sign in to comment.