Skip to content

Commit 49496e8

Browse files
committed
Merge branch 'feature/naming' into develop
2 parents 8e66ed3 + 7fff4cd commit 49496e8

18 files changed

+97
-64
lines changed

python-algorithm/algorithm/graph/test/test_breadth_first_search.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def create_city_graph() -> networkx.Graph:
3737

3838
@pytest.mark.benchmark(group='graph_neighbors')
3939
@pytest.mark.parametrize(
40-
argnames='graph, source, expected',
40+
argnames='graph, source, expected_neighbors',
4141
argvalues=[
4242
(city_graph, "Seattle", ["Chicago", "San Francisco"]),
4343
(city_graph, "San Francisco", ["Seattle", "Riverside", "Los Angeles"]),
@@ -55,24 +55,39 @@ def create_city_graph() -> networkx.Graph:
5555
(city_graph, "Philadelphia", ["New York", "Washington"]),
5656
(city_graph, "Washington", ["Atlanta", "Miami", "Detroit", "Philadelphia"])
5757
])
58-
def test_graph_neighbors(benchmark, graph, source, expected):
59-
result = benchmark(graph.neighbors, source)
60-
assert sorted(expected) == sorted(result)
58+
def test_graph_neighbors(benchmark, graph, source, expected_neighbors):
59+
"""
60+
Find the neighboring city nodes of the source city node in the United States city graph.
61+
:param benchmark: benchmark fixture
62+
:param graph: city graph of the United States
63+
:param source: source city node
64+
:param expected_neighbors: list of neighboring city nodes
65+
"""
66+
neighbor_iterator = benchmark(graph.neighbors, source)
67+
assert sorted(expected_neighbors) == sorted(neighbor_iterator)
6168

6269

6370
@pytest.mark.benchmark(group='graph_breadth_first_search')
6471
@pytest.mark.parametrize(
65-
argnames='graph, source, distance, expected',
72+
argnames='graph, source, distance, expected_nodes',
6673
argvalues=[
6774
(city_graph, "Boston", 1, ["Detroit", "New York"]),
6875
(city_graph, "Boston", 2, ["Chicago", "Washington", "Philadelphia"]),
6976
(city_graph, "Boston", 3, ["Seattle", "Riverside", "Dallas", "Atlanta", "Miami"]),
7077
],
71-
ids=["distance1", "distance2", "distance3"])
72-
def test_graph_breadth_first_search(benchmark, graph, source, distance, expected):
78+
ids=['distance1', 'distance2', 'distance3'])
79+
def test_graph_breadth_first_search(benchmark, graph, source, distance, expected_nodes):
80+
"""
81+
Find the city nodes at a given distance from the source city node in the United States city graph.
82+
:param benchmark: benchmark fixture
83+
:param graph: city graph of the United States
84+
:param source: source city node
85+
:param distance: distance from the source city node
86+
:param expected_nodes: list of city nodes at the given distance from the source city node
87+
"""
7388
bfs_generator = benchmark(networkx.bfs_layers, graph, source)
74-
path = []
89+
nodes = []
7590
for i, layer in enumerate(bfs_generator):
7691
if i == distance:
77-
path = list(layer)
78-
assert path == expected
92+
nodes = list(layer)
93+
assert nodes == expected_nodes

python-algorithm/algorithm/greedy/cashier_change.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
def cashier_change(denominations: list[int], price: int) -> int:
2+
"""
3+
Find the minimum number of coins needed to make change for a given amount of money.
4+
:param denominations: list of coin denominations
5+
:param price: amount of money
6+
:return: minimum number of coins
7+
"""
28
change = price
39
coins = 0
410

python-algorithm/algorithm/greedy/interval_schedule.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
def interval_scheduling_lecture(lectures: list[dict]) -> list[dict]:
22
"""
3-
Greedy algorithm to schedule lectures.
3+
Schedule lectures to maximize the number of lectures that can be attended. This uses the greedy algorithm.
44
:param lectures: list of lectures
55
:return: list of scheduled lectures
66
"""

python-algorithm/algorithm/greedy/test/test_cashier_change.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55

66
@pytest.mark.benchmark(group='cashier_change')
77
@pytest.mark.parametrize(
8-
argnames='denominations, price, expected',
8+
argnames='denominations, price, expected_count',
99
argvalues=[
1010
([25, 10, 5, 1], 30, 2),
1111
([25, 10, 5, 1], 24, 6)
1212
],
1313
ids=['case1', 'case2'])
14-
def test_cashier_change(benchmark, denominations, price, expected):
15-
result = benchmark(cashier_change, denominations, price)
16-
assert expected == result
14+
def test_cashier_change(benchmark, denominations, price, expected_count):
15+
coin_count = benchmark(cashier_change, denominations, price)
16+
assert expected_count == coin_count

python-algorithm/algorithm/greedy/test/test_interval_schedule.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717

1818
@pytest.mark.benchmark(group='interval_scheduling_lecture')
1919
@pytest.mark.parametrize(
20-
argnames='lectures, expected',
20+
argnames='lectures, expected_schedule',
2121
argvalues=[([lecture1, lecture2, lecture3], [lecture2, lecture3])],
2222
ids=['case1'])
23-
def test_interval_scheduling_lecture(benchmark, lectures, expected):
23+
def test_interval_scheduling_lecture(benchmark, lectures, expected_schedule):
2424
result = benchmark(interval_scheduling_lecture, lectures)
25-
assert expected == result
25+
assert expected_schedule == result

python-algorithm/algorithm/math/base_expansion.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
def base_expansion(number: int, base: int) -> str:
22
"""
3-
Base b expansion of integer number
3+
Convert a number to a different base. This is base b expansion of a number.
4+
:param number: number to convert
5+
:param base: base to convert to
6+
:return: number in the new base
47
"""
5-
68
result = []
79
expansion_table = {0: '0', 1: '1', 2: '2', 3: '3', 4: '4',
810
5: '5', 6: '6', 7: '7', 8: '8', 9: '9',

python-algorithm/algorithm/math/binary_operation.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
def addition_binary_number(number1: str, number2: str) -> str:
22
"""
3-
Addition of binary numbers
3+
Add two binary numbers which are represented as strings.
4+
:param number1: number string 1
5+
:param number2: number string 2
6+
:return: sum of the two binary numbers
47
"""
5-
68
result = []
79
carry = 0
810
number1 = number1[::-1]

python-algorithm/algorithm/math/greatest_common_divisor.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
def gcd_euclidean(a: int, b: int) -> int:
22
"""
33
Find the greatest common divisor of two numbers using the Euclidean algorithm.
4+
:param a: number
5+
:param b: divisor
6+
:return: the greatest common divisor
47
"""
5-
68
while b != 0:
79
a, b = b, a % b
810
return a
@@ -12,8 +14,10 @@ def gcd_euclidean_divmod(a: int, b: int) -> int:
1214
"""
1315
Find the greatest common divisor of two numbers using the Euclidean algorithm.
1416
If the numbers are large, use divmod to calculate them.
17+
:param a: number
18+
:param b: divisor
19+
:return: the greatest common divisor
1520
"""
16-
1721
while b != 0:
1822
a, b = b, divmod(a, b)[1]
1923
return a
@@ -22,8 +26,10 @@ def gcd_euclidean_divmod(a: int, b: int) -> int:
2226
def gcd_extended_euclidean(a: int, b: int) -> tuple:
2327
"""
2428
Find the greatest common divisor of two numbers using the extended Euclidean algorithm.
29+
:param a: number
30+
:param b: divisor
31+
:return: the greatest common divisor, x, y (where ax + by = gcd(a, b))
2532
"""
26-
2733
if b == 0:
2834
return a, 1, 0
2935
else:
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from algorithm.math.greatest_common_divisor import gcd_euclidean
22

33

4-
def lcm(a: int, b: int) -> int:
4+
def lcm_with_gcd(a: int, b: int) -> int:
55
"""
66
Find the least common multiple of two numbers.
7-
LCM(a,b) = a * b / GCD(a,b)
7+
LCM(a, b) = a * b / GCD(a, b)
8+
:param a: number1
9+
:param b: number2
10+
:return: the least common multiple
811
"""
9-
1012
return a * b // gcd_euclidean(a, b)

python-algorithm/algorithm/math/matrix_multiplication.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33

44
def multiply_matrix(matrix_a: np.array, matrix_b: np.array) -> np.array:
55
"""
6-
Function that performs matrix multiplication
7-
8-
return np.dot(matrix_a, matrix_b)
6+
Function that performs matrix multiplication.
7+
If you use numpy, it is np.dot(matrix_a, matrix_b)
8+
:param matrix_a: matrix1
9+
:param matrix_b: matrix2
10+
:return: the result of matrix multiplication
911
"""
10-
11-
# m x n matrix
1212
m = matrix_a.shape[0]
1313
n = matrix_b.shape[1]
1414
matrix_c = np.zeros((m, n))

0 commit comments

Comments
 (0)