Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 65ab6e2

Browse files
committedDec 24, 2024·
Merge branch 'master' into fix-sphinx/build_docs-warning-for-data_structures/binary_tree/mirror_binary_tree
2 parents d7c4aaf + c36aaf0 commit 65ab6e2

File tree

3 files changed

+150
-91
lines changed

3 files changed

+150
-91
lines changed
 

‎graphs/check_bipatrite.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@ def is_bipartite_dfs(graph: defaultdict[int, list[int]]) -> bool:
66
Check if a graph is bipartite using depth-first search (DFS).
77
88
Args:
9-
graph: Adjacency list representing the graph.
9+
`graph`: Adjacency list representing the graph.
1010
1111
Returns:
12-
True if bipartite, False otherwise.
12+
``True`` if bipartite, ``False`` otherwise.
1313
1414
Checks if the graph can be divided into two sets of vertices, such that no two
1515
vertices within the same set are connected by an edge.
1616
1717
Examples:
18-
# FIXME: This test should pass.
18+
19+
>>> # FIXME: This test should pass.
1920
>>> is_bipartite_dfs(defaultdict(list, {0: [1, 2], 1: [0, 3], 2: [0, 4]}))
2021
Traceback (most recent call last):
2122
...
@@ -37,7 +38,7 @@ def is_bipartite_dfs(graph: defaultdict[int, list[int]]) -> bool:
3738
...
3839
KeyError: 0
3940
40-
# FIXME: This test should fails with KeyError: 4.
41+
>>> # FIXME: This test should fails with KeyError: 4.
4142
>>> is_bipartite_dfs({0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 9: [0]})
4243
False
4344
>>> is_bipartite_dfs({0: [-1, 3], 1: [0, -2]})
@@ -51,7 +52,8 @@ def is_bipartite_dfs(graph: defaultdict[int, list[int]]) -> bool:
5152
...
5253
KeyError: 0
5354
54-
# FIXME: This test should fails with TypeError: list indices must be integers or...
55+
>>> # FIXME: This test should fails with
56+
>>> # TypeError: list indices must be integers or...
5557
>>> is_bipartite_dfs({0: [1.0, 3.0], 1.0: [0, 2.0], 2.0: [1.0, 3.0], 3.0: [0, 2.0]})
5658
True
5759
>>> is_bipartite_dfs({"a": [1, 3], "b": [0, 2], "c": [1, 3], "d": [0, 2]})
@@ -95,16 +97,17 @@ def is_bipartite_bfs(graph: defaultdict[int, list[int]]) -> bool:
9597
Check if a graph is bipartite using a breadth-first search (BFS).
9698
9799
Args:
98-
graph: Adjacency list representing the graph.
100+
`graph`: Adjacency list representing the graph.
99101
100102
Returns:
101-
True if bipartite, False otherwise.
103+
``True`` if bipartite, ``False`` otherwise.
102104
103105
Check if the graph can be divided into two sets of vertices, such that no two
104106
vertices within the same set are connected by an edge.
105107
106108
Examples:
107-
# FIXME: This test should pass.
109+
110+
>>> # FIXME: This test should pass.
108111
>>> is_bipartite_bfs(defaultdict(list, {0: [1, 2], 1: [0, 3], 2: [0, 4]}))
109112
Traceback (most recent call last):
110113
...
@@ -126,7 +129,7 @@ def is_bipartite_bfs(graph: defaultdict[int, list[int]]) -> bool:
126129
...
127130
KeyError: 0
128131
129-
# FIXME: This test should fails with KeyError: 4.
132+
>>> # FIXME: This test should fails with KeyError: 4.
130133
>>> is_bipartite_bfs({0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 9: [0]})
131134
False
132135
>>> is_bipartite_bfs({0: [-1, 3], 1: [0, -2]})
@@ -140,7 +143,8 @@ def is_bipartite_bfs(graph: defaultdict[int, list[int]]) -> bool:
140143
...
141144
KeyError: 0
142145
143-
# FIXME: This test should fails with TypeError: list indices must be integers or...
146+
>>> # FIXME: This test should fails with
147+
>>> # TypeError: list indices must be integers or...
144148
>>> is_bipartite_bfs({0: [1.0, 3.0], 1.0: [0, 2.0], 2.0: [1.0, 3.0], 3.0: [0, 2.0]})
145149
True
146150
>>> is_bipartite_bfs({"a": [1, 3], "b": [0, 2], "c": [1, 3], "d": [0, 2]})

‎maths/volume.py

Lines changed: 95 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""
22
Find the volume of various shapes.
3+
34
* https://en.wikipedia.org/wiki/Volume
45
* https://en.wikipedia.org/wiki/Spherical_cap
56
"""
@@ -12,6 +13,7 @@
1213
def vol_cube(side_length: float) -> float:
1314
"""
1415
Calculate the Volume of a Cube.
16+
1517
>>> vol_cube(1)
1618
1.0
1719
>>> vol_cube(3)
@@ -33,6 +35,7 @@ def vol_cube(side_length: float) -> float:
3335
def vol_spherical_cap(height: float, radius: float) -> float:
3436
"""
3537
Calculate the volume of the spherical cap.
38+
3639
>>> vol_spherical_cap(1, 2)
3740
5.235987755982988
3841
>>> vol_spherical_cap(1.6, 2.6)
@@ -57,20 +60,29 @@ def vol_spherical_cap(height: float, radius: float) -> float:
5760
def vol_spheres_intersect(
5861
radius_1: float, radius_2: float, centers_distance: float
5962
) -> float:
60-
"""
63+
r"""
6164
Calculate the volume of the intersection of two spheres.
65+
6266
The intersection is composed by two spherical caps and therefore its volume is the
63-
sum of the volumes of the spherical caps. First, it calculates the heights (h1, h2)
64-
of the spherical caps, then the two volumes and it returns the sum.
67+
sum of the volumes of the spherical caps.
68+
First, it calculates the heights :math:`(h_1, h_2)` of the spherical caps,
69+
then the two volumes and it returns the sum.
6570
The height formulas are
66-
h1 = (radius_1 - radius_2 + centers_distance)
67-
* (radius_1 + radius_2 - centers_distance)
68-
/ (2 * centers_distance)
69-
h2 = (radius_2 - radius_1 + centers_distance)
70-
* (radius_2 + radius_1 - centers_distance)
71-
/ (2 * centers_distance)
72-
if centers_distance is 0 then it returns the volume of the smallers sphere
73-
:return vol_spherical_cap(h1, radius_2) + vol_spherical_cap(h2, radius_1)
71+
72+
.. math::
73+
h_1 = \frac{(radius_1 - radius_2 + centers\_distance)
74+
\cdot (radius_1 + radius_2 - centers\_distance)}
75+
{2 \cdot centers\_distance}
76+
77+
h_2 = \frac{(radius_2 - radius_1 + centers\_distance)
78+
\cdot (radius_2 + radius_1 - centers\_distance)}
79+
{2 \cdot centers\_distance}
80+
81+
if `centers_distance` is 0 then it returns the volume of the smallers sphere
82+
83+
:return: ``vol_spherical_cap`` (:math:`h_1`, :math:`radius_2`)
84+
+ ``vol_spherical_cap`` (:math:`h_2`, :math:`radius_1`)
85+
7486
>>> vol_spheres_intersect(2, 2, 1)
7587
21.205750411731103
7688
>>> vol_spheres_intersect(2.6, 2.6, 1.6)
@@ -112,14 +124,18 @@ def vol_spheres_intersect(
112124
def vol_spheres_union(
113125
radius_1: float, radius_2: float, centers_distance: float
114126
) -> float:
115-
"""
127+
r"""
116128
Calculate the volume of the union of two spheres that possibly intersect.
117-
It is the sum of sphere A and sphere B minus their intersection.
118-
First, it calculates the volumes (v1, v2) of the spheres,
119-
then the volume of the intersection (i) and it returns the sum v1+v2-i.
120-
If centers_distance is 0 then it returns the volume of the larger sphere
121-
:return vol_sphere(radius_1) + vol_sphere(radius_2)
122-
- vol_spheres_intersect(radius_1, radius_2, centers_distance)
129+
130+
It is the sum of sphere :math:`A` and sphere :math:`B` minus their intersection.
131+
First, it calculates the volumes :math:`(v_1, v_2)` of the spheres,
132+
then the volume of the intersection :math:`i` and
133+
it returns the sum :math:`v_1 + v_2 - i`.
134+
If `centers_distance` is 0 then it returns the volume of the larger sphere
135+
136+
:return: ``vol_sphere`` (:math:`radius_1`) + ``vol_sphere`` (:math:`radius_2`)
137+
- ``vol_spheres_intersect``
138+
(:math:`radius_1`, :math:`radius_2`, :math:`centers\_distance`)
123139
124140
>>> vol_spheres_union(2, 2, 1)
125141
45.814892864851146
@@ -157,7 +173,9 @@ def vol_spheres_union(
157173
def vol_cuboid(width: float, height: float, length: float) -> float:
158174
"""
159175
Calculate the Volume of a Cuboid.
160-
:return multiple of width, length and height
176+
177+
:return: multiple of `width`, `length` and `height`
178+
161179
>>> vol_cuboid(1, 1, 1)
162180
1.0
163181
>>> vol_cuboid(1, 2, 3)
@@ -185,10 +203,12 @@ def vol_cuboid(width: float, height: float, length: float) -> float:
185203

186204

187205
def vol_cone(area_of_base: float, height: float) -> float:
188-
"""
189-
Calculate the Volume of a Cone.
190-
Wikipedia reference: https://en.wikipedia.org/wiki/Cone
191-
:return (1/3) * area_of_base * height
206+
r"""
207+
| Calculate the Volume of a Cone.
208+
| Wikipedia reference: https://en.wikipedia.org/wiki/Cone
209+
210+
:return: :math:`\frac{1}{3} \cdot area\_of\_base \cdot height`
211+
192212
>>> vol_cone(10, 3)
193213
10.0
194214
>>> vol_cone(1, 1)
@@ -212,10 +232,12 @@ def vol_cone(area_of_base: float, height: float) -> float:
212232

213233

214234
def vol_right_circ_cone(radius: float, height: float) -> float:
215-
"""
216-
Calculate the Volume of a Right Circular Cone.
217-
Wikipedia reference: https://en.wikipedia.org/wiki/Cone
218-
:return (1/3) * pi * radius^2 * height
235+
r"""
236+
| Calculate the Volume of a Right Circular Cone.
237+
| Wikipedia reference: https://en.wikipedia.org/wiki/Cone
238+
239+
:return: :math:`\frac{1}{3} \cdot \pi \cdot radius^2 \cdot height`
240+
219241
>>> vol_right_circ_cone(2, 3)
220242
12.566370614359172
221243
>>> vol_right_circ_cone(0, 0)
@@ -237,10 +259,12 @@ def vol_right_circ_cone(radius: float, height: float) -> float:
237259

238260

239261
def vol_prism(area_of_base: float, height: float) -> float:
240-
"""
241-
Calculate the Volume of a Prism.
242-
Wikipedia reference: https://en.wikipedia.org/wiki/Prism_(geometry)
243-
:return V = Bh
262+
r"""
263+
| Calculate the Volume of a Prism.
264+
| Wikipedia reference: https://en.wikipedia.org/wiki/Prism_(geometry)
265+
266+
:return: :math:`V = B \cdot h`
267+
244268
>>> vol_prism(10, 2)
245269
20.0
246270
>>> vol_prism(11, 1)
@@ -264,10 +288,12 @@ def vol_prism(area_of_base: float, height: float) -> float:
264288

265289

266290
def vol_pyramid(area_of_base: float, height: float) -> float:
267-
"""
268-
Calculate the Volume of a Pyramid.
269-
Wikipedia reference: https://en.wikipedia.org/wiki/Pyramid_(geometry)
270-
:return (1/3) * Bh
291+
r"""
292+
| Calculate the Volume of a Pyramid.
293+
| Wikipedia reference: https://en.wikipedia.org/wiki/Pyramid_(geometry)
294+
295+
:return: :math:`\frac{1}{3} \cdot B \cdot h`
296+
271297
>>> vol_pyramid(10, 3)
272298
10.0
273299
>>> vol_pyramid(1.5, 3)
@@ -291,10 +317,12 @@ def vol_pyramid(area_of_base: float, height: float) -> float:
291317

292318

293319
def vol_sphere(radius: float) -> float:
294-
"""
295-
Calculate the Volume of a Sphere.
296-
Wikipedia reference: https://en.wikipedia.org/wiki/Sphere
297-
:return (4/3) * pi * r^3
320+
r"""
321+
| Calculate the Volume of a Sphere.
322+
| Wikipedia reference: https://en.wikipedia.org/wiki/Sphere
323+
324+
:return: :math:`\frac{4}{3} \cdot \pi \cdot r^3`
325+
298326
>>> vol_sphere(5)
299327
523.5987755982989
300328
>>> vol_sphere(1)
@@ -315,10 +343,13 @@ def vol_sphere(radius: float) -> float:
315343

316344

317345
def vol_hemisphere(radius: float) -> float:
318-
"""Calculate the volume of a hemisphere
319-
Wikipedia reference: https://en.wikipedia.org/wiki/Hemisphere
320-
Other references: https://www.cuemath.com/geometry/hemisphere
321-
:return 2/3 * pi * radius^3
346+
r"""
347+
| Calculate the volume of a hemisphere
348+
| Wikipedia reference: https://en.wikipedia.org/wiki/Hemisphere
349+
| Other references: https://www.cuemath.com/geometry/hemisphere
350+
351+
:return: :math:`\frac{2}{3} \cdot \pi \cdot radius^3`
352+
322353
>>> vol_hemisphere(1)
323354
2.0943951023931953
324355
>>> vol_hemisphere(7)
@@ -339,9 +370,12 @@ def vol_hemisphere(radius: float) -> float:
339370

340371

341372
def vol_circular_cylinder(radius: float, height: float) -> float:
342-
"""Calculate the Volume of a Circular Cylinder.
343-
Wikipedia reference: https://en.wikipedia.org/wiki/Cylinder
344-
:return pi * radius^2 * height
373+
r"""
374+
| Calculate the Volume of a Circular Cylinder.
375+
| Wikipedia reference: https://en.wikipedia.org/wiki/Cylinder
376+
377+
:return: :math:`\pi \cdot radius^2 \cdot height`
378+
345379
>>> vol_circular_cylinder(1, 1)
346380
3.141592653589793
347381
>>> vol_circular_cylinder(4, 3)
@@ -368,7 +402,9 @@ def vol_circular_cylinder(radius: float, height: float) -> float:
368402
def vol_hollow_circular_cylinder(
369403
inner_radius: float, outer_radius: float, height: float
370404
) -> float:
371-
"""Calculate the Volume of a Hollow Circular Cylinder.
405+
"""
406+
Calculate the Volume of a Hollow Circular Cylinder.
407+
372408
>>> vol_hollow_circular_cylinder(1, 2, 3)
373409
28.274333882308138
374410
>>> vol_hollow_circular_cylinder(1.6, 2.6, 3.6)
@@ -405,8 +441,9 @@ def vol_hollow_circular_cylinder(
405441

406442

407443
def vol_conical_frustum(height: float, radius_1: float, radius_2: float) -> float:
408-
"""Calculate the Volume of a Conical Frustum.
409-
Wikipedia reference: https://en.wikipedia.org/wiki/Frustum
444+
"""
445+
| Calculate the Volume of a Conical Frustum.
446+
| Wikipedia reference: https://en.wikipedia.org/wiki/Frustum
410447
411448
>>> vol_conical_frustum(45, 7, 28)
412449
48490.482608158454
@@ -443,9 +480,12 @@ def vol_conical_frustum(height: float, radius_1: float, radius_2: float) -> floa
443480

444481

445482
def vol_torus(torus_radius: float, tube_radius: float) -> float:
446-
"""Calculate the Volume of a Torus.
447-
Wikipedia reference: https://en.wikipedia.org/wiki/Torus
448-
:return 2pi^2 * torus_radius * tube_radius^2
483+
r"""
484+
| Calculate the Volume of a Torus.
485+
| Wikipedia reference: https://en.wikipedia.org/wiki/Torus
486+
487+
:return: :math:`2 \pi^2 \cdot torus\_radius \cdot tube\_radius^2`
488+
449489
>>> vol_torus(1, 1)
450490
19.739208802178716
451491
>>> vol_torus(4, 3)
@@ -471,8 +511,9 @@ def vol_torus(torus_radius: float, tube_radius: float) -> float:
471511

472512

473513
def vol_icosahedron(tri_side: float) -> float:
474-
"""Calculate the Volume of an Icosahedron.
475-
Wikipedia reference: https://en.wikipedia.org/wiki/Regular_icosahedron
514+
"""
515+
| Calculate the Volume of an Icosahedron.
516+
| Wikipedia reference: https://en.wikipedia.org/wiki/Regular_icosahedron
476517
477518
>>> from math import isclose
478519
>>> isclose(vol_icosahedron(2.5), 34.088984228514256)

‎physics/horizontal_projectile_motion.py

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
"""
22
Horizontal Projectile Motion problem in physics.
3+
34
This algorithm solves a specific problem in which
4-
the motion starts from the ground as can be seen below:
5-
(v = 0)
6-
* *
7-
* *
8-
* *
9-
* *
10-
* *
11-
* *
12-
GROUND GROUND
5+
the motion starts from the ground as can be seen below::
6+
7+
(v = 0)
8+
* *
9+
* *
10+
* *
11+
* *
12+
* *
13+
* *
14+
GROUND GROUND
15+
1316
For more info: https://en.wikipedia.org/wiki/Projectile_motion
1417
"""
1518

@@ -43,14 +46,17 @@ def check_args(init_velocity: float, angle: float) -> None:
4346

4447

4548
def horizontal_distance(init_velocity: float, angle: float) -> float:
46-
"""
49+
r"""
4750
Returns the horizontal distance that the object cover
51+
4852
Formula:
49-
v_0^2 * sin(2 * alpha)
50-
---------------------
51-
g
52-
v_0 - initial velocity
53-
alpha - angle
53+
.. math::
54+
\frac{v_0^2 \cdot \sin(2 \alpha)}{g}
55+
56+
v_0 - \text{initial velocity}
57+
58+
\alpha - \text{angle}
59+
5460
>>> horizontal_distance(30, 45)
5561
91.77
5662
>>> horizontal_distance(100, 78)
@@ -70,14 +76,17 @@ def horizontal_distance(init_velocity: float, angle: float) -> float:
7076

7177

7278
def max_height(init_velocity: float, angle: float) -> float:
73-
"""
79+
r"""
7480
Returns the maximum height that the object reach
81+
7582
Formula:
76-
v_0^2 * sin^2(alpha)
77-
--------------------
78-
2g
79-
v_0 - initial velocity
80-
alpha - angle
83+
.. math::
84+
\frac{v_0^2 \cdot \sin^2 (\alpha)}{2 g}
85+
86+
v_0 - \text{initial velocity}
87+
88+
\alpha - \text{angle}
89+
8190
>>> max_height(30, 45)
8291
22.94
8392
>>> max_height(100, 78)
@@ -97,14 +106,17 @@ def max_height(init_velocity: float, angle: float) -> float:
97106

98107

99108
def total_time(init_velocity: float, angle: float) -> float:
100-
"""
109+
r"""
101110
Returns total time of the motion
111+
102112
Formula:
103-
2 * v_0 * sin(alpha)
104-
--------------------
105-
g
106-
v_0 - initial velocity
107-
alpha - angle
113+
.. math::
114+
\frac{2 v_0 \cdot \sin (\alpha)}{g}
115+
116+
v_0 - \text{initial velocity}
117+
118+
\alpha - \text{angle}
119+
108120
>>> total_time(30, 45)
109121
4.33
110122
>>> total_time(100, 78)
@@ -125,6 +137,8 @@ def total_time(init_velocity: float, angle: float) -> float:
125137

126138
def test_motion() -> None:
127139
"""
140+
Test motion
141+
128142
>>> test_motion()
129143
"""
130144
v0, angle = 25, 20

0 commit comments

Comments
 (0)
Please sign in to comment.