Skip to content

Commit 93943aa

Browse files
Merge branch 'TheAlgorithms:master' into master
2 parents 821615b + d0c54ac commit 93943aa

File tree

9 files changed

+615
-105
lines changed

9 files changed

+615
-105
lines changed

DIRECTORY.md

Lines changed: 13 additions & 1 deletion
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,6 +551,7 @@
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)
@@ -657,7 +667,6 @@
657667
* [P Series](maths/series/p_series.py)
658668
* [Sieve Of Eratosthenes](maths/sieve_of_eratosthenes.py)
659669
* [Sigmoid](maths/sigmoid.py)
660-
* [Sigmoid Linear Unit](maths/sigmoid_linear_unit.py)
661670
* [Signum](maths/signum.py)
662671
* [Simpson Rule](maths/simpson_rule.py)
663672
* [Simultaneous Linear Equation Solver](maths/simultaneous_linear_equation_solver.py)
@@ -716,6 +725,7 @@
716725
* [Leaky Rectified Linear Unit](neural_network/activation_functions/leaky_rectified_linear_unit.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)
@@ -1155,6 +1165,7 @@
11551165
* [Autocomplete Using Trie](strings/autocomplete_using_trie.py)
11561166
* [Barcode Validator](strings/barcode_validator.py)
11571167
* [Boyer Moore Search](strings/boyer_moore_search.py)
1168+
* [Camel Case To Snake Case](strings/camel_case_to_snake_case.py)
11581169
* [Can String Be Rearranged As Palindrome](strings/can_string_be_rearranged_as_palindrome.py)
11591170
* [Capitalize](strings/capitalize.py)
11601171
* [Check Anagrams](strings/check_anagrams.py)
@@ -1180,6 +1191,7 @@
11801191
* [Naive String Search](strings/naive_string_search.py)
11811192
* [Ngram](strings/ngram.py)
11821193
* [Palindrome](strings/palindrome.py)
1194+
* [Pig Latin](strings/pig_latin.py)
11831195
* [Prefix Function](strings/prefix_function.py)
11841196
* [Rabin Karp](strings/rabin_karp.py)
11851197
* [Remove Duplicate](strings/remove_duplicate.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()
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
"""
2+
Given the root of a binary tree, check whether it is a mirror of itself
3+
(i.e., symmetric around its center).
4+
5+
Leetcode reference: https://leetcode.com/problems/symmetric-tree/
6+
"""
7+
from __future__ import annotations
8+
9+
from dataclasses import dataclass
10+
11+
12+
@dataclass
13+
class Node:
14+
"""
15+
A Node has data variable and pointers to Nodes to its left and right.
16+
"""
17+
18+
data: int
19+
left: Node | None = None
20+
right: Node | None = None
21+
22+
23+
def make_symmetric_tree() -> Node:
24+
r"""
25+
Create a symmetric tree for testing.
26+
The tree looks like this:
27+
1
28+
/ \
29+
2 2
30+
/ \ / \
31+
3 4 4 3
32+
"""
33+
root = Node(1)
34+
root.left = Node(2)
35+
root.right = Node(2)
36+
root.left.left = Node(3)
37+
root.left.right = Node(4)
38+
root.right.left = Node(4)
39+
root.right.right = Node(3)
40+
return root
41+
42+
43+
def make_asymmetric_tree() -> Node:
44+
r"""
45+
Create a asymmetric tree for testing.
46+
The tree looks like this:
47+
1
48+
/ \
49+
2 2
50+
/ \ / \
51+
3 4 3 4
52+
"""
53+
root = Node(1)
54+
root.left = Node(2)
55+
root.right = Node(2)
56+
root.left.left = Node(3)
57+
root.left.right = Node(4)
58+
root.right.left = Node(3)
59+
root.right.right = Node(4)
60+
return root
61+
62+
63+
def is_symmetric_tree(tree: Node) -> bool:
64+
"""
65+
Test cases for is_symmetric_tree function
66+
>>> is_symmetric_tree(make_symmetric_tree())
67+
True
68+
>>> is_symmetric_tree(make_asymmetric_tree())
69+
False
70+
"""
71+
if tree:
72+
return is_mirror(tree.left, tree.right)
73+
return True # An empty tree is considered symmetric.
74+
75+
76+
def is_mirror(left: Node | None, right: Node | None) -> bool:
77+
"""
78+
>>> tree1 = make_symmetric_tree()
79+
>>> tree1.right.right = Node(3)
80+
>>> is_mirror(tree1.left, tree1.right)
81+
True
82+
>>> tree2 = make_asymmetric_tree()
83+
>>> is_mirror(tree2.left, tree2.right)
84+
False
85+
"""
86+
if left is None and right is None:
87+
# Both sides are empty, which is symmetric.
88+
return True
89+
if left is None or right is None:
90+
# One side is empty while the other is not, which is not symmetric.
91+
return False
92+
if left.data == right.data:
93+
# The values match, so check the subtree
94+
return is_mirror(left.left, right.right) and is_mirror(left.right, right.left)
95+
return False
96+
97+
98+
if __name__ == "__main__":
99+
from doctest import testmod
100+
101+
testmod()

0 commit comments

Comments
 (0)