Skip to content

Commit d392e21

Browse files
committed
Merge remote-tracking branch 'origin/master' into viterbi
2 parents b710895 + 717f0e4 commit d392e21

25 files changed

+887
-80
lines changed

.pre-commit-config.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ repos:
2626
- --profile=black
2727

2828
- repo: https://github.com/asottile/pyupgrade
29-
rev: v3.0.0
29+
rev: v3.1.0
3030
hooks:
3131
- id: pyupgrade
3232
args:
@@ -55,14 +55,14 @@ repos:
5555
additional_dependencies: [types-requests]
5656

5757
- repo: https://github.com/codespell-project/codespell
58-
rev: v2.2.1
58+
rev: v2.2.2
5959
hooks:
6060
- id: codespell
6161
args:
6262
- --ignore-words-list=ans,crate,damon,fo,followings,hist,iff,mater,secant,som,sur,tim,zar
63-
- --skip="./.*,./strings/dictionary.txt,./strings/words.txt,./project_euler/problem_022/p022_names.txt"
6463
exclude: |
6564
(?x)^(
65+
ciphers/prehistoric_men.txt |
6666
strings/dictionary.txt |
6767
strings/words.txt |
6868
project_euler/problem_022/p022_names.txt

DIRECTORY.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@
2323
* [All Permutations](backtracking/all_permutations.py)
2424
* [All Subsequences](backtracking/all_subsequences.py)
2525
* [Coloring](backtracking/coloring.py)
26+
* [Combination Sum](backtracking/combination_sum.py)
2627
* [Hamiltonian Cycle](backtracking/hamiltonian_cycle.py)
2728
* [Knight Tour](backtracking/knight_tour.py)
2829
* [Minimax](backtracking/minimax.py)
30+
* [Minmax](backtracking/minmax.py)
2931
* [N Queens](backtracking/n_queens.py)
3032
* [N Queens Math](backtracking/n_queens_math.py)
3133
* [Rat In Maze](backtracking/rat_in_maze.py)
@@ -156,6 +158,7 @@
156158
* [Binary Tree Mirror](data_structures/binary_tree/binary_tree_mirror.py)
157159
* [Binary Tree Node Sum](data_structures/binary_tree/binary_tree_node_sum.py)
158160
* [Binary Tree Traversals](data_structures/binary_tree/binary_tree_traversals.py)
161+
* [Diff Views Of Binary Tree](data_structures/binary_tree/diff_views_of_binary_tree.py)
159162
* [Fenwick Tree](data_structures/binary_tree/fenwick_tree.py)
160163
* [Inorder Tree Traversal 2022](data_structures/binary_tree/inorder_tree_traversal_2022.py)
161164
* [Lazy Segment Tree](data_structures/binary_tree/lazy_segment_tree.py)
@@ -512,6 +515,7 @@
512515
* [Gamma](maths/gamma.py)
513516
* [Gamma Recursive](maths/gamma_recursive.py)
514517
* [Gaussian](maths/gaussian.py)
518+
* [Gaussian Error Linear Unit](maths/gaussian_error_linear_unit.py)
515519
* [Greatest Common Divisor](maths/greatest_common_divisor.py)
516520
* [Greedy Coin Change](maths/greedy_coin_change.py)
517521
* [Hamming Numbers](maths/hamming_numbers.py)
@@ -600,6 +604,7 @@
600604
* [Inverse Of Matrix](matrix/inverse_of_matrix.py)
601605
* [Matrix Class](matrix/matrix_class.py)
602606
* [Matrix Operation](matrix/matrix_operation.py)
607+
* [Max Area Of Island](matrix/max_area_of_island.py)
603608
* [Nth Fibonacci Using Matrix Exponentiation](matrix/nth_fibonacci_using_matrix_exponentiation.py)
604609
* [Rotate Matrix](matrix/rotate_matrix.py)
605610
* [Searching In Sorted Matrix](matrix/searching_in_sorted_matrix.py)
@@ -642,6 +647,7 @@
642647
* [Tower Of Hanoi](other/tower_of_hanoi.py)
643648

644649
## Physics
650+
* [Casimir Effect](physics/casimir_effect.py)
645651
* [Horizontal Projectile Motion](physics/horizontal_projectile_motion.py)
646652
* [Lorentz Transformation Four Vector](physics/lorentz_transformation_four_vector.py)
647653
* [N Body Simulation](physics/n_body_simulation.py)
@@ -928,6 +934,7 @@
928934
* [Deutsch Jozsa](quantum/deutsch_jozsa.py)
929935
* [Half Adder](quantum/half_adder.py)
930936
* [Not Gate](quantum/not_gate.py)
937+
* [Q Full Adder](quantum/q_full_adder.py)
931938
* [Quantum Entanglement](quantum/quantum_entanglement.py)
932939
* [Ripple Adder Classic](quantum/ripple_adder_classic.py)
933940
* [Single Qubit Measure](quantum/single_qubit_measure.py)

backtracking/combination_sum.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
"""
2+
In the Combination Sum problem, we are given a list consisting of distinct integers.
3+
We need to find all the combinations whose sum equals to target given.
4+
We can use an element more than one.
5+
6+
Time complexity(Average Case): O(n!)
7+
8+
Constraints:
9+
1 <= candidates.length <= 30
10+
2 <= candidates[i] <= 40
11+
All elements of candidates are distinct.
12+
1 <= target <= 40
13+
"""
14+
15+
16+
def backtrack(
17+
candidates: list, path: list, answer: list, target: int, previous_index: int
18+
) -> None:
19+
"""
20+
A recursive function that searches for possible combinations. Backtracks in case
21+
of a bigger current combination value than the target value.
22+
23+
Parameters
24+
----------
25+
previous_index: Last index from the previous search
26+
target: The value we need to obtain by summing our integers in the path list.
27+
answer: A list of possible combinations
28+
path: Current combination
29+
candidates: A list of integers we can use.
30+
"""
31+
if target == 0:
32+
answer.append(path.copy())
33+
else:
34+
for index in range(previous_index, len(candidates)):
35+
if target >= candidates[index]:
36+
path.append(candidates[index])
37+
backtrack(candidates, path, answer, target - candidates[index], index)
38+
path.pop(len(path) - 1)
39+
40+
41+
def combination_sum(candidates: list, target: int) -> list:
42+
"""
43+
>>> combination_sum([2, 3, 5], 8)
44+
[[2, 2, 2, 2], [2, 3, 3], [3, 5]]
45+
>>> combination_sum([2, 3, 6, 7], 7)
46+
[[2, 2, 3], [7]]
47+
>>> combination_sum([-8, 2.3, 0], 1)
48+
Traceback (most recent call last):
49+
...
50+
RecursionError: maximum recursion depth exceeded in comparison
51+
"""
52+
path = [] # type: list[int]
53+
answer = [] # type: list[int]
54+
backtrack(candidates, path, answer, target, 0)
55+
return answer
56+
57+
58+
def main() -> None:
59+
print(combination_sum([-8, 2.3, 0], 1))
60+
61+
62+
if __name__ == "__main__":
63+
import doctest
64+
65+
doctest.testmod()
66+
main()

backtracking/minmax.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
"""
2+
Minimax helps to achieve maximum score in a game by checking all possible moves.
3+
4+
"""
5+
from __future__ import annotations
6+
7+
import math
8+
9+
10+
def minimax(
11+
depth: int, node_index: int, is_max: bool, scores: list[int], height: float
12+
) -> int:
13+
"""
14+
depth is current depth in game tree.
15+
node_index is index of current node in scores[].
16+
scores[] contains the leaves of game tree.
17+
height is maximum height of game tree.
18+
19+
>>> scores = [90, 23, 6, 33, 21, 65, 123, 34423]
20+
>>> height = math.log(len(scores), 2)
21+
>>> minimax(0, 0, True, scores, height)
22+
65
23+
>>> minimax(-1, 0, True, scores, height)
24+
Traceback (most recent call last):
25+
...
26+
ValueError: Depth cannot be less than 0
27+
>>> minimax(0, 0, True, [], 2)
28+
Traceback (most recent call last):
29+
...
30+
ValueError: Scores cannot be empty
31+
>>> scores = [3, 5, 2, 9, 12, 5, 23, 23]
32+
>>> height = math.log(len(scores), 2)
33+
>>> minimax(0, 0, True, scores, height)
34+
12
35+
"""
36+
37+
if depth < 0:
38+
raise ValueError("Depth cannot be less than 0")
39+
40+
if not scores:
41+
raise ValueError("Scores cannot be empty")
42+
43+
if depth == height:
44+
return scores[node_index]
45+
46+
return (
47+
max(
48+
minimax(depth + 1, node_index * 2, False, scores, height),
49+
minimax(depth + 1, node_index * 2 + 1, False, scores, height),
50+
)
51+
if is_max
52+
else min(
53+
minimax(depth + 1, node_index * 2, True, scores, height),
54+
minimax(depth + 1, node_index * 2 + 1, True, scores, height),
55+
)
56+
)
57+
58+
59+
def main() -> None:
60+
scores = [90, 23, 6, 33, 21, 65, 123, 34423]
61+
height = math.log(len(scores), 2)
62+
print(f"Optimal value : {minimax(0, 0, True, scores, height)}")
63+
64+
65+
if __name__ == "__main__":
66+
import doctest
67+
68+
doctest.testmod()
69+
main()

boolean_algebra/norgate.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
""" A NOR Gate is a logic gate in boolean algebra which results to false(0)
2-
if any of the input is 1, and True(1) if both the inputs are 0.
3-
Following is the truth table of an NOR Gate:
1+
"""
2+
A NOR Gate is a logic gate in boolean algebra which results to false(0)
3+
if any of the input is 1, and True(1) if both the inputs are 0.
4+
Following is the truth table of a NOR Gate:
45
| Input 1 | Input 2 | Output |
56
| 0 | 0 | 1 |
67
| 0 | 1 | 0 |
78
| 1 | 0 | 0 |
89
| 1 | 1 | 0 |
10+
11+
Following is the code implementation of the NOR Gate
912
"""
10-
"""Following is the code implementation of the NOR Gate"""
1113

1214

1315
def nor_gate(input_1: int, input_2: int) -> int:
@@ -30,11 +32,11 @@ def nor_gate(input_1: int, input_2: int) -> int:
3032

3133
def main() -> None:
3234
print("Truth Table of NOR Gate:")
33-
print("| Input 1 |", " Input 2 |", " Output |")
34-
print("| 0 |", " 0 | ", nor_gate(0, 0), " |")
35-
print("| 0 |", " 1 | ", nor_gate(0, 1), " |")
36-
print("| 1 |", " 0 | ", nor_gate(1, 0), " |")
37-
print("| 1 |", " 1 | ", nor_gate(1, 1), " |")
35+
print("| Input 1 | Input 2 | Output |")
36+
print(f"| 0 | 0 | {nor_gate(0, 0)} |")
37+
print(f"| 0 | 1 | {nor_gate(0, 1)} |")
38+
print(f"| 1 | 0 | {nor_gate(1, 0)} |")
39+
print(f"| 1 | 1 | {nor_gate(1, 1)} |")
3840

3941

4042
if __name__ == "__main__":

computer_vision/horn_schunck.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from typing import SupportsIndex
1313

1414
import numpy as np
15-
from scipy.ndimage.filters import convolve
15+
from scipy.ndimage import convolve
1616

1717

1818
def warp(

0 commit comments

Comments
 (0)