Skip to content

Commit a1fb73a

Browse files
Merge branch 'master' into commits
2 parents 20b90e5 + 89343fd commit a1fb73a

101 files changed

Lines changed: 4913 additions & 884 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

.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: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
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.
1817
* [ ] All function parameters and return values are annotated with Python [type hints](https://docs.python.org/3/library/typing.html).
1918
* [ ] All functions have [doctests](https://docs.python.org/3/library/doctest.html) that pass the automated testing.
2019
* [ ] All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
21-
* [ ] If this pull request resolves one or more open issues then the description above includes the issue number(s) with a [closing keyword](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue): "Fixes #ISSUE-NUMBER".
20+
* [ ] If this pull request resolves one or more open issues, then the description above includes the issue number(s) with a [closing keyword](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue): "Fixes #ISSUE-NUMBER".

.github/skills/code-review/SKILL.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,32 @@ suggest the fix — never just "rejected".
3333
- [ ] Descriptive variable and function names (no single letters where a word helps).
3434
- [ ] Code is formatted and lint-clean (`ruff`, `pre-commit`).
3535

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+
3662
### 3. Other Requirements for Submissions
3763

3864
- [ ] At least one **Wikipedia (or equivalent) URL** documenting the algorithm.
@@ -43,7 +69,7 @@ suggest the fix — never just "rejected".
4369

4470
Emit exactly these headings so reviews are comparable and easy to automate:
4571

46-
```
72+
```text
4773
### Is this an algorithm? — <yes/no + one-line why>
4874
### Duplicate / prior-art check — <#NNNN | none found>
4975
### Coding style — <pass | issues: …>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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 — and it does
23+
this *before* a human reads the PR, so a genuinely good change gets closed for a
24+
formatting reason. This applies to **every** pull request, including CI, docs,
25+
and tooling changes that are not algorithms: tick the boxes that genuinely apply
26+
so the body is never submitted with all boxes empty. Any repo maintainer can
27+
undo this if you @mention them on the closed pull request, but re-opening is
28+
often unreliable, so it is far better to get it right the first time.
29+
30+
### 1. Before contributing / Is this an algorithm?
31+
32+
- [ ] The change adds, fixes, or documents **one algorithm** — not multiple, and
33+
not both code and doctest changes in the same PR.
34+
- [ ] It is a genuine algorithm or data structure (see the *What is an Algorithm?*
35+
section), not a script, snippet, how-to-use for an existing API, or exercise
36+
dump.
37+
- [ ] It is **not already in the repository** (search the existing directories).
38+
- [ ] **No earlier open PR** already does the same thing — link it if one exists.
39+
- [ ] Properly attributed — no plagiarism; prior sources credited.
40+
41+
### 2. Coding Style
42+
43+
- [ ] `from __future__ import annotations` is not needed because this repo only uses
44+
the latest version of CPython.
45+
- [ ] File and directory names are lowercase, use underscores, and land inside an
46+
existing directory.
47+
- [ ] Public functions/classes have **type hints**.
48+
- [ ] Public functions have **doctests that actually pass**.
49+
- [ ] Descriptive variable and function names (no single letters where a word helps).
50+
- [ ] For a simple class that is mostly a bundle of fields, **consider**
51+
`from typing import NamedTuple` or `from dataclasses import dataclass`
52+
instead of a hand-written `__init__`/`__repr__`/`__eq__`. These are
53+
underutilized tools that make simple classes shorter and clearer — use
54+
them where they genuinely simplify the code, not everywhere.
55+
- [ ] Code is formatted and lint-clean (`ruff`, `pre-commit`).
56+
- [ ] `DIRECTORY.md` and `README.md` are **not hand-edited** — the
57+
`algorithms-keeper` bot regenerates them automatically after merge.
58+
59+
### 3. Other Requirements for Submissions
60+
61+
- [ ] At least one **Wikipedia (or equivalent) URL** documenting the algorithm.
62+
- [ ] Docstring explains what the function does and its parameters/returns.
63+
- [ ] No unnecessary third-party dependencies.
64+
65+
## Before you click "Create pull request"
66+
67+
This is the final gate. Do not open the pull request until every item here is true:
68+
69+
- [ ] At least one Markdown checkbox in the PR description is checked. **Verify
70+
this by re-reading the rendered body** — if every box is still `- [ ]`, the
71+
`algorithms-keeper` bot will auto-close the PR before any human sees it.
72+
Check the boxes that genuinely apply to this change; never submit an
73+
all-empty checklist, even for a CI, docs, or tooling PR.
74+
- [ ] The branch is not `master`, and `master` is synced with `upstream/master`.
75+
- [ ] `uv.lock` was not hand-edited.

.github/workflows/build.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ jobs:
1010
runs-on: ubuntu-latest
1111
steps:
1212
- uses: actions/checkout@v7
13+
with:
14+
persist-credentials: false
1315
- uses: astral-sh/setup-uv@v7
1416
with:
1517
enable-cache: true

.github/workflows/devcontainer_ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ jobs:
1515
runs-on: ubuntu-latest
1616
steps:
1717
- uses: actions/checkout@v7
18+
with:
19+
persist-credentials: false
1820
- uses: devcontainers/ci@v0.3
1921
with:
2022
push: never

.github/workflows/directory_writer.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ jobs:
99
- uses: actions/checkout@v7
1010
with:
1111
fetch-depth: 0
12+
persist-credentials: false
1213
- uses: actions/setup-python@v7
1314
with:
1415
python-version-file: .python-version
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# Daily refresh of the Hacktoberfest 2026 open-PR cleanup tracker.
2+
# Ticks off any tracked pull request that has since been merged/closed, and
3+
# rewrites the "Automated statistics" section (open issue/PR counts + the top
4+
# three `awaiting reviews` directories). The job fails on purpose once
5+
# Hacktoberfest 2026 has begun (>= 2026-10-01), which is the signal to retire it.
6+
name: hacktoberfest_prep
7+
8+
on:
9+
push:
10+
paths:
11+
- ".github/workflows/hacktoberfest_prep.yml"
12+
- "scripts/hacktoberfest_prep_update.py"
13+
pull_request:
14+
paths:
15+
- ".github/workflows/hacktoberfest_prep.yml"
16+
- "scripts/hacktoberfest_prep_update.py"
17+
schedule:
18+
- cron: "50 11 * * *" # 11:50 UTC every day
19+
workflow_dispatch: # allow a manual run while testing
20+
21+
permissions:
22+
contents: write
23+
pull-requests: write
24+
25+
jobs:
26+
hacktoberfest-prep:
27+
# No point running on forks — this pushes to the repo's own docs file.
28+
if: github.repository == 'TheAlgorithms/Python'
29+
runs-on: ubuntu-latest
30+
steps:
31+
- uses: actions/checkout@v7
32+
with:
33+
persist-credentials: false
34+
- uses: actions/setup-python@v7
35+
with:
36+
python-version-file: .python-version
37+
allow-prereleases: true
38+
- name: Install dependencies
39+
run: python -m pip install --upgrade "httpx2>=2.0.1"
40+
- name: Update the tracker
41+
id: update
42+
env:
43+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
44+
GITHUB_REPOSITORY: ${{ github.repository }}
45+
# Don't let the intentional post-Oct-1 failure stop the commit step;
46+
# capture the exit code and re-raise it after pushing any changes.
47+
run: |
48+
set +e
49+
python scripts/hacktoberfest_prep_update.py
50+
echo "exit_code=$?" >> "$GITHUB_OUTPUT"
51+
# Dry run on push / pull_request: show the diff the script produced but
52+
# do NOT commit or push. This lets a PR prove the tracker still gathers
53+
# its data and rewrites docs/hacktober_2026_prep.md correctly without
54+
# leaving a permanent commit. Only the schedule/manual runs persist.
55+
- name: Show changes (dry run)
56+
if: github.event_name == 'push' || github.event_name == 'pull_request'
57+
run: |
58+
echo "Dry run (${{ github.event_name }}): showing git diff, not committing."
59+
git --no-pager diff -- docs/hacktober_2026_prep.md
60+
if git diff --quiet -- docs/hacktober_2026_prep.md; then
61+
echo "No changes to docs/hacktober_2026_prep.md."
62+
fi
63+
# `master` is a protected branch: direct pushes are rejected with
64+
# `GH006: Protected branch update failed ... Changes must be made through
65+
# a pull request`. So instead of committing straight to master, persist
66+
# the refreshed tracker on a single rolling branch and open — or, since
67+
# re-pushing the branch updates the existing PR in place, leave open — one
68+
# pull request. Uses the bundled `gh` CLI rather than a third-party
69+
# action (see zizmor's "superfluous actions" audit).
70+
- name: Open or update the tracker pull request
71+
if: github.event_name != 'push' && github.event_name != 'pull_request'
72+
env:
73+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
74+
BRANCH: chore/hacktoberfest-2026-prep-refresh
75+
run: |
76+
set -euo pipefail
77+
if git diff --quiet -- docs/hacktober_2026_prep.md; then
78+
echo "No changes to docs/hacktober_2026_prep.md; nothing to persist."
79+
exit 0
80+
fi
81+
git config --global user.name "$GITHUB_ACTOR"
82+
git config --global user.email "$GITHUB_ACTOR@users.noreply.github.com"
83+
# `actions/checkout` runs with `persist-credentials: false`, so git has
84+
# no token to authenticate the push below (it fails with
85+
# `fatal: could not read Username for 'https://github.com'`). Wire the
86+
# bundled `gh` in as git's credential helper so `git push` reuses
87+
# GH_TOKEN — no third-party action and no token baked into the remote URL.
88+
gh auth setup-git
89+
git switch -c "$BRANCH"
90+
git add docs/hacktober_2026_prep.md
91+
git commit -m "chore: refresh Hacktoberfest 2026 prep tracker"
92+
# Force-push so the rolling branch always carries just the latest
93+
# snapshot on top of master; this also updates any open PR in place.
94+
git push --force origin "$BRANCH"
95+
if [ -z "$(gh pr list --head "$BRANCH" --state open --json number --jq '.[].number')" ]; then
96+
gh pr create \
97+
--base master \
98+
--head "$BRANCH" \
99+
--title "chore: refresh Hacktoberfest 2026 prep tracker" \
100+
--body "Automated daily refresh of the Hacktoberfest 2026 open-PR cleanup tracker (\`docs/hacktober_2026_prep.md\`): ticks off any tracked pull request that has since been merged/closed and rewrites the **Automated statistics** section.
101+
102+
This PR is updated in place by the \`hacktoberfest_prep\` workflow, so it always reflects the latest scheduled run. Merge it whenever you want to capture the current snapshot."
103+
else
104+
echo "Open tracker PR already exists; force-push updated it in place."
105+
fi
106+
- name: Propagate the script's exit code
107+
env:
108+
UPDATE_EXIT_CODE: ${{ steps.update.outputs.exit_code }}
109+
run: exit ${UPDATE_EXIT_CODE}

.github/workflows/project_euler.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ jobs:
1515
runs-on: ubuntu-latest
1616
steps:
1717
- uses: actions/checkout@v7
18+
with:
19+
persist-credentials: false
1820
- uses: astral-sh/setup-uv@v7
1921
- uses: actions/setup-python@v7
2022
with:
@@ -26,6 +28,8 @@ jobs:
2628
runs-on: ubuntu-latest
2729
steps:
2830
- uses: actions/checkout@v7
31+
with:
32+
persist-credentials: false
2933
- uses: astral-sh/setup-uv@v7
3034
- uses: actions/setup-python@v7
3135
with:

0 commit comments

Comments
 (0)