mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-05-17 14:46:45 +00:00
Merge branch 'TheAlgorithms:master' into master
This commit is contained in:
commit
8d86c62d56
2
.github/workflows/build.yml
vendored
2
.github/workflows/build.yml
vendored
@ -20,7 +20,7 @@ jobs:
|
|||||||
key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }}
|
key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }}
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: |
|
run: |
|
||||||
python -m pip install --upgrade pip setuptools six wheel
|
python -m pip install --upgrade pip setuptools wheel
|
||||||
python -m pip install pytest-cov -r requirements.txt
|
python -m pip install pytest-cov -r requirements.txt
|
||||||
- name: Run tests
|
- name: Run tests
|
||||||
# TODO: #8818 Re-enable quantum tests
|
# TODO: #8818 Re-enable quantum tests
|
||||||
|
@ -22,6 +22,7 @@
|
|||||||
* [Rat In Maze](backtracking/rat_in_maze.py)
|
* [Rat In Maze](backtracking/rat_in_maze.py)
|
||||||
* [Sudoku](backtracking/sudoku.py)
|
* [Sudoku](backtracking/sudoku.py)
|
||||||
* [Sum Of Subsets](backtracking/sum_of_subsets.py)
|
* [Sum Of Subsets](backtracking/sum_of_subsets.py)
|
||||||
|
* [Word Ladder](backtracking/word_ladder.py)
|
||||||
* [Word Search](backtracking/word_search.py)
|
* [Word Search](backtracking/word_search.py)
|
||||||
|
|
||||||
## Bit Manipulation
|
## Bit Manipulation
|
||||||
|
100
backtracking/word_ladder.py
Normal file
100
backtracking/word_ladder.py
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
"""
|
||||||
|
Word Ladder is a classic problem in computer science.
|
||||||
|
The problem is to transform a start word into an end word
|
||||||
|
by changing one letter at a time.
|
||||||
|
Each intermediate word must be a valid word from a given list of words.
|
||||||
|
The goal is to find a transformation sequence
|
||||||
|
from the start word to the end word.
|
||||||
|
|
||||||
|
Wikipedia: https://en.wikipedia.org/wiki/Word_ladder
|
||||||
|
"""
|
||||||
|
|
||||||
|
import string
|
||||||
|
|
||||||
|
|
||||||
|
def backtrack(
|
||||||
|
current_word: str, path: list[str], end_word: str, word_set: set[str]
|
||||||
|
) -> list[str]:
|
||||||
|
"""
|
||||||
|
Helper function to perform backtracking to find the transformation
|
||||||
|
from the current_word to the end_word.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
current_word (str): The current word in the transformation sequence.
|
||||||
|
path (list[str]): The list of transformations from begin_word to current_word.
|
||||||
|
end_word (str): The target word for transformation.
|
||||||
|
word_set (set[str]): The set of valid words for transformation.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
list[str]: The list of transformations from begin_word to end_word.
|
||||||
|
Returns an empty list if there is no valid
|
||||||
|
transformation from current_word to end_word.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
>>> backtrack("hit", ["hit"], "cog", {"hot", "dot", "dog", "lot", "log", "cog"})
|
||||||
|
['hit', 'hot', 'dot', 'lot', 'log', 'cog']
|
||||||
|
|
||||||
|
>>> backtrack("hit", ["hit"], "cog", {"hot", "dot", "dog", "lot", "log"})
|
||||||
|
[]
|
||||||
|
|
||||||
|
>>> backtrack("lead", ["lead"], "gold", {"load", "goad", "gold", "lead", "lord"})
|
||||||
|
['lead', 'lead', 'load', 'goad', 'gold']
|
||||||
|
|
||||||
|
>>> backtrack("game", ["game"], "code", {"came", "cage", "code", "cade", "gave"})
|
||||||
|
['game', 'came', 'cade', 'code']
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Base case: If the current word is the end word, return the path
|
||||||
|
if current_word == end_word:
|
||||||
|
return path
|
||||||
|
|
||||||
|
# Try all possible single-letter transformations
|
||||||
|
for i in range(len(current_word)):
|
||||||
|
for c in string.ascii_lowercase: # Try changing each letter
|
||||||
|
transformed_word = current_word[:i] + c + current_word[i + 1 :]
|
||||||
|
if transformed_word in word_set:
|
||||||
|
word_set.remove(transformed_word)
|
||||||
|
# Recur with the new word added to the path
|
||||||
|
result = backtrack(
|
||||||
|
transformed_word, [*path, transformed_word], end_word, word_set
|
||||||
|
)
|
||||||
|
if result: # valid transformation found
|
||||||
|
return result
|
||||||
|
word_set.add(transformed_word) # backtrack
|
||||||
|
|
||||||
|
return [] # No valid transformation found
|
||||||
|
|
||||||
|
|
||||||
|
def word_ladder(begin_word: str, end_word: str, word_set: set[str]) -> list[str]:
|
||||||
|
"""
|
||||||
|
Solve the Word Ladder problem using Backtracking and return
|
||||||
|
the list of transformations from begin_word to end_word.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
begin_word (str): The word from which the transformation starts.
|
||||||
|
end_word (str): The target word for transformation.
|
||||||
|
word_list (list[str]): The list of valid words for transformation.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
list[str]: The list of transformations from begin_word to end_word.
|
||||||
|
Returns an empty list if there is no valid transformation.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
>>> word_ladder("hit", "cog", ["hot", "dot", "dog", "lot", "log", "cog"])
|
||||||
|
['hit', 'hot', 'dot', 'lot', 'log', 'cog']
|
||||||
|
|
||||||
|
>>> word_ladder("hit", "cog", ["hot", "dot", "dog", "lot", "log"])
|
||||||
|
[]
|
||||||
|
|
||||||
|
>>> word_ladder("lead", "gold", ["load", "goad", "gold", "lead", "lord"])
|
||||||
|
['lead', 'lead', 'load', 'goad', 'gold']
|
||||||
|
|
||||||
|
>>> word_ladder("game", "code", ["came", "cage", "code", "cade", "gave"])
|
||||||
|
['game', 'came', 'cade', 'code']
|
||||||
|
"""
|
||||||
|
|
||||||
|
if end_word not in word_set: # no valid transformation possible
|
||||||
|
return []
|
||||||
|
|
||||||
|
# Perform backtracking starting from the begin_word
|
||||||
|
return backtrack(begin_word, [begin_word], end_word, word_set)
|
@ -13,7 +13,21 @@ from dataclasses import dataclass
|
|||||||
@dataclass
|
@dataclass
|
||||||
class Node:
|
class Node:
|
||||||
"""
|
"""
|
||||||
A Node has data variable and pointers to Nodes to its left and right.
|
A Node represents an element of a binary tree, which contains:
|
||||||
|
|
||||||
|
Attributes:
|
||||||
|
data: The value stored in the node (int).
|
||||||
|
left: Pointer to the left child node (Node or None).
|
||||||
|
right: Pointer to the right child node (Node or None).
|
||||||
|
|
||||||
|
Example:
|
||||||
|
>>> node = Node(1, Node(2), Node(3))
|
||||||
|
>>> node.data
|
||||||
|
1
|
||||||
|
>>> node.left.data
|
||||||
|
2
|
||||||
|
>>> node.right.data
|
||||||
|
3
|
||||||
"""
|
"""
|
||||||
|
|
||||||
data: int
|
data: int
|
||||||
@ -24,12 +38,25 @@ class Node:
|
|||||||
def make_symmetric_tree() -> Node:
|
def make_symmetric_tree() -> Node:
|
||||||
r"""
|
r"""
|
||||||
Create a symmetric tree for testing.
|
Create a symmetric tree for testing.
|
||||||
|
|
||||||
The tree looks like this:
|
The tree looks like this:
|
||||||
1
|
1
|
||||||
/ \
|
/ \
|
||||||
2 2
|
2 2
|
||||||
/ \ / \
|
/ \ / \
|
||||||
3 4 4 3
|
3 4 4 3
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Node: Root node of a symmetric tree.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
>>> tree = make_symmetric_tree()
|
||||||
|
>>> tree.data
|
||||||
|
1
|
||||||
|
>>> tree.left.data == tree.right.data
|
||||||
|
True
|
||||||
|
>>> tree.left.left.data == tree.right.right.data
|
||||||
|
True
|
||||||
"""
|
"""
|
||||||
root = Node(1)
|
root = Node(1)
|
||||||
root.left = Node(2)
|
root.left = Node(2)
|
||||||
@ -43,13 +70,26 @@ def make_symmetric_tree() -> Node:
|
|||||||
|
|
||||||
def make_asymmetric_tree() -> Node:
|
def make_asymmetric_tree() -> Node:
|
||||||
r"""
|
r"""
|
||||||
Create a asymmetric tree for testing.
|
Create an asymmetric tree for testing.
|
||||||
|
|
||||||
The tree looks like this:
|
The tree looks like this:
|
||||||
1
|
1
|
||||||
/ \
|
/ \
|
||||||
2 2
|
2 2
|
||||||
/ \ / \
|
/ \ / \
|
||||||
3 4 3 4
|
3 4 3 4
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Node: Root node of an asymmetric tree.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
>>> tree = make_asymmetric_tree()
|
||||||
|
>>> tree.data
|
||||||
|
1
|
||||||
|
>>> tree.left.data == tree.right.data
|
||||||
|
True
|
||||||
|
>>> tree.left.left.data == tree.right.right.data
|
||||||
|
False
|
||||||
"""
|
"""
|
||||||
root = Node(1)
|
root = Node(1)
|
||||||
root.left = Node(2)
|
root.left = Node(2)
|
||||||
@ -63,7 +103,15 @@ def make_asymmetric_tree() -> Node:
|
|||||||
|
|
||||||
def is_symmetric_tree(tree: Node) -> bool:
|
def is_symmetric_tree(tree: Node) -> bool:
|
||||||
"""
|
"""
|
||||||
Test cases for is_symmetric_tree function
|
Check if a binary tree is symmetric (i.e., a mirror of itself).
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
tree: The root node of the binary tree.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: True if the tree is symmetric, False otherwise.
|
||||||
|
|
||||||
|
Example:
|
||||||
>>> is_symmetric_tree(make_symmetric_tree())
|
>>> is_symmetric_tree(make_symmetric_tree())
|
||||||
True
|
True
|
||||||
>>> is_symmetric_tree(make_asymmetric_tree())
|
>>> is_symmetric_tree(make_asymmetric_tree())
|
||||||
@ -76,8 +124,17 @@ def is_symmetric_tree(tree: Node) -> bool:
|
|||||||
|
|
||||||
def is_mirror(left: Node | None, right: Node | None) -> bool:
|
def is_mirror(left: Node | None, right: Node | None) -> bool:
|
||||||
"""
|
"""
|
||||||
|
Check if two subtrees are mirror images of each other.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
left: The root node of the left subtree.
|
||||||
|
right: The root node of the right subtree.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: True if the two subtrees are mirrors of each other, False otherwise.
|
||||||
|
|
||||||
|
Example:
|
||||||
>>> tree1 = make_symmetric_tree()
|
>>> tree1 = make_symmetric_tree()
|
||||||
>>> tree1.right.right = Node(3)
|
|
||||||
>>> is_mirror(tree1.left, tree1.right)
|
>>> is_mirror(tree1.left, tree1.right)
|
||||||
True
|
True
|
||||||
>>> tree2 = make_asymmetric_tree()
|
>>> tree2 = make_asymmetric_tree()
|
||||||
@ -91,7 +148,7 @@ def is_mirror(left: Node | None, right: Node | None) -> bool:
|
|||||||
# One side is empty while the other is not, which is not symmetric.
|
# One side is empty while the other is not, which is not symmetric.
|
||||||
return False
|
return False
|
||||||
if left.data == right.data:
|
if left.data == right.data:
|
||||||
# The values match, so check the subtree
|
# The values match, so check the subtrees recursively.
|
||||||
return is_mirror(left.left, right.right) and is_mirror(left.right, right.left)
|
return is_mirror(left.left, right.right) and is_mirror(left.right, right.left)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user