Skip to content

Commit 5ea5053

Browse files
authored
Merge pull request #111 from BrianLusina/feat/lowest-common-ancestor
Lowest common ancestor of binary tree nodes
2 parents c09b702 + 52d4054 commit 5ea5053

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+728
-113
lines changed

DIRECTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,11 +284,13 @@
284284
* [Test Binary Search Tree Delete Node](https://github.com/BrianLusina/PythonSnips/blob/master/datastructures/trees/binary/search_tree/test_binary_search_tree_delete_node.py)
285285
* [Test Binary Search Tree Insert](https://github.com/BrianLusina/PythonSnips/blob/master/datastructures/trees/binary/search_tree/test_binary_search_tree_insert.py)
286286
* [Test Binary Search Tree Search](https://github.com/BrianLusina/PythonSnips/blob/master/datastructures/trees/binary/search_tree/test_binary_search_tree_search.py)
287+
* [Test Utils](https://github.com/BrianLusina/PythonSnips/blob/master/datastructures/trees/binary/test_utils.py)
287288
* Tree
288289
* [Test Binary Tree](https://github.com/BrianLusina/PythonSnips/blob/master/datastructures/trees/binary/tree/test_binary_tree.py)
289290
* [Test Binary Tree Deserialize](https://github.com/BrianLusina/PythonSnips/blob/master/datastructures/trees/binary/tree/test_binary_tree_deserialize.py)
290291
* [Test Binary Tree Serialize](https://github.com/BrianLusina/PythonSnips/blob/master/datastructures/trees/binary/tree/test_binary_tree_serialize.py)
291292
* [Test Binary Tree Visible Nodes](https://github.com/BrianLusina/PythonSnips/blob/master/datastructures/trees/binary/tree/test_binary_tree_visible_nodes.py)
293+
* [Utils](https://github.com/BrianLusina/PythonSnips/blob/master/datastructures/trees/binary/utils.py)
292294
* Btree
293295
* [Node](https://github.com/BrianLusina/PythonSnips/blob/master/datastructures/trees/btree/node.py)
294296
* Heaps

algorithms/arrays/two_sum_less_k/test_two_sum.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
class TwoSumLessKTestCase(unittest.TestCase):
66
def test_1(self):
77
"""numbers = [4,2,11,2,5,3,5,8], target = 7"""
8-
numbers = [4,2,11,2,5,3,5,8]
8+
numbers = [4, 2, 11, 2, 5, 3, 5, 8]
99
target = 7
1010
expected = 6
1111
actual = two_sum_less_than_k(numbers, target)
@@ -21,23 +21,23 @@ def test_2(self):
2121

2222
def test_3(self):
2323
"""numbers = [34,23,1,24,75,33,54,8], k = 60"""
24-
numbers = [34,23,1,24,75,33,54,8]
24+
numbers = [34, 23, 1, 24, 75, 33, 54, 8]
2525
k = 60
2626
expected = 58
2727
actual = two_sum_less_than_k(numbers, k)
2828
self.assertEqual(expected, actual)
2929

3030
def test_4(self):
3131
"""numbers = [5,5,5,5,5,5], k = 15"""
32-
numbers = [5,5,5,5,5,5]
32+
numbers = [5, 5, 5, 5, 5, 5]
3333
k = 15
3434
expected = 10
3535
actual = two_sum_less_than_k(numbers, k)
3636
self.assertEqual(expected, actual)
3737

3838
def test_5(self):
3939
"""numbers = [1,2,3,4,5], k = 3"""
40-
numbers = [1,2,3,4,5]
40+
numbers = [1, 2, 3, 4, 5]
4141
k = 3
4242
expected = -1
4343
actual = two_sum_less_than_k(numbers, k)

algorithms/search/binary_search/maxruntime_n_computers/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ def can_run_for(batteries: List[int], n: int, target_time: int) -> bool:
4949

5050

5151
def max_run_time_2(batteries: List[int], n: int) -> int:
52-
5352
"""
5453
Finds the maximum runtime that can power the computers for the given amount of time.
5554
@@ -67,7 +66,7 @@ def max_run_time_2(batteries: List[int], n: int) -> int:
6766
usable = sum(min(b, mid) for b in batteries)
6867

6968
if usable >= mid * n:
70-
left = mid
69+
left = mid
7170
else:
72-
right = mid - 1
71+
right = mid - 1
7372
return left

algorithms/search/binary_search/maxruntime_n_computers/test_max_runtime.py

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,38 @@
44

55

66
class MaxRunTimeTestCase(unittest.TestCase):
7-
8-
@parameterized.expand([
9-
([2,3,3,4], 3, 4),
10-
([1,1,4,5], 2, 5),
11-
([2,2,2,2], 1, 8),
12-
([7,2,5,10,8], 2, 16),
13-
([1,2,3,4,5], 2, 7),
14-
([3,4,3,4,5,5,8,2], 4, 8),
15-
([5,2,4], 2, 5),
16-
([1,6,2,6,8], 5, 1)
17-
])
7+
@parameterized.expand(
8+
[
9+
([2, 3, 3, 4], 3, 4),
10+
([1, 1, 4, 5], 2, 5),
11+
([2, 2, 2, 2], 1, 8),
12+
([7, 2, 5, 10, 8], 2, 16),
13+
([1, 2, 3, 4, 5], 2, 7),
14+
([3, 4, 3, 4, 5, 5, 8, 2], 4, 8),
15+
([5, 2, 4], 2, 5),
16+
([1, 6, 2, 6, 8], 5, 1),
17+
]
18+
)
1819
def test_max_runtime_1(self, batteries, n, expected):
1920
actual = max_runtime(batteries, n)
2021
self.assertEqual(expected, actual)
2122

22-
@parameterized.expand([
23-
([2,3,3,4], 3, 4),
24-
([1,1,4,5], 2, 5),
25-
([2,2,2,2], 1, 8),
26-
([7,2,5,10,8], 2, 16),
27-
([1,2,3,4,5], 2, 7),
28-
([3,4,3,4,5,5,8,2], 4, 8),
29-
([5,2,4], 2, 5),
30-
([1,6,2,6,8], 5, 1)
31-
])
23+
@parameterized.expand(
24+
[
25+
([2, 3, 3, 4], 3, 4),
26+
([1, 1, 4, 5], 2, 5),
27+
([2, 2, 2, 2], 1, 8),
28+
([7, 2, 5, 10, 8], 2, 16),
29+
([1, 2, 3, 4, 5], 2, 7),
30+
([3, 4, 3, 4, 5, 5, 8, 2], 4, 8),
31+
([5, 2, 4], 2, 5),
32+
([1, 6, 2, 6, 8], 5, 1),
33+
]
34+
)
3235
def test_max_runtime_2(self, batteries, n, expected):
3336
actual = max_run_time_2(batteries, n)
3437
self.assertEqual(expected, actual)
3538

3639

37-
if __name__ == '__main__':
40+
if __name__ == "__main__":
3841
unittest.main()

datascience/numeric_python/statistics/TestChecker.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ def __init__(self, n):
88
@staticmethod
99
def test_function(actual, expected):
1010
print
11-
"Test for " + str(
12-
actual
13-
) + " passed " if actual == expected else "Test for " + str(
14-
actual
15-
) + " failed, expected " + str(expected)
11+
(
12+
"Test for " + str(actual) + " passed "
13+
if actual == expected
14+
else "Test for " + str(actual) + " failed, expected " + str(expected)
15+
)

datastructures/__init__.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
from datastructures.sets import DisjointSetUnion, UnionFind
22

3-
__all__ = [
4-
"DisjointSetUnion",
5-
"UnionFind"
6-
]
3+
__all__ = ["DisjointSetUnion", "UnionFind"]

datastructures/lists/is_sorted_how/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ def is_sorted_and_how_2(arr):
1515
return (
1616
"yes, ascending"
1717
if is_sorted_with(arr, operator.le)
18-
else "yes, descending"
19-
if is_sorted_with(arr, operator.ge)
20-
else "no"
18+
else "yes, descending" if is_sorted_with(arr, operator.ge) else "no"
2119
)
2220

2321

datastructures/sets/union_find/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def get_count(self) -> int:
4747

4848
class UnionFind:
4949
"""A minimal Union-Find data structure with path compression."""
50-
50+
5151
def __init__(self, size: int):
5252
"""Initializes the data structure with 'size' elements."""
5353
if size <= 0:
@@ -71,4 +71,4 @@ def union(self, x: int, y: int) -> bool:
7171
if root_x != root_y:
7272
self.parent[root_y] = root_x
7373
return True
74-
return False
74+
return False

datastructures/streams/stream_checker/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55

66
class StreamChecker(object):
7-
87
def __init__(self, words: List[str]):
98
"""
109
Initializes a StreamChecker instance.

datastructures/streams/stream_checker/test_stream_checker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ def test_3(self):
3131
self.assertFalse(stream.query("b"))
3232

3333

34-
if __name__ == '__main__':
34+
if __name__ == "__main__":
3535
unittest.main()

0 commit comments

Comments
 (0)