Skip to content

Commit a0a1fb4

Browse files
committed
Merge branch 'master' into kibolho/missing_number
2 parents 1f3b829 + 69707bf commit a0a1fb4

Some content is hidden

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

45 files changed

+1833
-325
lines changed

Diff for: .github/ISSUE_TEMPLATE/feature_request.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ body:
66
attributes:
77
value: >
88
Before requesting please search [existing issues](https://github.com/TheAlgorithms/Python/labels/enhancement).
9+
Do not create issues to implement new algorithms as these will be closed.
910
Usage questions such as "How do I...?" belong on the
1011
[Discord](https://discord.gg/c7MnfGFGa6) and will be closed.
1112
1213
- type: textarea
1314
attributes:
1415
label: "Feature description"
1516
description: >
16-
This could be new algorithms, data structures or improving any existing
17-
implementations.
17+
This could include new topics or improving any existing implementations.
1818
validations:
1919
required: true

Diff for: DIRECTORY.md

+22-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@
6363

6464
## Boolean Algebra
6565
* [And Gate](boolean_algebra/and_gate.py)
66+
* [Imply Gate](boolean_algebra/imply_gate.py)
6667
* [Nand Gate](boolean_algebra/nand_gate.py)
68+
* [Nimply Gate](boolean_algebra/nimply_gate.py)
6769
* [Nor Gate](boolean_algebra/nor_gate.py)
6870
* [Not Gate](boolean_algebra/not_gate.py)
6971
* [Or Gate](boolean_algebra/or_gate.py)
@@ -119,9 +121,9 @@
119121
* [Shuffled Shift Cipher](ciphers/shuffled_shift_cipher.py)
120122
* [Simple Keyword Cypher](ciphers/simple_keyword_cypher.py)
121123
* [Simple Substitution Cipher](ciphers/simple_substitution_cipher.py)
122-
* [Trafid Cipher](ciphers/trafid_cipher.py)
123124
* [Transposition Cipher](ciphers/transposition_cipher.py)
124125
* [Transposition Cipher Encrypt Decrypt File](ciphers/transposition_cipher_encrypt_decrypt_file.py)
126+
* [Trifid Cipher](ciphers/trifid_cipher.py)
125127
* [Vigenere Cipher](ciphers/vigenere_cipher.py)
126128
* [Xor Cipher](ciphers/xor_cipher.py)
127129

@@ -174,10 +176,13 @@
174176
## Data Structures
175177
* Arrays
176178
* [Equilibrium Index In Array](data_structures/arrays/equilibrium_index_in_array.py)
179+
* [Find Triplets With 0 Sum](data_structures/arrays/find_triplets_with_0_sum.py)
177180
* [Median Two Array](data_structures/arrays/median_two_array.py)
181+
* [Pairs With Given Sum](data_structures/arrays/pairs_with_given_sum.py)
178182
* [Permutations](data_structures/arrays/permutations.py)
179183
* [Prefix Sum](data_structures/arrays/prefix_sum.py)
180184
* [Product Sum](data_structures/arrays/product_sum.py)
185+
* [Sparse Table](data_structures/arrays/sparse_table.py)
181186
* Binary Tree
182187
* [Avl Tree](data_structures/binary_tree/avl_tree.py)
183188
* [Basic Binary Tree](data_structures/binary_tree/basic_binary_tree.py)
@@ -261,6 +266,7 @@
261266
* [Postfix Evaluation](data_structures/stacks/postfix_evaluation.py)
262267
* [Prefix Evaluation](data_structures/stacks/prefix_evaluation.py)
263268
* [Stack](data_structures/stacks/stack.py)
269+
* [Stack Using Two Queues](data_structures/stacks/stack_using_two_queues.py)
264270
* [Stack With Doubly Linked List](data_structures/stacks/stack_with_doubly_linked_list.py)
265271
* [Stack With Singly Linked List](data_structures/stacks/stack_with_singly_linked_list.py)
266272
* [Stock Span Problem](data_structures/stacks/stock_span_problem.py)
@@ -335,6 +341,7 @@
335341
* [Longest Increasing Subsequence O(Nlogn)](dynamic_programming/longest_increasing_subsequence_o(nlogn).py)
336342
* [Longest Palindromic Subsequence](dynamic_programming/longest_palindromic_subsequence.py)
337343
* [Longest Sub Array](dynamic_programming/longest_sub_array.py)
344+
* [Matrix Chain Multiplication](dynamic_programming/matrix_chain_multiplication.py)
338345
* [Matrix Chain Order](dynamic_programming/matrix_chain_order.py)
339346
* [Max Non Adjacent Sum](dynamic_programming/max_non_adjacent_sum.py)
340347
* [Max Product Subarray](dynamic_programming/max_product_subarray.py)
@@ -357,18 +364,21 @@
357364
* [Trapped Water](dynamic_programming/trapped_water.py)
358365
* [Tribonacci](dynamic_programming/tribonacci.py)
359366
* [Viterbi](dynamic_programming/viterbi.py)
367+
* [Wildcard Matching](dynamic_programming/wildcard_matching.py)
360368
* [Word Break](dynamic_programming/word_break.py)
361369

362370
## Electronics
363371
* [Apparent Power](electronics/apparent_power.py)
364372
* [Builtin Voltage](electronics/builtin_voltage.py)
365373
* [Carrier Concentration](electronics/carrier_concentration.py)
366374
* [Charging Capacitor](electronics/charging_capacitor.py)
375+
* [Charging Inductor](electronics/charging_inductor.py)
367376
* [Circular Convolution](electronics/circular_convolution.py)
368377
* [Coulombs Law](electronics/coulombs_law.py)
369378
* [Electric Conductivity](electronics/electric_conductivity.py)
370379
* [Electric Power](electronics/electric_power.py)
371380
* [Electrical Impedance](electronics/electrical_impedance.py)
381+
* [Ic 555 Timer](electronics/ic_555_timer.py)
372382
* [Ind Reactance](electronics/ind_reactance.py)
373383
* [Ohms Law](electronics/ohms_law.py)
374384
* [Real And Reactive Power](electronics/real_and_reactive_power.py)
@@ -385,6 +395,7 @@
385395

386396
## Financial
387397
* [Equated Monthly Installments](financial/equated_monthly_installments.py)
398+
* [Exponential Moving Average](financial/exponential_moving_average.py)
388399
* [Interest](financial/interest.py)
389400
* [Present Value](financial/present_value.py)
390401
* [Price Plus Tax](financial/price_plus_tax.py)
@@ -516,6 +527,7 @@
516527
* [Simplex](linear_programming/simplex.py)
517528

518529
## Machine Learning
530+
* [Apriori Algorithm](machine_learning/apriori_algorithm.py)
519531
* [Astar](machine_learning/astar.py)
520532
* [Data Transformations](machine_learning/data_transformations.py)
521533
* [Decision Tree](machine_learning/decision_tree.py)
@@ -617,6 +629,7 @@
617629
* [Is Ip V4 Address Valid](maths/is_ip_v4_address_valid.py)
618630
* [Is Square Free](maths/is_square_free.py)
619631
* [Jaccard Similarity](maths/jaccard_similarity.py)
632+
* [Joint Probability Distribution](maths/joint_probability_distribution.py)
620633
* [Juggler Sequence](maths/juggler_sequence.py)
621634
* [Karatsuba](maths/karatsuba.py)
622635
* [Krishnamurthy Number](maths/krishnamurthy_number.py)
@@ -671,6 +684,7 @@
671684
* [Radix2 Fft](maths/radix2_fft.py)
672685
* [Remove Digit](maths/remove_digit.py)
673686
* [Runge Kutta](maths/runge_kutta.py)
687+
* [Runge Kutta Fehlberg 45](maths/runge_kutta_fehlberg_45.py)
674688
* [Segmented Sieve](maths/segmented_sieve.py)
675689
* Series
676690
* [Arithmetic](maths/series/arithmetic.py)
@@ -688,6 +702,7 @@
688702
* [Sin](maths/sin.py)
689703
* [Sock Merchant](maths/sock_merchant.py)
690704
* [Softmax](maths/softmax.py)
705+
* [Solovay Strassen Primality Test](maths/solovay_strassen_primality_test.py)
691706
* [Square Root](maths/square_root.py)
692707
* [Sum Of Arithmetic Series](maths/sum_of_arithmetic_series.py)
693708
* [Sum Of Digits](maths/sum_of_digits.py)
@@ -728,6 +743,7 @@
728743
* [Spiral Print](matrix/spiral_print.py)
729744
* Tests
730745
* [Test Matrix Operation](matrix/tests/test_matrix_operation.py)
746+
* [Validate Sudoku Board](matrix/validate_sudoku_board.py)
731747

732748
## Networking Flow
733749
* [Ford Fulkerson](networking_flow/ford_fulkerson.py)
@@ -781,7 +797,7 @@
781797

782798
## Physics
783799
* [Altitude Pressure](physics/altitude_pressure.py)
784-
* [Archimedes Principle](physics/archimedes_principle.py)
800+
* [Archimedes Principle Of Buoyant Force](physics/archimedes_principle_of_buoyant_force.py)
785801
* [Basic Orbital Capture](physics/basic_orbital_capture.py)
786802
* [Casimir Effect](physics/casimir_effect.py)
787803
* [Centripetal Force](physics/centripetal_force.py)
@@ -803,6 +819,7 @@
803819
* [Rms Speed Of Molecule](physics/rms_speed_of_molecule.py)
804820
* [Shear Stress](physics/shear_stress.py)
805821
* [Speed Of Sound](physics/speed_of_sound.py)
822+
* [Speeds Of Gas Molecules](physics/speeds_of_gas_molecules.py)
806823

807824
## Project Euler
808825
* Problem 001
@@ -1106,6 +1123,7 @@
11061123
## Scheduling
11071124
* [First Come First Served](scheduling/first_come_first_served.py)
11081125
* [Highest Response Ratio Next](scheduling/highest_response_ratio_next.py)
1126+
* [Job Sequence With Deadline](scheduling/job_sequence_with_deadline.py)
11091127
* [Job Sequencing With Deadline](scheduling/job_sequencing_with_deadline.py)
11101128
* [Multi Level Feedback Queue](scheduling/multi_level_feedback_queue.py)
11111129
* [Non Preemptive Shortest Job First](scheduling/non_preemptive_shortest_job_first.py)
@@ -1193,6 +1211,7 @@
11931211
* [Capitalize](strings/capitalize.py)
11941212
* [Check Anagrams](strings/check_anagrams.py)
11951213
* [Credit Card Validator](strings/credit_card_validator.py)
1214+
* [Damerau Levenshtein Distance](strings/damerau_levenshtein_distance.py)
11961215
* [Detecting English Programmatically](strings/detecting_english_programmatically.py)
11971216
* [Dna](strings/dna.py)
11981217
* [Frequency Finder](strings/frequency_finder.py)
@@ -1225,6 +1244,7 @@
12251244
* [String Switch Case](strings/string_switch_case.py)
12261245
* [Strip](strings/strip.py)
12271246
* [Text Justification](strings/text_justification.py)
1247+
* [Title](strings/title.py)
12281248
* [Top K Frequent Words](strings/top_k_frequent_words.py)
12291249
* [Upper](strings/upper.py)
12301250
* [Wave](strings/wave.py)

Diff for: arithmetic_analysis/lu_decomposition.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,19 @@ def lower_upper_decomposition(table: np.ndarray) -> tuple[np.ndarray, np.ndarray
8888

8989
lower = np.zeros((rows, columns))
9090
upper = np.zeros((rows, columns))
91+
92+
# in 'total', the necessary data is extracted through slices
93+
# and the sum of the products is obtained.
94+
9195
for i in range(columns):
9296
for j in range(i):
93-
total = sum(lower[i][k] * upper[k][j] for k in range(j))
97+
total = np.sum(lower[i, :i] * upper[:i, j])
9498
if upper[j][j] == 0:
9599
raise ArithmeticError("No LU decomposition exists")
96100
lower[i][j] = (table[i][j] - total) / upper[j][j]
97101
lower[i][i] = 1
98102
for j in range(i, columns):
99-
total = sum(lower[i][k] * upper[k][j] for k in range(j))
103+
total = np.sum(lower[i, :i] * upper[:i, j])
100104
upper[i][j] = table[i][j] - total
101105
return lower, upper
102106

Diff for: backtracking/power_sum.py

+20-22
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
The only solution is 2^2+3^2. Constraints: 1<=X<=1000, 2<=N<=10.
77
"""
88

9-
from math import pow
10-
119

1210
def backtrack(
1311
needed_sum: int,
@@ -19,25 +17,25 @@ def backtrack(
1917
"""
2018
>>> backtrack(13, 2, 1, 0, 0)
2119
(0, 1)
22-
>>> backtrack(100, 2, 1, 0, 0)
23-
(0, 3)
24-
>>> backtrack(100, 3, 1, 0, 0)
20+
>>> backtrack(10, 2, 1, 0, 0)
21+
(0, 1)
22+
>>> backtrack(10, 3, 1, 0, 0)
23+
(0, 0)
24+
>>> backtrack(20, 2, 1, 0, 0)
2525
(0, 1)
26-
>>> backtrack(800, 2, 1, 0, 0)
27-
(0, 561)
28-
>>> backtrack(1000, 10, 1, 0, 0)
26+
>>> backtrack(15, 10, 1, 0, 0)
2927
(0, 0)
30-
>>> backtrack(400, 2, 1, 0, 0)
31-
(0, 55)
32-
>>> backtrack(50, 1, 1, 0, 0)
33-
(0, 3658)
28+
>>> backtrack(16, 2, 1, 0, 0)
29+
(0, 1)
30+
>>> backtrack(20, 1, 1, 0, 0)
31+
(0, 64)
3432
"""
3533
if current_sum == needed_sum:
3634
# If the sum of the powers is equal to needed_sum, then we have a solution.
3735
solutions_count += 1
3836
return current_sum, solutions_count
3937

40-
i_to_n = int(pow(current_number, power))
38+
i_to_n = current_number**power
4139
if current_sum + i_to_n <= needed_sum:
4240
# If the sum of the powers is less than needed_sum, then continue adding powers.
4341
current_sum += i_to_n
@@ -57,17 +55,17 @@ def solve(needed_sum: int, power: int) -> int:
5755
"""
5856
>>> solve(13, 2)
5957
1
60-
>>> solve(100, 2)
61-
3
62-
>>> solve(100, 3)
58+
>>> solve(10, 2)
6359
1
64-
>>> solve(800, 2)
65-
561
66-
>>> solve(1000, 10)
60+
>>> solve(10, 3)
6761
0
68-
>>> solve(400, 2)
69-
55
70-
>>> solve(50, 1)
62+
>>> solve(20, 2)
63+
1
64+
>>> solve(15, 10)
65+
0
66+
>>> solve(16, 2)
67+
1
68+
>>> solve(20, 1)
7169
Traceback (most recent call last):
7270
...
7371
ValueError: Invalid input

Diff for: bit_manipulation/missing_number.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,22 @@ def find_missing_number(nums: list[int]) -> int:
1717
Traceback (most recent call last):
1818
...
1919
ValueError: negative values not supported
20+
>>> find_missing_number([1, 3, 4, 5, 6])
21+
2
22+
>>> find_missing_number([6, 5, 4, 2, 1])
23+
3
24+
>>> find_missing_number([6, 1, 5, 3, 4])
25+
2
2026
"""
21-
n = len(nums)
22-
missing_number = n
27+
low = min(nums)
28+
high = max(nums)
29+
missing_number = high
2330

24-
for i in range(n):
25-
if nums[i] < 0:
31+
for i in range(low, high):
32+
index = i - low
33+
if nums[index] < 0:
2634
raise ValueError("negative values not supported")
27-
missing_number ^= i ^ nums[i]
35+
missing_number ^= i ^ nums[index]
2836

2937
return missing_number
3038

Diff for: boolean_algebra/and_gate.py

+3-15
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,7 @@ def and_gate(input_1: int, input_2: int) -> int:
3232
return int((input_1, input_2).count(0) == 0)
3333

3434

35-
def test_and_gate() -> None:
36-
"""
37-
Tests the and_gate function
38-
"""
39-
assert and_gate(0, 0) == 0
40-
assert and_gate(0, 1) == 0
41-
assert and_gate(1, 0) == 0
42-
assert and_gate(1, 1) == 1
43-
44-
4535
if __name__ == "__main__":
46-
test_and_gate()
47-
print(and_gate(1, 0))
48-
print(and_gate(0, 0))
49-
print(and_gate(0, 1))
50-
print(and_gate(1, 1))
36+
import doctest
37+
38+
doctest.testmod()

Diff for: boolean_algebra/imply_gate.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ def imply_gate(input_1: int, input_2: int) -> int:
3434

3535

3636
if __name__ == "__main__":
37-
print(imply_gate(0, 0))
38-
print(imply_gate(0, 1))
39-
print(imply_gate(1, 0))
40-
print(imply_gate(1, 1))
37+
import doctest
38+
39+
doctest.testmod()

Diff for: boolean_algebra/nand_gate.py

+3-14
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,7 @@ def nand_gate(input_1: int, input_2: int) -> int:
3030
return int((input_1, input_2).count(0) != 0)
3131

3232

33-
def test_nand_gate() -> None:
34-
"""
35-
Tests the nand_gate function
36-
"""
37-
assert nand_gate(0, 0) == 1
38-
assert nand_gate(0, 1) == 1
39-
assert nand_gate(1, 0) == 1
40-
assert nand_gate(1, 1) == 0
41-
42-
4333
if __name__ == "__main__":
44-
print(nand_gate(0, 0))
45-
print(nand_gate(0, 1))
46-
print(nand_gate(1, 0))
47-
print(nand_gate(1, 1))
34+
import doctest
35+
36+
doctest.testmod()

Diff for: boolean_algebra/nimply_gate.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ def nimply_gate(input_1: int, input_2: int) -> int:
3434

3535

3636
if __name__ == "__main__":
37-
print(nimply_gate(0, 0))
38-
print(nimply_gate(0, 1))
39-
print(nimply_gate(1, 0))
40-
print(nimply_gate(1, 1))
37+
import doctest
38+
39+
doctest.testmod()

Diff for: boolean_algebra/not_gate.py

+3-10
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,7 @@ def not_gate(input_1: int) -> int:
2424
return 1 if input_1 == 0 else 0
2525

2626

27-
def test_not_gate() -> None:
28-
"""
29-
Tests the not_gate function
30-
"""
31-
assert not_gate(0) == 1
32-
assert not_gate(1) == 0
33-
34-
3527
if __name__ == "__main__":
36-
print(not_gate(0))
37-
print(not_gate(1))
28+
import doctest
29+
30+
doctest.testmod()

0 commit comments

Comments
 (0)