Skip to content

Commit dbfa0e9

Browse files
Merge branch 'TheAlgorithms:master' into master
2 parents e76a176 + a8ad2db commit dbfa0e9

File tree

76 files changed

+1925
-713
lines changed

Some content is hidden

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

76 files changed

+1925
-713
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
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.6
19+
rev: v0.11.9
2020
hooks:
2121
- id: ruff
2222
- id: ruff-format

DIRECTORY.md

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,6 @@
128128
* [Vigenere Cipher](ciphers/vigenere_cipher.py)
129129
* [Xor Cipher](ciphers/xor_cipher.py)
130130

131-
## Compression
132-
* [Burrows Wheeler](compression/burrows_wheeler.py)
133-
* [Huffman](compression/huffman.py)
134-
* [Lempel Ziv](compression/lempel_ziv.py)
135-
* [Lempel Ziv Decompress](compression/lempel_ziv_decompress.py)
136-
* [Lz77](compression/lz77.py)
137-
* [Peak Signal To Noise Ratio](compression/peak_signal_to_noise_ratio.py)
138-
* [Run Length Encoding](compression/run_length_encoding.py)
139-
140131
## Computer Vision
141132
* [Cnn Classification](computer_vision/cnn_classification.py)
142133
* [Flip Augmentation](computer_vision/flip_augmentation.py)
@@ -181,6 +172,15 @@
181172
* [Volume Conversions](conversions/volume_conversions.py)
182173
* [Weight Conversion](conversions/weight_conversion.py)
183174

175+
## Data Compression
176+
* [Burrows Wheeler](data_compression/burrows_wheeler.py)
177+
* [Huffman](data_compression/huffman.py)
178+
* [Lempel Ziv](data_compression/lempel_ziv.py)
179+
* [Lempel Ziv Decompress](data_compression/lempel_ziv_decompress.py)
180+
* [Lz77](data_compression/lz77.py)
181+
* [Peak Signal To Noise Ratio](data_compression/peak_signal_to_noise_ratio.py)
182+
* [Run Length Encoding](data_compression/run_length_encoding.py)
183+
184184
## Data Structures
185185
* Arrays
186186
* [Equilibrium Index In Array](data_structures/arrays/equilibrium_index_in_array.py)
@@ -884,6 +884,7 @@
884884
* [Centripetal Force](physics/centripetal_force.py)
885885
* [Coulombs Law](physics/coulombs_law.py)
886886
* [Doppler Frequency](physics/doppler_frequency.py)
887+
* [Escape Velocity](physics/escape_velocity.py)
887888
* [Grahams Law](physics/grahams_law.py)
888889
* [Horizontal Projectile Motion](physics/horizontal_projectile_motion.py)
889890
* [Hubble Parameter](physics/hubble_parameter.py)
@@ -898,6 +899,7 @@
898899
* [N Body Simulation](physics/n_body_simulation.py)
899900
* [Newtons Law Of Gravitation](physics/newtons_law_of_gravitation.py)
900901
* [Newtons Second Law Of Motion](physics/newtons_second_law_of_motion.py)
902+
* [Orbital Transfer Work](physics/orbital_transfer_work.py)
901903
* [Period Of Pendulum](physics/period_of_pendulum.py)
902904
* [Photoelectric Effect](physics/photoelectric_effect.py)
903905
* [Potential Energy](physics/potential_energy.py)
@@ -1122,6 +1124,8 @@
11221124
* [Sol1](project_euler/problem_092/sol1.py)
11231125
* Problem 094
11241126
* [Sol1](project_euler/problem_094/sol1.py)
1127+
* Problem 095
1128+
* [Sol1](project_euler/problem_095/sol1.py)
11251129
* Problem 097
11261130
* [Sol1](project_euler/problem_097/sol1.py)
11271131
* Problem 099
@@ -1174,6 +1178,8 @@
11741178
* [Sol1](project_euler/problem_144/sol1.py)
11751179
* Problem 145
11761180
* [Sol1](project_euler/problem_145/sol1.py)
1181+
* Problem 164
1182+
* [Sol1](project_euler/problem_164/sol1.py)
11771183
* Problem 173
11781184
* [Sol1](project_euler/problem_173/sol1.py)
11791185
* Problem 174
@@ -1184,6 +1190,8 @@
11841190
* [Sol1](project_euler/problem_187/sol1.py)
11851191
* Problem 188
11861192
* [Sol1](project_euler/problem_188/sol1.py)
1193+
* Problem 190
1194+
* [Sol1](project_euler/problem_190/sol1.py)
11871195
* Problem 191
11881196
* [Sol1](project_euler/problem_191/sol1.py)
11891197
* Problem 203
@@ -1198,6 +1206,8 @@
11981206
* [Sol1](project_euler/problem_234/sol1.py)
11991207
* Problem 301
12001208
* [Sol1](project_euler/problem_301/sol1.py)
1209+
* Problem 345
1210+
* [Sol1](project_euler/problem_345/sol1.py)
12011211
* Problem 493
12021212
* [Sol1](project_euler/problem_493/sol1.py)
12031213
* Problem 551

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()

boolean_algebra/and_gate.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"""
2-
An AND Gate is a logic gate in boolean algebra which results to 1 (True) if both the
3-
inputs are 1, and 0 (False) otherwise.
2+
An AND Gate is a logic gate in boolean algebra which results to 1 (True) if all the
3+
inputs are 1 (True), and 0 (False) otherwise.
44
5-
Following is the truth table of an AND Gate:
5+
Following is the truth table of a Two Input AND Gate:
66
------------------------------
77
| Input 1 | Input 2 | Output |
88
------------------------------
@@ -12,7 +12,7 @@
1212
| 1 | 1 | 1 |
1313
------------------------------
1414
15-
Refer - https://www.geeksforgeeks.org/logic-gates-in-python/
15+
Refer - https://www.geeksforgeeks.org/logic-gates/
1616
"""
1717

1818

@@ -32,6 +32,18 @@ def and_gate(input_1: int, input_2: int) -> int:
3232
return int(input_1 and input_2)
3333

3434

35+
def n_input_and_gate(inputs: list[int]) -> int:
36+
"""
37+
Calculate AND of a list of input values
38+
39+
>>> n_input_and_gate([1, 0, 1, 1, 0])
40+
0
41+
>>> n_input_and_gate([1, 1, 1, 1, 1])
42+
1
43+
"""
44+
return int(all(inputs))
45+
46+
3547
if __name__ == "__main__":
3648
import doctest
3749

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)