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 44f173f

Browse files
committedDec 24, 2024·
Merge branch 'master' into fix-sphinx/build_docs-warning-for-physics/speeds_of_gas_molecules
2 parents 05921b5 + ae28fa7 commit 44f173f

File tree

3 files changed

+84
-60
lines changed

3 files changed

+84
-60
lines changed
 

‎data_structures/binary_tree/mirror_binary_tree.py

+29-23
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ def mirror(self) -> Node:
5656
def make_tree_seven() -> Node:
5757
r"""
5858
Return a binary tree with 7 nodes that looks like this:
59+
::
60+
5961
1
6062
/ \
6163
2 3
@@ -81,13 +83,15 @@ def make_tree_seven() -> Node:
8183
def make_tree_nine() -> Node:
8284
r"""
8385
Return a binary tree with 9 nodes that looks like this:
84-
1
85-
/ \
86-
2 3
87-
/ \ \
88-
4 5 6
89-
/ \ \
90-
7 8 9
86+
::
87+
88+
1
89+
/ \
90+
2 3
91+
/ \ \
92+
4 5 6
93+
/ \ \
94+
7 8 9
9195
9296
>>> tree_nine = make_tree_nine()
9397
>>> len(tree_nine)
@@ -117,23 +121,25 @@ def main() -> None:
117121
>>> tuple(tree.mirror())
118122
(6, 3, 1, 9, 5, 2, 8, 4, 7)
119123
120-
nine_tree:
121-
1
122-
/ \
123-
2 3
124-
/ \ \
125-
4 5 6
126-
/ \ \
127-
7 8 9
128-
129-
The mirrored tree looks like this:
124+
nine_tree::
125+
126+
1
127+
/ \
128+
2 3
129+
/ \ \
130+
4 5 6
131+
/ \ \
132+
7 8 9
133+
134+
The mirrored tree looks like this::
135+
130136
1
131-
/ \
132-
3 2
133-
/ / \
134-
6 5 4
135-
/ / \
136-
9 8 7
137+
/ \
138+
3 2
139+
/ / \
140+
6 5 4
141+
/ / \
142+
9 8 7
137143
"""
138144
trees = {"zero": Node(0), "seven": make_tree_seven(), "nine": make_tree_nine()}
139145
for name, tree in trees.items():

‎graphs/check_bipatrite.py

+14-10
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]})

‎physics/horizontal_projectile_motion.py

+41-27
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.