Skip to content

Commit 96b4f34

Browse files
authored
Merge branch 'master' into improve-linear-search-edge-case
2 parents a3425cf + 6a539de commit 96b4f34

191 files changed

Lines changed: 12040 additions & 2026 deletions

File tree

Some content is hidden

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

.devcontainer/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ This is **Devcontainer** configuration to provide a consistent development envir
1313

1414
## Usage
1515

16-
1. Install [**Docker** ](https://www.docker.com/get-started/) and [**Visual Studio Code**](https://code.visualstudio.com/)
16+
1. Install [**Docker**](https://www.docker.com/get-started/) and [**Visual Studio Code**](https://code.visualstudio.com/)
1717
2. Install the **Remote - Containers** extension in VS Code
1818

1919
- Do `CTRL+P`, paste this command and press `Enter`
2020

2121
```shell
2222
ext install ms-vscode-remote.remote-containers
2323
```
24+
2425
3. Open this repository in VS Code
2526
4. When prompted, click **"Reopen in Container"**
2627
5. Wait for the environment to build and initialize

.devcontainer/devcontainer.json

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
{
22
"name": "Python 3",
3-
"build": {
4-
"dockerfile": "Dockerfile",
5-
"context": "..",
6-
"args": {
7-
// Update 'VARIANT' to pick a Python version: 3, 3.11, 3.10, 3.9, 3.8
8-
// Append -bullseye or -buster to pin to an OS version.
9-
// Use -bullseye variants on local on arm64/Apple Silicon.
10-
"VARIANT": "3.13-bookworm"
11-
}
12-
},
133

14-
"postCreateCommand": "zsh .devcontainer/post_install",
4+
// Use a prebuilt dev container image instead of building from a local
5+
// Dockerfile. The repo migrated its dependencies to pyproject.toml, so the
6+
// old Dockerfile's `COPY requirements.txt` step no longer had a file to copy
7+
// and the image build failed. The upstream images already ship Python + a
8+
// full toolchain, so pulling one is both faster and less to maintain.
9+
//
10+
// This repo tracks the latest-and-greatest CPython on the newest stable
11+
// Debian. Images are published per CPython minor version on Debian 13
12+
// "Trixie" (3.11-trixie ... 3.14-trixie); bump this to the newest available
13+
// when a new stable CPython ships.
14+
// NOTE: these images do not publish free-threaded (`t`) variants, so 3.14t
15+
// cannot be selected via the tag alone -- but the repo's `.python-version`
16+
// pins 3.14t, and `uv run`/`uv sync` in the container honor it, so uv gives
17+
// contributors free-threaded 3.14t regardless of the base tag.
18+
"image": "mcr.microsoft.com/devcontainers/python:latest",
19+
20+
// Install the tools post_install and CI expect (pre-commit + ruff), plus uv
21+
// for the free-threaded workflow above, then run the existing setup script.
22+
"postCreateCommand": "pipx install pre-commit ruff uv && zsh .devcontainer/post_install",
1523

1624
// Configure tool-specific properties.
1725
"customizations": {
@@ -20,26 +28,30 @@
2028
// Set *default* container specific settings.json values on container create.
2129
"settings": {
2230
"python.defaultInterpreterPath": "/usr/local/bin/python",
23-
"python.linting.enabled": true,
24-
"python.formatting.blackPath": "/usr/local/py-utils/bin/black",
25-
"python.linting.mypyPath": "/usr/local/py-utils/bin/mypy",
31+
// Formatting/linting is handled by Ruff (matches pre-commit and CI).
32+
"editor.formatOnSave": true,
33+
"[python]": {
34+
"editor.defaultFormatter": "charliermarsh.ruff",
35+
"editor.codeActionsOnSave": {
36+
"source.fixAll": "explicit",
37+
"source.organizeImports": "explicit"
38+
}
39+
},
2640
"terminal.integrated.defaultProfile.linux": "zsh"
2741
},
2842

2943
// Add the IDs of extensions you want installed when the container is created.
3044
"extensions": [
3145
"ms-python.python",
32-
"ms-python.vscode-pylance"
46+
"ms-python.vscode-pylance",
47+
"charliermarsh.ruff"
3348
]
3449
}
3550
},
3651

3752
// Use 'forwardPorts' to make a list of ports inside the container available locally.
3853
// "forwardPorts": [],
3954

40-
// Use 'postCreateCommand' to run commands after the container is created.
41-
// "postCreateCommand": "pip3 install --user -r requirements.txt",
42-
4355
// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
4456
"remoteUser": "vscode"
4557
}

.github/dependabot.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ updates:
66
directory: "/"
77
schedule:
88
interval: "daily"
9+
cooldown:
10+
default-days: 7

.github/pull_request_template.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
### Describe your change:
2-
3-
1+
### Describe your change
42

53
* [ ] Add an algorithm?
64
* [ ] Fix a bug or typo in an existing algorithm?
75
* [ ] Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
86
* [ ] Documentation change?
97

10-
### Checklist:
8+
### Checklist
9+
1110
* [ ] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Python/blob/master/CONTRIBUTING.md).
1211
* [ ] This pull request is all my own work -- I have not plagiarized.
1312
* [ ] I know that pull requests will not be merged if they fail the automated tests.
14-
* [ ] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
13+
* [ ] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
1514
* [ ] All new Python files are placed inside an existing directory.
1615
* [ ] All filenames are in all lowercase characters with no spaces or dashes.
1716
* [ ] All functions and variable names follow Python naming conventions.
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Skill: Code review for TheAlgorithms/Python
2+
3+
Review a pull request against the rules already written in
4+
[`CONTRIBUTING.md`](../../../CONTRIBUTING.md). The goal is a review that any
5+
reviewer (human or AI) can run the same way every time, and that produces a clear,
6+
kind, actionable verdict.
7+
8+
## How to run this skill
9+
10+
Read the PR diff, then work through the four `CONTRIBUTING.md` sections in order
11+
and emit the fixed output shape below. Cite the exact rule you are applying and
12+
suggest the fix — never just "rejected".
13+
14+
### 1. Before contributing / Is this an algorithm?
15+
16+
- [ ] The change adds, fixes, or documents **one algorithm** — not multiple, and
17+
not both code and doctest changes in the same PR.
18+
- [ ] It is a genuine algorithm or data structure (see the *What is an Algorithm?*
19+
section), not a script, snippet, how-to-use for an existing API, or exercise
20+
dump.
21+
- [ ] It is **not already in the repository** (search the existing directories).
22+
- [ ] **No earlier open PR** already does the same thing — link it if one exists.
23+
- [ ] Properly attributed — no plagiarism; prior sources credited.
24+
25+
### 2. Coding Style
26+
27+
- [ ] `from __future__ import annotations` is not needed because this repo only uses
28+
the latest version of CPython.
29+
- [ ] File and directory names are lowercase, use underscores, and land inside an
30+
existing directory.
31+
- [ ] Public functions/classes have **type hints**.
32+
- [ ] Public functions have **doctests that actually pass**.
33+
- [ ] Descriptive variable and function names (no single letters where a word helps).
34+
- [ ] Code is formatted and lint-clean (`ruff`, `pre-commit`).
35+
36+
> **Optional hint:** When a PR hand-writes a simple class that is mostly a
37+
> bundle of fields (a manual `__init__` plus `__repr__`/`__eq__`), it is worth
38+
> **suggesting** `from typing import NamedTuple` or
39+
> `from dataclasses import dataclass` where they would simplify the code. These
40+
> are underutilized tools that our contributors would benefit from using where
41+
> they make sense. Offer it as an optional improvement, not a blocker — do not
42+
> request changes solely because a class was written the longhand way.
43+
44+
#### When a PR fails `ruff check`
45+
46+
Don't just report the failure — try the mechanical fixes and recommend the one
47+
that works, in this order:
48+
49+
1. Run `ruff check --fix file_path.py`. If that makes the file pass, recommend
50+
that solution — these are the fixes `ruff` considers **safe**.
51+
2. If it still fails, run `ruff check --fix --unsafe-fixes file_path.py`. If that
52+
makes the file pass **and** the resulting diff is genuinely safe (it preserves
53+
behavior — review it, don't trust it blindly), recommend that solution and note
54+
that it required `--unsafe-fixes`.
55+
3. If neither passes, or the unsafe fix would change behavior, describe the
56+
remaining rule violations and the manual change the author needs to make.
57+
58+
Always quote the exact rule code(s) `ruff` reports (e.g., `ruff rule UP047`,
59+
`ruff rule RUF100`) so the author can run those commands to read the rules being
60+
flagged. Also, paste the concrete command you ran.
61+
62+
### 3. Other Requirements for Submissions
63+
64+
- [ ] At least one **Wikipedia (or equivalent) URL** documenting the algorithm.
65+
- [ ] Docstring explains what the function does and its parameters/returns.
66+
- [ ] No unnecessary third-party dependencies.
67+
68+
### 4. Verdict — fixed output shape
69+
70+
Emit exactly these headings so reviews are comparable and easy to automate:
71+
72+
```text
73+
### Is this an algorithm? — <yes/no + one-line why>
74+
### Duplicate / prior-art check — <#NNNN | none found>
75+
### Coding style — <pass | issues: …>
76+
### Other requirements (doctests, type hints, descriptive names, Wikipedia URL) — <pass | issues: …>
77+
### Verdict — <approve | request changes | close> + one-line reason
78+
```
79+
80+
## Tone
81+
82+
Be specific and kind. Point at the exact `CONTRIBUTING.md` rule and offer the fix
83+
rather than a bare rejection — first-time and Hacktoberfest contributors are more
84+
likely to come back and improve the PR when the path forward is clear.
85+
86+
## Map findings to labels
87+
88+
Where a finding matches an existing label, name it so the review lines up with the
89+
maintenance/cleanup tooling:
90+
91+
- missing/failing doctests → `require tests`
92+
- missing type hints → `require type hints`
93+
- non-descriptive names → `require descriptive names`
94+
- CI red → `tests are failing`
95+
- otherwise ready for a maintainer → `awaiting reviews`
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Skill: New pull request for TheAlgorithms/Python
2+
3+
Create a new pull request using the rules already written in
4+
[`CONTRIBUTING.md`](../../../CONTRIBUTING.md). The goal is that creating a new
5+
pull request (human or AI) can run the same way every time, and that produces a
6+
clear, kind, tested, type-hinted, mergeable contribution.
7+
8+
## How to run this skill
9+
10+
Make sure that the local `master` branch is synced with `upstream/master` before
11+
creating a new pull request.
12+
13+
Create a new clearly named branch for the pull request. Pull request changes must
14+
not be made or submitted on the `master` branch.
15+
16+
Never hand-edit or revert the `uv.lock` file. If you add a legitimate
17+
dependency, let the `uv-lock` pre-commit hook regenerate it — do not touch it by
18+
hand. A hand-modified `uv.lock` makes the `algorithms-keeper` bot close the pull
19+
request as invalid, and even a repo maintainer cannot undo that.
20+
21+
Always check at least one Markdown checkbox in the pull request description (the "Describe your change" section), or the
22+
`algorithms-keeper` bot will close the pull request as invalid. Any repo maintainer can undo this if you @mention them on the closed pull request.
23+
24+
### 1. Before contributing / Is this an algorithm?
25+
26+
- [ ] The change adds, fixes, or documents **one algorithm** — not multiple, and
27+
not both code and doctest changes in the same PR.
28+
- [ ] It is a genuine algorithm or data structure (see the *What is an Algorithm?*
29+
section), not a script, snippet, how-to-use for an existing API, or exercise
30+
dump.
31+
- [ ] It is **not already in the repository** (search the existing directories).
32+
- [ ] **No earlier open PR** already does the same thing — link it if one exists.
33+
- [ ] Properly attributed — no plagiarism; prior sources credited.
34+
35+
### 2. Coding Style
36+
37+
- [ ] `from __future__ import annotations` is not needed because this repo only uses
38+
the latest version of CPython.
39+
- [ ] File and directory names are lowercase, use underscores, and land inside an
40+
existing directory.
41+
- [ ] Public functions/classes have **type hints**.
42+
- [ ] Public functions have **doctests that actually pass**.
43+
- [ ] Descriptive variable and function names (no single letters where a word helps).
44+
- [ ] For a simple class that is mostly a bundle of fields, **consider**
45+
`from typing import NamedTuple` or `from dataclasses import dataclass`
46+
instead of a hand-written `__init__`/`__repr__`/`__eq__`. These are
47+
underutilized tools that make simple classes shorter and clearer — use
48+
them where they genuinely simplify the code, not everywhere.
49+
- [ ] Code is formatted and lint-clean (`ruff`, `pre-commit`).
50+
- [ ] `DIRECTORY.md` and `README.md` are **not hand-edited** — the
51+
`algorithms-keeper` bot regenerates them automatically after merge.
52+
53+
### 3. Other Requirements for Submissions
54+
55+
- [ ] At least one **Wikipedia (or equivalent) URL** documenting the algorithm.
56+
- [ ] Docstring explains what the function does and its parameters/returns.
57+
- [ ] No unnecessary third-party dependencies.

.github/workflows/build.yml

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,40 @@ jobs:
99
build:
1010
runs-on: ubuntu-latest
1111
steps:
12-
- run: sudo apt-get update && sudo apt-get install -y libhdf5-dev
13-
- uses: actions/checkout@v6
12+
- uses: actions/checkout@v7
13+
with:
14+
persist-credentials: false
1415
- uses: astral-sh/setup-uv@v7
1516
with:
1617
enable-cache: true
1718
cache-dependency-glob: uv.lock
18-
- uses: actions/setup-python@v6
19+
- uses: actions/setup-python@v7
1920
with:
20-
python-version: 3.14
21+
python-version-file: .python-version
2122
allow-prereleases: true
2223
- run: uv sync --group=test
2324
- name: Run tests
24-
# TODO: #8818 Re-enable quantum tests
25+
# opencv-python is gated out on 3.14t (no cp314t wheel yet), so skip the
26+
# files that import cv2. Pure-Python algorithms in computer_vision/ and
27+
# data_compression/ still run; digital_image_processing/ is almost entirely
28+
# cv2-based so it is skipped as a tree. Re-enable when a cp314t wheel ships.
29+
# --ignore-gil-enabled: some compiled deps (sklearn, xgboost, ...) don't
30+
# yet ship the Py_mod_gil slot, so importing them re-enables the GIL under
31+
# 3.14t. That's an upstream-wheel gap, not our code; the flag lets the suite
32+
# run anyway and pytest-run-parallel still reports which tests are not
33+
# thread-safe. Drop the flag once the scientific stack ships free-threaded wheels.
34+
# qiskit is likewise gated out of the 3.14t deps (Qiskit/qiskit#16893), so the
35+
# single file that imports it (quantum/q_fourier_transform.py) is skipped too.
2536
run: uv run --with=pytest-run-parallel pytest
26-
--iterations=8 --parallel-threads=auto
37+
--iterations=8 --parallel-threads=auto --ignore-gil-enabled
2738
--ignore=computer_vision/cnn_classification.py
39+
--ignore=computer_vision/flip_augmentation.py
40+
--ignore=computer_vision/harris_corner.py
41+
--ignore=computer_vision/mosaic_augmentation.py
42+
--ignore=data_compression/peak_signal_to_noise_ratio.py
43+
--ignore=digital_image_processing/
2844
--ignore=docs/conf.py
2945
--ignore=dynamic_programming/k_means_clustering_tensorflow.py
30-
--ignore=machine_learning/local_weighted_learning/local_weighted_learning.py
3146
--ignore=machine_learning/lstm/lstm_prediction.py
3247
--ignore=neural_network/input_data.py
3348
--ignore=project_euler/

.github/workflows/devcontainer_ci.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@ on:
44
push:
55
paths:
66
- ".devcontainer/**"
7+
- ".github/workflows/devcontainer_ci.yml"
78
pull_request:
89
paths:
910
- ".devcontainer/**"
11+
- ".github/workflows/devcontainer_ci.yml"
1012

1113
jobs:
1214
build:
1315
runs-on: ubuntu-latest
1416
steps:
15-
- uses: actions/checkout@v6
17+
- uses: actions/checkout@v7
18+
with:
19+
persist-credentials: false
1620
- uses: devcontainers/ci@v0.3
1721
with:
1822
push: never

.github/workflows/directory_writer.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ jobs:
66
directory_writer:
77
runs-on: ubuntu-latest
88
steps:
9-
- uses: actions/checkout@v6
9+
- uses: actions/checkout@v7
1010
with:
1111
fetch-depth: 0
12-
- uses: actions/setup-python@v6
12+
persist-credentials: false
13+
- uses: actions/setup-python@v7
1314
with:
14-
python-version: 3.14
15+
python-version-file: .python-version
1516
allow-prereleases: true
1617
- name: Write DIRECTORY.md
1718
run: |

0 commit comments

Comments
 (0)