Skip to content

Commit c76f6d2

Browse files
authored
Merge branch 'master' into pre-commit-ci-update-config
2 parents f30a474 + f3f32ae commit c76f6d2

File tree

108 files changed

+1952
-356
lines changed

Some content is hidden

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

108 files changed

+1952
-356
lines changed

Diff for: .devcontainer/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# https://github.com/microsoft/vscode-dev-containers/blob/main/containers/python-3/README.md
2-
ARG VARIANT=3.12-bookworm
2+
ARG VARIANT=3.13-bookworm
33
FROM mcr.microsoft.com/vscode/devcontainers/python:${VARIANT}
44
COPY requirements.txt /tmp/pip-tmp/
55
RUN python3 -m pip install --upgrade pip \

Diff for: .devcontainer/devcontainer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// Update 'VARIANT' to pick a Python version: 3, 3.11, 3.10, 3.9, 3.8
88
// Append -bullseye or -buster to pin to an OS version.
99
// Use -bullseye variants on local on arm64/Apple Silicon.
10-
"VARIANT": "3.12-bookworm",
10+
"VARIANT": "3.13-bookworm",
1111
}
1212
},
1313

Diff for: .github/CODEOWNERS

-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
/.* @cclauss
1111

12-
# /arithmetic_analysis/
13-
1412
# /backtracking/
1513

1614
# /bit_manipulation/

Diff for: .github/workflows/build.yml

+8-3
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,26 @@ jobs:
1212
- uses: actions/checkout@v4
1313
- uses: actions/setup-python@v5
1414
with:
15-
python-version: 3.12
15+
python-version: 3.13
1616
allow-prereleases: true
1717
- uses: actions/cache@v4
1818
with:
1919
path: ~/.cache/pip
2020
key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }}
2121
- name: Install dependencies
2222
run: |
23-
python -m pip install --upgrade pip setuptools six wheel
23+
python -m pip install --upgrade pip setuptools wheel
2424
python -m pip install pytest-cov -r requirements.txt
2525
- name: Run tests
2626
# TODO: #8818 Re-enable quantum tests
2727
run: pytest
28-
--ignore=quantum/q_fourier_transform.py
28+
--ignore=computer_vision/cnn_classification.py
29+
--ignore=docs/conf.py
30+
--ignore=dynamic_programming/k_means_clustering_tensorflow.py
31+
--ignore=machine_learning/lstm/lstm_prediction.py
32+
--ignore=neural_network/input_data.py
2933
--ignore=project_euler/
34+
--ignore=quantum/q_fourier_transform.py
3035
--ignore=scripts/validate_solutions.py
3136
--cov-report=term-missing:skip-covered
3237
--cov=. .

Diff for: .github/workflows/sphinx.yml

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
name: sphinx
2+
3+
on:
4+
# Triggers the workflow on push or pull request events but only for the "master" branch
5+
push:
6+
branches: ["master"]
7+
pull_request:
8+
branches: ["master"]
9+
# Or manually from the Actions tab
10+
workflow_dispatch:
11+
12+
# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
13+
permissions:
14+
contents: read
15+
pages: write
16+
id-token: write
17+
18+
# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
19+
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
20+
concurrency:
21+
group: "pages"
22+
cancel-in-progress: false
23+
24+
jobs:
25+
build_docs:
26+
runs-on: ubuntu-latest
27+
steps:
28+
- uses: actions/checkout@v4
29+
- uses: actions/setup-python@v5
30+
with:
31+
python-version: 3.13
32+
allow-prereleases: true
33+
- run: pip install --upgrade pip
34+
- run: pip install myst-parser sphinx-autoapi sphinx-pyproject
35+
- uses: actions/configure-pages@v5
36+
- run: sphinx-build -c docs . docs/_build/html
37+
- uses: actions/upload-pages-artifact@v3
38+
with:
39+
path: docs/_build/html
40+
41+
deploy_docs:
42+
environment:
43+
name: github-pages
44+
url: ${{ steps.deployment.outputs.page_url }}
45+
if: github.event_name != 'pull_request'
46+
needs: build_docs
47+
runs-on: ubuntu-latest
48+
steps:
49+
- uses: actions/deploy-pages@v4
50+
id: deployment

Diff for: .pre-commit-config.yaml

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/pre-commit/pre-commit-hooks
3-
rev: v4.6.0
3+
rev: v5.0.0
44
hooks:
55
- id: check-executables-have-shebangs
66
- id: check-toml
@@ -16,7 +16,7 @@ repos:
1616
- id: auto-walrus
1717

1818
- repo: https://github.com/astral-sh/ruff-pre-commit
19-
rev: v0.5.0
19+
rev: v0.8.0
2020
hooks:
2121
- id: ruff
2222
- id: ruff-format
@@ -29,7 +29,7 @@ repos:
2929
- tomli
3030

3131
- repo: https://github.com/tox-dev/pyproject-fmt
32-
rev: "2.1.3"
32+
rev: "v2.5.0"
3333
hooks:
3434
- id: pyproject-fmt
3535

@@ -42,12 +42,12 @@ repos:
4242
pass_filenames: false
4343

4444
- repo: https://github.com/abravalheri/validate-pyproject
45-
rev: v0.18
45+
rev: v0.23
4646
hooks:
4747
- id: validate-pyproject
4848

4949
- repo: https://github.com/pre-commit/mirrors-mypy
50-
rev: v1.10.1
50+
rev: v1.13.0
5151
hooks:
5252
- id: mypy
5353
args:

Diff for: CONTRIBUTING.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ pre-commit run --all-files --show-diff-on-failure
7777

7878
We want your work to be readable by others; therefore, we encourage you to note the following:
7979

80-
- Please write in Python 3.12+. For instance: `print()` is a function in Python 3 so `print "Hello"` will *not* work but `print("Hello")` will.
80+
- Please write in Python 3.13+. For instance: `print()` is a function in Python 3 so `print "Hello"` will *not* work but `print("Hello")` will.
8181
- Please focus hard on the naming of functions, classes, and variables. Help your reader by using __descriptive names__ that can help you to remove redundant comments.
8282
- Single letter variable names are *old school* so please avoid them unless their life only spans a few lines.
8383
- Expand acronyms because `gcd()` is hard to understand but `greatest_common_divisor()` is not.
@@ -96,7 +96,7 @@ We want your work to be readable by others; therefore, we encourage you to note
9696

9797
```bash
9898
python3 -m pip install ruff # only required the first time
99-
ruff .
99+
ruff check
100100
```
101101

102102
- Original code submission require docstrings or comments to describe your work.

Diff for: DIRECTORY.md

+27-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
* [Rat In Maze](backtracking/rat_in_maze.py)
2323
* [Sudoku](backtracking/sudoku.py)
2424
* [Sum Of Subsets](backtracking/sum_of_subsets.py)
25+
* [Word Break](backtracking/word_break.py)
26+
* [Word Ladder](backtracking/word_ladder.py)
2527
* [Word Search](backtracking/word_search.py)
2628

2729
## Bit Manipulation
@@ -98,6 +100,7 @@
98100
* [Elgamal Key Generator](ciphers/elgamal_key_generator.py)
99101
* [Enigma Machine2](ciphers/enigma_machine2.py)
100102
* [Fractionated Morse Cipher](ciphers/fractionated_morse_cipher.py)
103+
* [Gronsfeld Cipher](ciphers/gronsfeld_cipher.py)
101104
* [Hill Cipher](ciphers/hill_cipher.py)
102105
* [Mixed Keyword Cypher](ciphers/mixed_keyword_cypher.py)
103106
* [Mono Alphabetic Ciphers](ciphers/mono_alphabetic_ciphers.py)
@@ -210,6 +213,7 @@
210213
* [Lazy Segment Tree](data_structures/binary_tree/lazy_segment_tree.py)
211214
* [Lowest Common Ancestor](data_structures/binary_tree/lowest_common_ancestor.py)
212215
* [Maximum Fenwick Tree](data_structures/binary_tree/maximum_fenwick_tree.py)
216+
* [Maximum Sum Bst](data_structures/binary_tree/maximum_sum_bst.py)
213217
* [Merge Two Binary Trees](data_structures/binary_tree/merge_two_binary_trees.py)
214218
* [Mirror Binary Tree](data_structures/binary_tree/mirror_binary_tree.py)
215219
* [Non Recursive Segment Tree](data_structures/binary_tree/non_recursive_segment_tree.py)
@@ -243,6 +247,15 @@
243247
* [Min Heap](data_structures/heap/min_heap.py)
244248
* [Randomized Heap](data_structures/heap/randomized_heap.py)
245249
* [Skew Heap](data_structures/heap/skew_heap.py)
250+
* Kd Tree
251+
* [Build Kdtree](data_structures/kd_tree/build_kdtree.py)
252+
* Example
253+
* [Example Usage](data_structures/kd_tree/example/example_usage.py)
254+
* [Hypercube Points](data_structures/kd_tree/example/hypercube_points.py)
255+
* [Kd Node](data_structures/kd_tree/kd_node.py)
256+
* [Nearest Neighbour Search](data_structures/kd_tree/nearest_neighbour_search.py)
257+
* Tests
258+
* [Test Kdtree](data_structures/kd_tree/tests/test_kdtree.py)
246259
* Linked List
247260
* [Circular Linked List](data_structures/linked_list/circular_linked_list.py)
248261
* [Deque Doubly](data_structures/linked_list/deque_doubly.py)
@@ -274,6 +287,7 @@
274287
* [Dijkstras Two Stack Algorithm](data_structures/stacks/dijkstras_two_stack_algorithm.py)
275288
* [Infix To Postfix Conversion](data_structures/stacks/infix_to_postfix_conversion.py)
276289
* [Infix To Prefix Conversion](data_structures/stacks/infix_to_prefix_conversion.py)
290+
* [Lexicographical Numbers](data_structures/stacks/lexicographical_numbers.py)
277291
* [Next Greater Element](data_structures/stacks/next_greater_element.py)
278292
* [Postfix Evaluation](data_structures/stacks/postfix_evaluation.py)
279293
* [Prefix Evaluation](data_structures/stacks/prefix_evaluation.py)
@@ -282,6 +296,13 @@
282296
* [Stack With Doubly Linked List](data_structures/stacks/stack_with_doubly_linked_list.py)
283297
* [Stack With Singly Linked List](data_structures/stacks/stack_with_singly_linked_list.py)
284298
* [Stock Span Problem](data_structures/stacks/stock_span_problem.py)
299+
* Suffix Tree
300+
* Example
301+
* [Example Usage](data_structures/suffix_tree/example/example_usage.py)
302+
* [Suffix Tree](data_structures/suffix_tree/suffix_tree.py)
303+
* [Suffix Tree Node](data_structures/suffix_tree/suffix_tree_node.py)
304+
* Tests
305+
* [Test Suffix Tree](data_structures/suffix_tree/tests/test_suffix_tree.py)
285306
* Trie
286307
* [Radix Tree](data_structures/trie/radix_tree.py)
287308
* [Trie](data_structures/trie/trie.py)
@@ -330,6 +351,9 @@
330351
* [Power](divide_and_conquer/power.py)
331352
* [Strassen Matrix Multiplication](divide_and_conquer/strassen_matrix_multiplication.py)
332353

354+
## Docs
355+
* [Conf](docs/conf.py)
356+
333357
## Dynamic Programming
334358
* [Abbreviation](dynamic_programming/abbreviation.py)
335359
* [All Construct](dynamic_programming/all_construct.py)
@@ -540,8 +564,7 @@
540564
* [Lu Decomposition](linear_algebra/lu_decomposition.py)
541565
* Src
542566
* [Conjugate Gradient](linear_algebra/src/conjugate_gradient.py)
543-
* Gaussian Elimination Pivoting
544-
* [Gaussian Elimination Pivoting](linear_algebra/src/gaussian_elimination_pivoting/gaussian_elimination_pivoting.py)
567+
* [Gaussian Elimination Pivoting](linear_algebra/src/gaussian_elimination_pivoting.py)
545568
* [Lib](linear_algebra/src/lib.py)
546569
* [Polynom For Points](linear_algebra/src/polynom_for_points.py)
547570
* [Power Iteration](linear_algebra/src/power_iteration.py)
@@ -1185,6 +1208,7 @@
11851208
* [Binary Tree Traversal](searches/binary_tree_traversal.py)
11861209
* [Double Linear Search](searches/double_linear_search.py)
11871210
* [Double Linear Search Recursion](searches/double_linear_search_recursion.py)
1211+
* [Exponential Search](searches/exponential_search.py)
11881212
* [Fibonacci Search](searches/fibonacci_search.py)
11891213
* [Hill Climbing](searches/hill_climbing.py)
11901214
* [Interpolation Search](searches/interpolation_search.py)
@@ -1260,6 +1284,7 @@
12601284
* [Can String Be Rearranged As Palindrome](strings/can_string_be_rearranged_as_palindrome.py)
12611285
* [Capitalize](strings/capitalize.py)
12621286
* [Check Anagrams](strings/check_anagrams.py)
1287+
* [Count Vowels](strings/count_vowels.py)
12631288
* [Credit Card Validator](strings/credit_card_validator.py)
12641289
* [Damerau Levenshtein Distance](strings/damerau_levenshtein_distance.py)
12651290
* [Detecting English Programmatically](strings/detecting_english_programmatically.py)
@@ -1327,7 +1352,6 @@
13271352
* [Get Ip Geolocation](web_programming/get_ip_geolocation.py)
13281353
* [Get Top Billionaires](web_programming/get_top_billionaires.py)
13291354
* [Get Top Hn Posts](web_programming/get_top_hn_posts.py)
1330-
* [Get User Tweets](web_programming/get_user_tweets.py)
13311355
* [Giphy](web_programming/giphy.py)
13321356
* [Instagram Crawler](web_programming/instagram_crawler.py)
13331357
* [Instagram Pic](web_programming/instagram_pic.py)

Diff for: LICENSE.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
MIT License
1+
## MIT License
22

33
Copyright (c) 2016-2022 TheAlgorithms and contributors
44

Diff for: backtracking/word_break.py

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
"""
2+
Word Break Problem is a well-known problem in computer science.
3+
Given a string and a dictionary of words, the task is to determine if
4+
the string can be segmented into a sequence of one or more dictionary words.
5+
6+
Wikipedia: https://en.wikipedia.org/wiki/Word_break_problem
7+
"""
8+
9+
10+
def backtrack(input_string: str, word_dict: set[str], start: int) -> bool:
11+
"""
12+
Helper function that uses backtracking to determine if a valid
13+
word segmentation is possible starting from index 'start'.
14+
15+
Parameters:
16+
input_string (str): The input string to be segmented.
17+
word_dict (set[str]): A set of valid dictionary words.
18+
start (int): The starting index of the substring to be checked.
19+
20+
Returns:
21+
bool: True if a valid segmentation is possible, otherwise False.
22+
23+
Example:
24+
>>> backtrack("leetcode", {"leet", "code"}, 0)
25+
True
26+
27+
>>> backtrack("applepenapple", {"apple", "pen"}, 0)
28+
True
29+
30+
>>> backtrack("catsandog", {"cats", "dog", "sand", "and", "cat"}, 0)
31+
False
32+
"""
33+
34+
# Base case: if the starting index has reached the end of the string
35+
if start == len(input_string):
36+
return True
37+
38+
# Try every possible substring from 'start' to 'end'
39+
for end in range(start + 1, len(input_string) + 1):
40+
if input_string[start:end] in word_dict and backtrack(
41+
input_string, word_dict, end
42+
):
43+
return True
44+
45+
return False
46+
47+
48+
def word_break(input_string: str, word_dict: set[str]) -> bool:
49+
"""
50+
Determines if the input string can be segmented into a sequence of
51+
valid dictionary words using backtracking.
52+
53+
Parameters:
54+
input_string (str): The input string to segment.
55+
word_dict (set[str]): The set of valid words.
56+
57+
Returns:
58+
bool: True if the string can be segmented into valid words, otherwise False.
59+
60+
Example:
61+
>>> word_break("leetcode", {"leet", "code"})
62+
True
63+
64+
>>> word_break("applepenapple", {"apple", "pen"})
65+
True
66+
67+
>>> word_break("catsandog", {"cats", "dog", "sand", "and", "cat"})
68+
False
69+
"""
70+
71+
return backtrack(input_string, word_dict, 0)

0 commit comments

Comments
 (0)