Skip to content

Commit cb339ad

Browse files
Merge branch 'TheAlgorithms:master' into master
2 parents 4cb3a55 + ed19b1c commit cb339ad

File tree

62 files changed

+2547
-406
lines changed

Some content is hidden

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

62 files changed

+2547
-406
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ repos:
2626
- id: black
2727

2828
- repo: https://github.com/codespell-project/codespell
29-
rev: v2.2.5
29+
rev: v2.2.6
3030
hooks:
3131
- id: codespell
3232
additional_dependencies:

DIRECTORY.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* [Combination Sum](backtracking/combination_sum.py)
2626
* [Hamiltonian Cycle](backtracking/hamiltonian_cycle.py)
2727
* [Knight Tour](backtracking/knight_tour.py)
28+
* [Match Word Pattern](backtracking/match_word_pattern.py)
2829
* [Minimax](backtracking/minimax.py)
2930
* [N Queens](backtracking/n_queens.py)
3031
* [N Queens Math](backtracking/n_queens_math.py)
@@ -50,6 +51,7 @@
5051
* [Index Of Rightmost Set Bit](bit_manipulation/index_of_rightmost_set_bit.py)
5152
* [Is Even](bit_manipulation/is_even.py)
5253
* [Is Power Of Two](bit_manipulation/is_power_of_two.py)
54+
* [Largest Pow Of Two Le Num](bit_manipulation/largest_pow_of_two_le_num.py)
5355
* [Missing Number](bit_manipulation/missing_number.py)
5456
* [Numbers Different Signs](bit_manipulation/numbers_different_signs.py)
5557
* [Reverse Bits](bit_manipulation/reverse_bits.py)
@@ -198,6 +200,7 @@
198200
* [Red Black Tree](data_structures/binary_tree/red_black_tree.py)
199201
* [Segment Tree](data_structures/binary_tree/segment_tree.py)
200202
* [Segment Tree Other](data_structures/binary_tree/segment_tree_other.py)
203+
* [Symmetric Tree](data_structures/binary_tree/symmetric_tree.py)
201204
* [Treap](data_structures/binary_tree/treap.py)
202205
* [Wavelet Tree](data_structures/binary_tree/wavelet_tree.py)
203206
* Disjoint Set
@@ -276,6 +279,7 @@
276279
* [Convolve](digital_image_processing/filters/convolve.py)
277280
* [Gabor Filter](digital_image_processing/filters/gabor_filter.py)
278281
* [Gaussian Filter](digital_image_processing/filters/gaussian_filter.py)
282+
* [Laplacian Filter](digital_image_processing/filters/laplacian_filter.py)
279283
* [Local Binary Pattern](digital_image_processing/filters/local_binary_pattern.py)
280284
* [Median Filter](digital_image_processing/filters/median_filter.py)
281285
* [Sobel Filter](digital_image_processing/filters/sobel_filter.py)
@@ -322,6 +326,7 @@
322326
* [Integer Partition](dynamic_programming/integer_partition.py)
323327
* [Iterating Through Submasks](dynamic_programming/iterating_through_submasks.py)
324328
* [Knapsack](dynamic_programming/knapsack.py)
329+
* [Largest Divisible Subset](dynamic_programming/largest_divisible_subset.py)
325330
* [Longest Common Subsequence](dynamic_programming/longest_common_subsequence.py)
326331
* [Longest Common Substring](dynamic_programming/longest_common_substring.py)
327332
* [Longest Increasing Subsequence](dynamic_programming/longest_increasing_subsequence.py)
@@ -363,8 +368,10 @@
363368
* [Ind Reactance](electronics/ind_reactance.py)
364369
* [Ohms Law](electronics/ohms_law.py)
365370
* [Real And Reactive Power](electronics/real_and_reactive_power.py)
371+
* [Resistor Color Code](electronics/resistor_color_code.py)
366372
* [Resistor Equivalence](electronics/resistor_equivalence.py)
367373
* [Resonant Frequency](electronics/resonant_frequency.py)
374+
* [Wheatstone Bridge](electronics/wheatstone_bridge.py)
368375

369376
## File Transfer
370377
* [Receive File](file_transfer/receive_file.py)
@@ -413,6 +420,7 @@
413420
* [Check Bipartite Graph Dfs](graphs/check_bipartite_graph_dfs.py)
414421
* [Check Cycle](graphs/check_cycle.py)
415422
* [Connected Components](graphs/connected_components.py)
423+
* [Deep Clone Graph](graphs/deep_clone_graph.py)
416424
* [Depth First Search](graphs/depth_first_search.py)
417425
* [Depth First Search 2](graphs/depth_first_search_2.py)
418426
* [Dijkstra](graphs/dijkstra.py)
@@ -460,6 +468,7 @@
460468
## Greedy Methods
461469
* [Fractional Knapsack](greedy_methods/fractional_knapsack.py)
462470
* [Fractional Knapsack 2](greedy_methods/fractional_knapsack_2.py)
471+
* [Gas Station](greedy_methods/gas_station.py)
463472
* [Minimum Waiting Time](greedy_methods/minimum_waiting_time.py)
464473
* [Optimal Merge Pattern](greedy_methods/optimal_merge_pattern.py)
465474

@@ -542,11 +551,12 @@
542551
* [Average Median](maths/average_median.py)
543552
* [Average Mode](maths/average_mode.py)
544553
* [Bailey Borwein Plouffe](maths/bailey_borwein_plouffe.py)
554+
* [Base Neg2 Conversion](maths/base_neg2_conversion.py)
545555
* [Basic Maths](maths/basic_maths.py)
546556
* [Bell Numbers](maths/bell_numbers.py)
547557
* [Binary Exp Mod](maths/binary_exp_mod.py)
548558
* [Binary Exponentiation](maths/binary_exponentiation.py)
549-
* [Binary Exponentiation 3](maths/binary_exponentiation_3.py)
559+
* [Binary Exponentiation 2](maths/binary_exponentiation_2.py)
550560
* [Binary Multiplication](maths/binary_multiplication.py)
551561
* [Binomial Coefficient](maths/binomial_coefficient.py)
552562
* [Binomial Distribution](maths/binomial_distribution.py)
@@ -578,7 +588,6 @@
578588
* [Find Min](maths/find_min.py)
579589
* [Floor](maths/floor.py)
580590
* [Gamma](maths/gamma.py)
581-
* [Gamma Recursive](maths/gamma_recursive.py)
582591
* [Gaussian](maths/gaussian.py)
583592
* [Gaussian Error Linear Unit](maths/gaussian_error_linear_unit.py)
584593
* [Gcd Of N Numbers](maths/gcd_of_n_numbers.py)
@@ -657,7 +666,6 @@
657666
* [P Series](maths/series/p_series.py)
658667
* [Sieve Of Eratosthenes](maths/sieve_of_eratosthenes.py)
659668
* [Sigmoid](maths/sigmoid.py)
660-
* [Sigmoid Linear Unit](maths/sigmoid_linear_unit.py)
661669
* [Signum](maths/signum.py)
662670
* [Simpson Rule](maths/simpson_rule.py)
663671
* [Simultaneous Linear Equation Solver](maths/simultaneous_linear_equation_solver.py)
@@ -714,8 +722,10 @@
714722
* Activation Functions
715723
* [Exponential Linear Unit](neural_network/activation_functions/exponential_linear_unit.py)
716724
* [Leaky Rectified Linear Unit](neural_network/activation_functions/leaky_rectified_linear_unit.py)
725+
* [Mish](neural_network/activation_functions/mish.py)
717726
* [Rectified Linear Unit](neural_network/activation_functions/rectified_linear_unit.py)
718727
* [Scaled Exponential Linear Unit](neural_network/activation_functions/scaled_exponential_linear_unit.py)
728+
* [Sigmoid Linear Unit](neural_network/activation_functions/sigmoid_linear_unit.py)
719729
* [Back Propagation Neural Network](neural_network/back_propagation_neural_network.py)
720730
* [Convolution Neural Network](neural_network/convolution_neural_network.py)
721731
* [Perceptron](neural_network/perceptron.py)
@@ -738,6 +748,7 @@
738748
* [Linear Congruential Generator](other/linear_congruential_generator.py)
739749
* [Lru Cache](other/lru_cache.py)
740750
* [Magicdiamondpattern](other/magicdiamondpattern.py)
751+
* [Majority Vote Algorithm](other/majority_vote_algorithm.py)
741752
* [Maximum Subsequence](other/maximum_subsequence.py)
742753
* [Nested Brackets](other/nested_brackets.py)
743754
* [Number Container System](other/number_container_system.py)
@@ -1155,6 +1166,7 @@
11551166
* [Autocomplete Using Trie](strings/autocomplete_using_trie.py)
11561167
* [Barcode Validator](strings/barcode_validator.py)
11571168
* [Boyer Moore Search](strings/boyer_moore_search.py)
1169+
* [Camel Case To Snake Case](strings/camel_case_to_snake_case.py)
11581170
* [Can String Be Rearranged As Palindrome](strings/can_string_be_rearranged_as_palindrome.py)
11591171
* [Capitalize](strings/capitalize.py)
11601172
* [Check Anagrams](strings/check_anagrams.py)
@@ -1180,11 +1192,11 @@
11801192
* [Naive String Search](strings/naive_string_search.py)
11811193
* [Ngram](strings/ngram.py)
11821194
* [Palindrome](strings/palindrome.py)
1195+
* [Pig Latin](strings/pig_latin.py)
11831196
* [Prefix Function](strings/prefix_function.py)
11841197
* [Rabin Karp](strings/rabin_karp.py)
11851198
* [Remove Duplicate](strings/remove_duplicate.py)
11861199
* [Reverse Letters](strings/reverse_letters.py)
1187-
* [Reverse Long Words](strings/reverse_long_words.py)
11881200
* [Reverse Words](strings/reverse_words.py)
11891201
* [Snake Case To Camel Pascal Case](strings/snake_case_to_camel_pascal_case.py)
11901202
* [Split](strings/split.py)

backtracking/match_word_pattern.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
def match_word_pattern(pattern: str, input_string: str) -> bool:
2+
"""
3+
Determine if a given pattern matches a string using backtracking.
4+
5+
pattern: The pattern to match.
6+
input_string: The string to match against the pattern.
7+
return: True if the pattern matches the string, False otherwise.
8+
9+
>>> match_word_pattern("aba", "GraphTreesGraph")
10+
True
11+
12+
>>> match_word_pattern("xyx", "PythonRubyPython")
13+
True
14+
15+
>>> match_word_pattern("GG", "PythonJavaPython")
16+
False
17+
"""
18+
19+
def backtrack(pattern_index: int, str_index: int) -> bool:
20+
"""
21+
>>> backtrack(0, 0)
22+
True
23+
24+
>>> backtrack(0, 1)
25+
True
26+
27+
>>> backtrack(0, 4)
28+
False
29+
"""
30+
if pattern_index == len(pattern) and str_index == len(input_string):
31+
return True
32+
if pattern_index == len(pattern) or str_index == len(input_string):
33+
return False
34+
char = pattern[pattern_index]
35+
if char in pattern_map:
36+
mapped_str = pattern_map[char]
37+
if input_string.startswith(mapped_str, str_index):
38+
return backtrack(pattern_index + 1, str_index + len(mapped_str))
39+
else:
40+
return False
41+
for end in range(str_index + 1, len(input_string) + 1):
42+
substr = input_string[str_index:end]
43+
if substr in str_map:
44+
continue
45+
pattern_map[char] = substr
46+
str_map[substr] = char
47+
if backtrack(pattern_index + 1, end):
48+
return True
49+
del pattern_map[char]
50+
del str_map[substr]
51+
return False
52+
53+
pattern_map: dict[str, str] = {}
54+
str_map: dict[str, str] = {}
55+
return backtrack(0, 0)
56+
57+
58+
if __name__ == "__main__":
59+
import doctest
60+
61+
doctest.testmod()

bit_manipulation/power_of_4.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"""
2+
3+
Task:
4+
Given a positive int number. Return True if this number is power of 4
5+
or False otherwise.
6+
7+
Implementation notes: Use bit manipulation.
8+
For example if the number is the power of 2 it's bits representation:
9+
n = 0..100..00
10+
n - 1 = 0..011..11
11+
12+
n & (n - 1) - no intersections = 0
13+
If the number is a power of 4 then it should be a power of 2
14+
and the set bit should be at an odd position.
15+
"""
16+
17+
18+
def power_of_4(number: int) -> bool:
19+
"""
20+
Return True if this number is power of 4 or False otherwise.
21+
22+
>>> power_of_4(0)
23+
Traceback (most recent call last):
24+
...
25+
ValueError: number must be positive
26+
>>> power_of_4(1)
27+
True
28+
>>> power_of_4(2)
29+
False
30+
>>> power_of_4(4)
31+
True
32+
>>> power_of_4(6)
33+
False
34+
>>> power_of_4(8)
35+
False
36+
>>> power_of_4(17)
37+
False
38+
>>> power_of_4(64)
39+
True
40+
>>> power_of_4(-1)
41+
Traceback (most recent call last):
42+
...
43+
ValueError: number must be positive
44+
>>> power_of_4(1.2)
45+
Traceback (most recent call last):
46+
...
47+
TypeError: number must be an integer
48+
49+
"""
50+
if not isinstance(number, int):
51+
raise TypeError("number must be an integer")
52+
if number <= 0:
53+
raise ValueError("number must be positive")
54+
if number & (number - 1) == 0:
55+
c = 0
56+
while number:
57+
c += 1
58+
number >>= 1
59+
return c % 2 == 1
60+
else:
61+
return False
62+
63+
64+
if __name__ == "__main__":
65+
import doctest
66+
67+
doctest.testmod()

0 commit comments

Comments
 (0)