Skip to content

Commit

Permalink
Merge branch 'master' into kibolho/missing_number
Browse files Browse the repository at this point in the history
  • Loading branch information
kibolho committed Oct 16, 2023
2 parents 1f3b829 + 69707bf commit a0a1fb4
Show file tree
Hide file tree
Showing 45 changed files with 1,833 additions and 325 deletions.
4 changes: 2 additions & 2 deletions .github/ISSUE_TEMPLATE/feature_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ body:
attributes:
value: >
Before requesting please search [existing issues](https://github.com/TheAlgorithms/Python/labels/enhancement).
Do not create issues to implement new algorithms as these will be closed.
Usage questions such as "How do I...?" belong on the
[Discord](https://discord.gg/c7MnfGFGa6) and will be closed.
- type: textarea
attributes:
label: "Feature description"
description: >
This could be new algorithms, data structures or improving any existing
implementations.
This could include new topics or improving any existing implementations.
validations:
required: true
24 changes: 22 additions & 2 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@

## Boolean Algebra
* [And Gate](boolean_algebra/and_gate.py)
* [Imply Gate](boolean_algebra/imply_gate.py)
* [Nand Gate](boolean_algebra/nand_gate.py)
* [Nimply Gate](boolean_algebra/nimply_gate.py)
* [Nor Gate](boolean_algebra/nor_gate.py)
* [Not Gate](boolean_algebra/not_gate.py)
* [Or Gate](boolean_algebra/or_gate.py)
Expand Down Expand Up @@ -119,9 +121,9 @@
* [Shuffled Shift Cipher](ciphers/shuffled_shift_cipher.py)
* [Simple Keyword Cypher](ciphers/simple_keyword_cypher.py)
* [Simple Substitution Cipher](ciphers/simple_substitution_cipher.py)
* [Trafid Cipher](ciphers/trafid_cipher.py)
* [Transposition Cipher](ciphers/transposition_cipher.py)
* [Transposition Cipher Encrypt Decrypt File](ciphers/transposition_cipher_encrypt_decrypt_file.py)
* [Trifid Cipher](ciphers/trifid_cipher.py)
* [Vigenere Cipher](ciphers/vigenere_cipher.py)
* [Xor Cipher](ciphers/xor_cipher.py)

Expand Down Expand Up @@ -174,10 +176,13 @@
## Data Structures
* Arrays
* [Equilibrium Index In Array](data_structures/arrays/equilibrium_index_in_array.py)
* [Find Triplets With 0 Sum](data_structures/arrays/find_triplets_with_0_sum.py)
* [Median Two Array](data_structures/arrays/median_two_array.py)
* [Pairs With Given Sum](data_structures/arrays/pairs_with_given_sum.py)
* [Permutations](data_structures/arrays/permutations.py)
* [Prefix Sum](data_structures/arrays/prefix_sum.py)
* [Product Sum](data_structures/arrays/product_sum.py)
* [Sparse Table](data_structures/arrays/sparse_table.py)
* Binary Tree
* [Avl Tree](data_structures/binary_tree/avl_tree.py)
* [Basic Binary Tree](data_structures/binary_tree/basic_binary_tree.py)
Expand Down Expand Up @@ -261,6 +266,7 @@
* [Postfix Evaluation](data_structures/stacks/postfix_evaluation.py)
* [Prefix Evaluation](data_structures/stacks/prefix_evaluation.py)
* [Stack](data_structures/stacks/stack.py)
* [Stack Using Two Queues](data_structures/stacks/stack_using_two_queues.py)
* [Stack With Doubly Linked List](data_structures/stacks/stack_with_doubly_linked_list.py)
* [Stack With Singly Linked List](data_structures/stacks/stack_with_singly_linked_list.py)
* [Stock Span Problem](data_structures/stacks/stock_span_problem.py)
Expand Down Expand Up @@ -335,6 +341,7 @@
* [Longest Increasing Subsequence O(Nlogn)](dynamic_programming/longest_increasing_subsequence_o(nlogn).py)
* [Longest Palindromic Subsequence](dynamic_programming/longest_palindromic_subsequence.py)
* [Longest Sub Array](dynamic_programming/longest_sub_array.py)
* [Matrix Chain Multiplication](dynamic_programming/matrix_chain_multiplication.py)
* [Matrix Chain Order](dynamic_programming/matrix_chain_order.py)
* [Max Non Adjacent Sum](dynamic_programming/max_non_adjacent_sum.py)
* [Max Product Subarray](dynamic_programming/max_product_subarray.py)
Expand All @@ -357,18 +364,21 @@
* [Trapped Water](dynamic_programming/trapped_water.py)
* [Tribonacci](dynamic_programming/tribonacci.py)
* [Viterbi](dynamic_programming/viterbi.py)
* [Wildcard Matching](dynamic_programming/wildcard_matching.py)
* [Word Break](dynamic_programming/word_break.py)

## Electronics
* [Apparent Power](electronics/apparent_power.py)
* [Builtin Voltage](electronics/builtin_voltage.py)
* [Carrier Concentration](electronics/carrier_concentration.py)
* [Charging Capacitor](electronics/charging_capacitor.py)
* [Charging Inductor](electronics/charging_inductor.py)
* [Circular Convolution](electronics/circular_convolution.py)
* [Coulombs Law](electronics/coulombs_law.py)
* [Electric Conductivity](electronics/electric_conductivity.py)
* [Electric Power](electronics/electric_power.py)
* [Electrical Impedance](electronics/electrical_impedance.py)
* [Ic 555 Timer](electronics/ic_555_timer.py)
* [Ind Reactance](electronics/ind_reactance.py)
* [Ohms Law](electronics/ohms_law.py)
* [Real And Reactive Power](electronics/real_and_reactive_power.py)
Expand All @@ -385,6 +395,7 @@

## Financial
* [Equated Monthly Installments](financial/equated_monthly_installments.py)
* [Exponential Moving Average](financial/exponential_moving_average.py)
* [Interest](financial/interest.py)
* [Present Value](financial/present_value.py)
* [Price Plus Tax](financial/price_plus_tax.py)
Expand Down Expand Up @@ -516,6 +527,7 @@
* [Simplex](linear_programming/simplex.py)

## Machine Learning
* [Apriori Algorithm](machine_learning/apriori_algorithm.py)
* [Astar](machine_learning/astar.py)
* [Data Transformations](machine_learning/data_transformations.py)
* [Decision Tree](machine_learning/decision_tree.py)
Expand Down Expand Up @@ -617,6 +629,7 @@
* [Is Ip V4 Address Valid](maths/is_ip_v4_address_valid.py)
* [Is Square Free](maths/is_square_free.py)
* [Jaccard Similarity](maths/jaccard_similarity.py)
* [Joint Probability Distribution](maths/joint_probability_distribution.py)
* [Juggler Sequence](maths/juggler_sequence.py)
* [Karatsuba](maths/karatsuba.py)
* [Krishnamurthy Number](maths/krishnamurthy_number.py)
Expand Down Expand Up @@ -671,6 +684,7 @@
* [Radix2 Fft](maths/radix2_fft.py)
* [Remove Digit](maths/remove_digit.py)
* [Runge Kutta](maths/runge_kutta.py)
* [Runge Kutta Fehlberg 45](maths/runge_kutta_fehlberg_45.py)
* [Segmented Sieve](maths/segmented_sieve.py)
* Series
* [Arithmetic](maths/series/arithmetic.py)
Expand All @@ -688,6 +702,7 @@
* [Sin](maths/sin.py)
* [Sock Merchant](maths/sock_merchant.py)
* [Softmax](maths/softmax.py)
* [Solovay Strassen Primality Test](maths/solovay_strassen_primality_test.py)
* [Square Root](maths/square_root.py)
* [Sum Of Arithmetic Series](maths/sum_of_arithmetic_series.py)
* [Sum Of Digits](maths/sum_of_digits.py)
Expand Down Expand Up @@ -728,6 +743,7 @@
* [Spiral Print](matrix/spiral_print.py)
* Tests
* [Test Matrix Operation](matrix/tests/test_matrix_operation.py)
* [Validate Sudoku Board](matrix/validate_sudoku_board.py)

## Networking Flow
* [Ford Fulkerson](networking_flow/ford_fulkerson.py)
Expand Down Expand Up @@ -781,7 +797,7 @@

## Physics
* [Altitude Pressure](physics/altitude_pressure.py)
* [Archimedes Principle](physics/archimedes_principle.py)
* [Archimedes Principle Of Buoyant Force](physics/archimedes_principle_of_buoyant_force.py)
* [Basic Orbital Capture](physics/basic_orbital_capture.py)
* [Casimir Effect](physics/casimir_effect.py)
* [Centripetal Force](physics/centripetal_force.py)
Expand All @@ -803,6 +819,7 @@
* [Rms Speed Of Molecule](physics/rms_speed_of_molecule.py)
* [Shear Stress](physics/shear_stress.py)
* [Speed Of Sound](physics/speed_of_sound.py)
* [Speeds Of Gas Molecules](physics/speeds_of_gas_molecules.py)

## Project Euler
* Problem 001
Expand Down Expand Up @@ -1106,6 +1123,7 @@
## Scheduling
* [First Come First Served](scheduling/first_come_first_served.py)
* [Highest Response Ratio Next](scheduling/highest_response_ratio_next.py)
* [Job Sequence With Deadline](scheduling/job_sequence_with_deadline.py)
* [Job Sequencing With Deadline](scheduling/job_sequencing_with_deadline.py)
* [Multi Level Feedback Queue](scheduling/multi_level_feedback_queue.py)
* [Non Preemptive Shortest Job First](scheduling/non_preemptive_shortest_job_first.py)
Expand Down Expand Up @@ -1193,6 +1211,7 @@
* [Capitalize](strings/capitalize.py)
* [Check Anagrams](strings/check_anagrams.py)
* [Credit Card Validator](strings/credit_card_validator.py)
* [Damerau Levenshtein Distance](strings/damerau_levenshtein_distance.py)
* [Detecting English Programmatically](strings/detecting_english_programmatically.py)
* [Dna](strings/dna.py)
* [Frequency Finder](strings/frequency_finder.py)
Expand Down Expand Up @@ -1225,6 +1244,7 @@
* [String Switch Case](strings/string_switch_case.py)
* [Strip](strings/strip.py)
* [Text Justification](strings/text_justification.py)
* [Title](strings/title.py)
* [Top K Frequent Words](strings/top_k_frequent_words.py)
* [Upper](strings/upper.py)
* [Wave](strings/wave.py)
Expand Down
8 changes: 6 additions & 2 deletions arithmetic_analysis/lu_decomposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,19 @@ def lower_upper_decomposition(table: np.ndarray) -> tuple[np.ndarray, np.ndarray

lower = np.zeros((rows, columns))
upper = np.zeros((rows, columns))

# in 'total', the necessary data is extracted through slices
# and the sum of the products is obtained.

for i in range(columns):
for j in range(i):
total = sum(lower[i][k] * upper[k][j] for k in range(j))
total = np.sum(lower[i, :i] * upper[:i, j])
if upper[j][j] == 0:
raise ArithmeticError("No LU decomposition exists")
lower[i][j] = (table[i][j] - total) / upper[j][j]
lower[i][i] = 1
for j in range(i, columns):
total = sum(lower[i][k] * upper[k][j] for k in range(j))
total = np.sum(lower[i, :i] * upper[:i, j])
upper[i][j] = table[i][j] - total
return lower, upper

Expand Down
42 changes: 20 additions & 22 deletions backtracking/power_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
The only solution is 2^2+3^2. Constraints: 1<=X<=1000, 2<=N<=10.
"""

from math import pow


def backtrack(
needed_sum: int,
Expand All @@ -19,25 +17,25 @@ def backtrack(
"""
>>> backtrack(13, 2, 1, 0, 0)
(0, 1)
>>> backtrack(100, 2, 1, 0, 0)
(0, 3)
>>> backtrack(100, 3, 1, 0, 0)
>>> backtrack(10, 2, 1, 0, 0)
(0, 1)
>>> backtrack(10, 3, 1, 0, 0)
(0, 0)
>>> backtrack(20, 2, 1, 0, 0)
(0, 1)
>>> backtrack(800, 2, 1, 0, 0)
(0, 561)
>>> backtrack(1000, 10, 1, 0, 0)
>>> backtrack(15, 10, 1, 0, 0)
(0, 0)
>>> backtrack(400, 2, 1, 0, 0)
(0, 55)
>>> backtrack(50, 1, 1, 0, 0)
(0, 3658)
>>> backtrack(16, 2, 1, 0, 0)
(0, 1)
>>> backtrack(20, 1, 1, 0, 0)
(0, 64)
"""
if current_sum == needed_sum:
# If the sum of the powers is equal to needed_sum, then we have a solution.
solutions_count += 1
return current_sum, solutions_count

i_to_n = int(pow(current_number, power))
i_to_n = current_number**power
if current_sum + i_to_n <= needed_sum:
# If the sum of the powers is less than needed_sum, then continue adding powers.
current_sum += i_to_n
Expand All @@ -57,17 +55,17 @@ def solve(needed_sum: int, power: int) -> int:
"""
>>> solve(13, 2)
1
>>> solve(100, 2)
3
>>> solve(100, 3)
>>> solve(10, 2)
1
>>> solve(800, 2)
561
>>> solve(1000, 10)
>>> solve(10, 3)
0
>>> solve(400, 2)
55
>>> solve(50, 1)
>>> solve(20, 2)
1
>>> solve(15, 10)
0
>>> solve(16, 2)
1
>>> solve(20, 1)
Traceback (most recent call last):
...
ValueError: Invalid input
Expand Down
18 changes: 13 additions & 5 deletions bit_manipulation/missing_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,22 @@ def find_missing_number(nums: list[int]) -> int:
Traceback (most recent call last):
...
ValueError: negative values not supported
>>> find_missing_number([1, 3, 4, 5, 6])
2
>>> find_missing_number([6, 5, 4, 2, 1])
3
>>> find_missing_number([6, 1, 5, 3, 4])
2
"""
n = len(nums)
missing_number = n
low = min(nums)
high = max(nums)
missing_number = high

for i in range(n):
if nums[i] < 0:
for i in range(low, high):
index = i - low
if nums[index] < 0:
raise ValueError("negative values not supported")
missing_number ^= i ^ nums[i]
missing_number ^= i ^ nums[index]

return missing_number

Expand Down
18 changes: 3 additions & 15 deletions boolean_algebra/and_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,7 @@ def and_gate(input_1: int, input_2: int) -> int:
return int((input_1, input_2).count(0) == 0)


def test_and_gate() -> None:
"""
Tests the and_gate function
"""
assert and_gate(0, 0) == 0
assert and_gate(0, 1) == 0
assert and_gate(1, 0) == 0
assert and_gate(1, 1) == 1


if __name__ == "__main__":
test_and_gate()
print(and_gate(1, 0))
print(and_gate(0, 0))
print(and_gate(0, 1))
print(and_gate(1, 1))
import doctest

doctest.testmod()
7 changes: 3 additions & 4 deletions boolean_algebra/imply_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def imply_gate(input_1: int, input_2: int) -> int:


if __name__ == "__main__":
print(imply_gate(0, 0))
print(imply_gate(0, 1))
print(imply_gate(1, 0))
print(imply_gate(1, 1))
import doctest

doctest.testmod()
17 changes: 3 additions & 14 deletions boolean_algebra/nand_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,7 @@ def nand_gate(input_1: int, input_2: int) -> int:
return int((input_1, input_2).count(0) != 0)


def test_nand_gate() -> None:
"""
Tests the nand_gate function
"""
assert nand_gate(0, 0) == 1
assert nand_gate(0, 1) == 1
assert nand_gate(1, 0) == 1
assert nand_gate(1, 1) == 0


if __name__ == "__main__":
print(nand_gate(0, 0))
print(nand_gate(0, 1))
print(nand_gate(1, 0))
print(nand_gate(1, 1))
import doctest

doctest.testmod()
7 changes: 3 additions & 4 deletions boolean_algebra/nimply_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def nimply_gate(input_1: int, input_2: int) -> int:


if __name__ == "__main__":
print(nimply_gate(0, 0))
print(nimply_gate(0, 1))
print(nimply_gate(1, 0))
print(nimply_gate(1, 1))
import doctest

doctest.testmod()
13 changes: 3 additions & 10 deletions boolean_algebra/not_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,7 @@ def not_gate(input_1: int) -> int:
return 1 if input_1 == 0 else 0


def test_not_gate() -> None:
"""
Tests the not_gate function
"""
assert not_gate(0) == 1
assert not_gate(1) == 0


if __name__ == "__main__":
print(not_gate(0))
print(not_gate(1))
import doctest

doctest.testmod()
Loading

0 comments on commit a0a1fb4

Please sign in to comment.