Skip to content

Commit e28cc39

Browse files
authored
Merge branch 'master' into Python-3.14
2 parents 6e07abf + e26c388 commit e28cc39

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

+1359
-685
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ repos:
1616
- id: auto-walrus
1717

1818
- repo: https://github.com/astral-sh/ruff-pre-commit
19-
rev: v0.11.8
19+
rev: v0.11.11
2020
hooks:
2121
- id: ruff
2222
- id: ruff-format
@@ -29,7 +29,7 @@ repos:
2929
- tomli
3030

3131
- repo: https://github.com/tox-dev/pyproject-fmt
32-
rev: "v2.5.1"
32+
rev: "v2.6.0"
3333
hooks:
3434
- id: pyproject-fmt
3535

DIRECTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@
480480
* [Bi Directional Dijkstra](graphs/bi_directional_dijkstra.py)
481481
* [Bidirectional A Star](graphs/bidirectional_a_star.py)
482482
* [Bidirectional Breadth First Search](graphs/bidirectional_breadth_first_search.py)
483+
* [Bidirectional Search](graphs/bidirectional_search.py)
483484
* [Boruvka](graphs/boruvka.py)
484485
* [Breadth First Search](graphs/breadth_first_search.py)
485486
* [Breadth First Search 2](graphs/breadth_first_search_2.py)
@@ -899,6 +900,7 @@
899900
* [N Body Simulation](physics/n_body_simulation.py)
900901
* [Newtons Law Of Gravitation](physics/newtons_law_of_gravitation.py)
901902
* [Newtons Second Law Of Motion](physics/newtons_second_law_of_motion.py)
903+
* [Orbital Transfer Work](physics/orbital_transfer_work.py)
902904
* [Period Of Pendulum](physics/period_of_pendulum.py)
903905
* [Photoelectric Effect](physics/photoelectric_effect.py)
904906
* [Potential Energy](physics/potential_energy.py)

backtracking/sum_of_subsets.py

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
11
"""
2-
The sum-of-subsetsproblem states that a set of non-negative integers, and a
2+
The sum-of-subsets problem states that a set of non-negative integers, and a
33
value M, determine all possible subsets of the given set whose summation sum
44
equal to given M.
55
66
Summation of the chosen numbers must be equal to given number M and one number
77
can be used only once.
88
"""
99

10-
from __future__ import annotations
1110

11+
def generate_sum_of_subsets_solutions(nums: list[int], max_sum: int) -> list[list[int]]:
12+
"""
13+
The main function. For list of numbers 'nums' find the subsets with sum
14+
equal to 'max_sum'
15+
16+
>>> generate_sum_of_subsets_solutions(nums=[3, 34, 4, 12, 5, 2], max_sum=9)
17+
[[3, 4, 2], [4, 5]]
18+
>>> generate_sum_of_subsets_solutions(nums=[3, 34, 4, 12, 5, 2], max_sum=3)
19+
[[3]]
20+
>>> generate_sum_of_subsets_solutions(nums=[3, 34, 4, 12, 5, 2], max_sum=1)
21+
[]
22+
"""
1223

13-
def generate_sum_of_subsets_soln(nums: list[int], max_sum: int) -> list[list[int]]:
1424
result: list[list[int]] = []
1525
path: list[int] = []
1626
num_index = 0
@@ -34,7 +44,21 @@ def create_state_space_tree(
3444
This algorithm follows depth-fist-search and backtracks when the node is not
3545
branchable.
3646
47+
>>> path = []
48+
>>> result = []
49+
>>> create_state_space_tree(
50+
... nums=[1],
51+
... max_sum=1,
52+
... num_index=0,
53+
... path=path,
54+
... result=result,
55+
... remaining_nums_sum=1)
56+
>>> path
57+
[]
58+
>>> result
59+
[[1]]
3760
"""
61+
3862
if sum(path) > max_sum or (remaining_nums_sum + sum(path)) < max_sum:
3963
return
4064
if sum(path) == max_sum:
@@ -51,16 +75,7 @@ def create_state_space_tree(
5175
)
5276

5377

54-
"""
55-
remove the comment to take an input from the user
56-
57-
print("Enter the elements")
58-
nums = list(map(int, input().split()))
59-
print("Enter max_sum sum")
60-
max_sum = int(input())
78+
if __name__ == "__main__":
79+
import doctest
6180

62-
"""
63-
nums = [3, 34, 4, 12, 5, 2]
64-
max_sum = 9
65-
result = generate_sum_of_subsets_soln(nums, max_sum)
66-
print(*result)
81+
doctest.testmod()

data_structures/hashing/hash_map.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
VAL = TypeVar("VAL")
1717

1818

19-
@dataclass(frozen=True, slots=True)
19+
@dataclass(slots=True)
2020
class _Item(Generic[KEY, VAL]):
2121
key: KEY
2222
val: VAL
@@ -72,16 +72,17 @@ def _try_set(self, ind: int, key: KEY, val: VAL) -> bool:
7272
7373
If bucket is empty or key is the same, does insert and return True.
7474
75-
If bucket has another key or deleted placeholder,
76-
that means that we need to check next bucket.
75+
If bucket has another key that means that we need to check next bucket.
7776
"""
7877
stored = self._buckets[ind]
7978
if not stored:
79+
# A falsy item means that bucket was never used (None)
80+
# or was deleted (_deleted).
8081
self._buckets[ind] = _Item(key, val)
8182
self._len += 1
8283
return True
8384
elif stored.key == key:
84-
self._buckets[ind] = _Item(key, val)
85+
stored.val = val
8586
return True
8687
else:
8788
return False
@@ -228,6 +229,27 @@ def __delitem__(self, key: KEY) -> None:
228229
Traceback (most recent call last):
229230
...
230231
KeyError: 4
232+
233+
# Test resize down when sparse
234+
## Setup: resize up
235+
>>> hm = HashMap(initial_block_size=100, capacity_factor=0.75)
236+
>>> len(hm._buckets)
237+
100
238+
>>> for i in range(75):
239+
... hm[i] = i
240+
>>> len(hm._buckets)
241+
100
242+
>>> hm[75] = 75
243+
>>> len(hm._buckets)
244+
200
245+
246+
## Resize down
247+
>>> del hm[75]
248+
>>> len(hm._buckets)
249+
200
250+
>>> del hm[74]
251+
>>> len(hm._buckets)
252+
100
231253
"""
232254
for ind in self._iterate_buckets(key):
233255
item = self._buckets[ind]

dynamic_programming/matrix_chain_order.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@
55
Implementation of Matrix Chain Multiplication
66
Time Complexity: O(n^3)
77
Space Complexity: O(n^2)
8+
9+
Reference: https://en.wikipedia.org/wiki/Matrix_chain_multiplication
810
"""
911

1012

11-
def matrix_chain_order(array):
13+
def matrix_chain_order(array: list[int]) -> tuple[list[list[int]], list[list[int]]]:
14+
"""
15+
>>> matrix_chain_order([10, 30, 5])
16+
([[0, 0, 0], [0, 0, 1500], [0, 0, 0]], [[0, 0, 0], [0, 0, 1], [0, 0, 0]])
17+
"""
1218
n = len(array)
13-
matrix = [[0 for x in range(n)] for x in range(n)]
14-
sol = [[0 for x in range(n)] for x in range(n)]
19+
matrix = [[0 for _ in range(n)] for _ in range(n)]
20+
sol = [[0 for _ in range(n)] for _ in range(n)]
1521

1622
for chain_length in range(2, n):
1723
for a in range(1, n - chain_length + 1):
@@ -28,26 +34,33 @@ def matrix_chain_order(array):
2834
return matrix, sol
2935

3036

31-
# Print order of matrix with Ai as Matrix
32-
def print_optiomal_solution(optimal_solution, i, j):
37+
def print_optimal_solution(optimal_solution: list[list[int]], i: int, j: int):
38+
"""
39+
Print order of matrix with Ai as Matrix.
40+
"""
41+
3342
if i == j:
3443
print("A" + str(i), end=" ")
3544
else:
3645
print("(", end=" ")
37-
print_optiomal_solution(optimal_solution, i, optimal_solution[i][j])
38-
print_optiomal_solution(optimal_solution, optimal_solution[i][j] + 1, j)
46+
print_optimal_solution(optimal_solution, i, optimal_solution[i][j])
47+
print_optimal_solution(optimal_solution, optimal_solution[i][j] + 1, j)
3948
print(")", end=" ")
4049

4150

4251
def main():
52+
"""
53+
Size of matrix created from array [30, 35, 15, 5, 10, 20, 25] will be:
54+
30*35 35*15 15*5 5*10 10*20 20*25
55+
"""
56+
4357
array = [30, 35, 15, 5, 10, 20, 25]
4458
n = len(array)
45-
# Size of matrix created from above array will be
46-
# 30*35 35*15 15*5 5*10 10*20 20*25
59+
4760
matrix, optimal_solution = matrix_chain_order(array)
4861

4962
print("No. of Operation required: " + str(matrix[1][n - 1]))
50-
print_optiomal_solution(optimal_solution, 1, n - 1)
63+
print_optimal_solution(optimal_solution, 1, n - 1)
5164

5265

5366
if __name__ == "__main__":

0 commit comments

Comments
 (0)