Skip to content

Commit 7e7894f

Browse files
authored
Merge branch 'master' into python-3.15
2 parents ce3da2f + 5600928 commit 7e7894f

483 files changed

Lines changed: 25657 additions & 5533 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.

‎.github/workflows/build.yml‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@ jobs:
2020
with:
2121
python-version-file: .python-version
2222
allow-prereleases: true
23-
# keras needs hdf5 on pre-release Python.
24-
- run: |
25-
if [ "$(python -c 'import sys; print(sys.version_info.releaselevel)')" != "final" ]; then
26-
sudo apt-get update -qq
27-
sudo apt-get install --yes libhdf5-dev
28-
fi
2923
- run: uv sync --group=test
3024
- name: Run tests
3125
# opencv-python is gated out on 3.14t (no cp314t wheel yet), so skip the
@@ -41,16 +35,12 @@ jobs:
4135
# single file that imports it (quantum/q_fourier_transform.py) is skipped too.
4236
run: uv run --with=pytest-run-parallel pytest
4337
--iterations=8 --parallel-threads=auto --ignore-gil-enabled
44-
--ignore=computer_vision/cnn_classification.py
4538
--ignore=computer_vision/flip_augmentation.py
4639
--ignore=computer_vision/harris_corner.py
4740
--ignore=computer_vision/mosaic_augmentation.py
4841
--ignore=data_compression/peak_signal_to_noise_ratio.py
4942
--ignore=digital_image_processing/
5043
--ignore=docs/conf.py
51-
--ignore=dynamic_programming/k_means_clustering_tensorflow.py
52-
--ignore=machine_learning/lstm/lstm_prediction.py
53-
--ignore=neural_network/input_data.py
5444
--ignore=project_euler/
5545
--ignore=quantum/q_fourier_transform.py
5646
--ignore=scripts/validate_solutions.py

‎.github/workflows/sphinx.yml‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,6 @@ jobs:
3333
with:
3434
python-version-file: .python-version
3535
allow-prereleases: true
36-
# keras needs hdf5 on pre-release Python.
37-
- run: |
38-
if [ "$(python -c 'import sys; print(sys.version_info.releaselevel)')" != "final" ]; then
39-
sudo apt-get update -qq
40-
sudo apt-get install --yes libhdf5-dev
41-
fi
4236
- run: uv sync --group=docs
4337
- uses: actions/configure-pages@v6
4438
- run: uv run sphinx-build -c docs . docs/_build/html

‎.pre-commit-config.yaml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ repos:
3434
- id: pyproject-fmt
3535

3636
- repo: https://github.com/astral-sh/ruff-pre-commit
37-
rev: v0.16.6
37+
rev: v0.16.7
3838
hooks:
3939
- id: ruff-check
4040
- id: ruff-format
4141

4242
- repo: https://github.com/rvben/rumdl-pre-commit
43-
rev: v0.2.69
43+
rev: v0.2.73
4444
hooks:
4545
- id: rumdl-fmt
4646
- id: rumdl
@@ -55,7 +55,7 @@ repos:
5555
pass_filenames: false
5656

5757
- repo: https://github.com/astral-sh/uv-pre-commit
58-
rev: 0.12.11
58+
rev: 0.12.13
5959
hooks:
6060
- id: uv-lock
6161

‎CONTRIBUTING.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ Comments which are not on the same line as code should appear ___before___ the c
196196
return a + b
197197
```
198198

199-
[Install ty](https://docs.astral.sh/ty/installation/) and then pslease use the command `ty check` to test all files or `ty check path/to/file.py` to test a specific file.
199+
[Install ty](https://docs.astral.sh/ty/installation/) and then please use the command `ty check` to test all files or `ty check path/to/file.py` to test a specific file.
200200

201201
- [__List comprehensions and generators__](https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions) are preferred over the use of `lambda`, `map`, `filter`, `reduce` but the important thing is to demonstrate the power of Python in code that is easy to read and maintain.
202202

‎DIRECTORY.md‎

Lines changed: 171 additions & 6 deletions
Large diffs are not rendered by default.

‎backtracking/all_subsequences.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,21 @@
1212

1313

1414
def generate_all_subsequences(sequence: list[Any]) -> None:
15+
"""
16+
Generate and print all possible subsequences of the given sequence.
17+
18+
>>> generate_all_subsequences([1, 2])
19+
[]
20+
[2]
21+
[1]
22+
[1, 2]
23+
>>> generate_all_subsequences(["A", "B"])
24+
[]
25+
['B']
26+
['A']
27+
['A', 'B']
28+
"""
29+
1530
create_state_space_tree(sequence, [], 0)
1631

1732

‎backtracking/coloring.py‎

Lines changed: 89 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
2-
Graph Coloring also called "m coloring problem"
3-
consists of coloring a given graph with at most m colors
4-
such that no adjacent vertices are assigned the same color
2+
Graph Coloring (also called the "m coloring problem") is the problem of
3+
assigning at most 'm' colors to the vertices of a graph such that
4+
no two adjacent vertices share the same color.
55
66
Wikipedia: https://en.wikipedia.org/wiki/Graph_coloring
77
"""
@@ -11,22 +11,46 @@ def valid_coloring(
1111
neighbours: list[int], colored_vertices: list[int], color: int
1212
) -> bool:
1313
"""
14+
Check if a given vertex can be assigned the specified color
15+
without violating the graph coloring constraints (i.e., no two adjacent vertices
16+
have the same color).
17+
18+
Procedure:
1419
For each neighbour check if the coloring constraint is satisfied
1520
If any of the neighbours fail the constraint return False
1621
If all neighbours validate the constraint return True
1722
18-
>>> neighbours = [0,1,0,1,0]
23+
Parameters:
24+
neighbours: The list representing which vertices
25+
are adjacent to the current vertex.
26+
1 indicates an edge between the current vertex
27+
and the neighbour.
28+
colored_vertices: List of current color assignments for all vertices
29+
(-1 means uncolored).
30+
color: The color we are trying to assign to the current vertex.
31+
32+
Returns:
33+
True if the vertex can be safely colored with the given color,
34+
otherwise False.
35+
36+
Examples:
37+
>>> neighbours = [0, 1, 0, 1, 0]
1938
>>> colored_vertices = [0, 2, 1, 2, 0]
20-
2139
>>> color = 1
2240
>>> valid_coloring(neighbours, colored_vertices, color)
2341
True
2442
2543
>>> color = 2
2644
>>> valid_coloring(neighbours, colored_vertices, color)
2745
False
46+
47+
>>> neighbors = [1, 0, 1, 0]
48+
>>> colored_vertices = [-1, -1, -1, -1]
49+
>>> color = 0
50+
>>> valid_coloring(neighbors, colored_vertices, color)
51+
True
2852
"""
29-
# Does any neighbour not satisfy the constraints
53+
# Check if any adjacent vertex has already been colored with the same color
3054
return not any(
3155
neighbour == 1 and colored_vertices[i] == color
3256
for i, neighbour in enumerate(neighbours)
@@ -37,7 +61,7 @@ def util_color(
3761
graph: list[list[int]], max_colors: int, colored_vertices: list[int], index: int
3862
) -> bool:
3963
"""
40-
Pseudo-Code
64+
Recursive function to try and color the graph using backtracking.
4165
4266
Base Case:
4367
1. Check if coloring is complete
@@ -51,6 +75,20 @@ def util_color(
5175
2.4. if current coloring leads to a solution return
5276
2.5. Uncolor given vertex
5377
78+
Parameters:
79+
graph: Adjacency matrix representing the graph.
80+
graph[i][j] is 1 if there is an edge
81+
between vertex i and j.
82+
max_colors: Maximum number of colors allowed (m in the m-coloring problem).
83+
colored_vertices: Current color assignments for each vertex.
84+
-1 indicates that the vertex has not been colored
85+
yet.
86+
index: The current vertex index being processed.
87+
88+
Returns:
89+
True if the graph can be colored using at most max_colors, otherwise False.
90+
91+
Examples:
5492
>>> graph = [[0, 1, 0, 0, 0],
5593
... [1, 0, 1, 0, 1],
5694
... [0, 1, 0, 1, 0],
@@ -67,43 +105,68 @@ def util_color(
67105
>>> util_color(graph, max_colors, colored_vertices, index)
68106
False
69107
"""
70-
71-
# Base Case
108+
# Base Case: If all vertices have been assigned a color, we have a valid solution
72109
if index == len(graph):
73110
return True
74111

75-
# Recursive Step
76-
for i in range(max_colors):
77-
if valid_coloring(graph[index], colored_vertices, i):
78-
# Color current vertex
79-
colored_vertices[index] = i
80-
# Validate coloring
112+
# Try each color for the current vertex
113+
for color in range(max_colors):
114+
# Check if it's valid to color the current vertex with 'color'
115+
if valid_coloring(graph[index], colored_vertices, color):
116+
colored_vertices[index] = color # Assign color
117+
# Recur to color the rest of the vertices
81118
if util_color(graph, max_colors, colored_vertices, index + 1):
82119
return True
83-
# Backtrack
120+
# Backtrack if no solution found with the current assignment
84121
colored_vertices[index] = -1
85-
return False
122+
123+
return False # Return False if no valid coloring is possible
86124

87125

88126
def color(graph: list[list[int]], max_colors: int) -> list[int]:
89127
"""
90-
Wrapper function to call subroutine called util_color
91-
which will either return True or False.
92-
If True is returned colored_vertices list is filled with correct colorings
93-
128+
Attempt to color the graph with at most max_colors colors such that no two adjacent
129+
vertices have the same color.
130+
If it is possible, returns the list of color assignments;
131+
otherwise, returns an empty list.
132+
133+
Parameters:
134+
graph: Adjacency matrix representing the graph.
135+
max_colors: Maximum number of colors allowed.
136+
137+
Returns:
138+
List of color assignments if the graph can be colored using max_colors.
139+
Each index in the list represents the color assigned
140+
to the corresponding vertex.
141+
If coloring is not possible, returns an empty list.
142+
143+
Examples:
94144
>>> graph = [[0, 1, 0, 0, 0],
95145
... [1, 0, 1, 0, 1],
96146
... [0, 1, 0, 1, 0],
97147
... [0, 1, 1, 0, 0],
98148
... [0, 1, 0, 0, 0]]
99-
100149
>>> max_colors = 3
101150
>>> color(graph, max_colors)
102151
[0, 1, 0, 2, 0]
103152
104153
>>> max_colors = 2
105154
>>> color(graph, max_colors)
106155
[]
156+
157+
>>> graph = [[0, 1], [1, 0]] # Simple 2-node graph
158+
>>> max_colors = 2
159+
>>> color(graph, max_colors)
160+
[0, 1]
161+
162+
>>> graph = [[0, 1, 1], [1, 0, 1], [1, 1, 0]] # Complete graph of 3 vertices
163+
>>> max_colors = 2
164+
>>> color(graph, max_colors)
165+
[]
166+
167+
>>> max_colors = 3
168+
>>> color(graph, max_colors)
169+
[0, 1, 2]
107170
>>> color([], 2) # empty graph
108171
[]
109172
>>> color([[0]], 1) # single node, 1 color
@@ -113,9 +176,11 @@ def color(graph: list[list[int]], max_colors: int) -> list[int]:
113176
>>> color([[0, 1], [1, 0]], 2) # 2 nodes, 2 colors (possible)
114177
[0, 1]
115178
"""
179+
# Initialize all vertices as uncolored (-1)
116180
colored_vertices = [-1] * len(graph)
117181

182+
# Use the utility function to try and color the graph starting from vertex 0
118183
if util_color(graph, max_colors, colored_vertices, 0):
119-
return colored_vertices
184+
return colored_vertices # The successful color assignment
120185

121-
return []
186+
return [] # No valid coloring is possible

0 commit comments

Comments
 (0)