Skip to content

Commit 2a99912

Browse files
authored
Merge branch 'master' into patch-2
2 parents c9f99c6 + 140d410 commit 2a99912

2 files changed

Lines changed: 60 additions & 10 deletions

File tree

‎graphs/dijkstra_2.py‎

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,41 @@
11
def print_dist(dist, v) -> None:
2-
print("\nVertex Distance")
2+
"""
3+
Print vertex distances.
4+
>>> print_dist([0.0, 5.0, 8.0, 9.0], 4)
5+
Vertex Distance
6+
0 0
7+
1 5
8+
2 8
9+
3 9
10+
>>> print_dist([0.0, float('inf')], 2)
11+
Vertex Distance
12+
0 0
13+
1 INF
14+
>>> print_dist([0.0], 1)
15+
Vertex Distance
16+
0 0
17+
"""
18+
print("Vertex Distance")
319
for i in range(v):
420
if dist[i] != float("inf"):
5-
print(i, "\t", int(dist[i]), end="\t")
21+
print(i, " ", int(dist[i]))
622
else:
7-
print(i, "\t", "INF", end="\t")
8-
print()
23+
print(i, " ", "INF")
924

1025

1126
def min_dist(mdist, vset, v):
1227
"""
13-
Returns the vertex with the minimum distance from the source vertex\
14-
that has not been visited.
28+
Returns the vertex with the minimum distance from the source vertex
29+
that has not yet been visited.
1530
31+
>>> min_dist([0, 4, 2, float('inf')], [True, False, False, False], 4)
32+
2
33+
>>> min_dist([0, 4, 2, 1], [True, False, True, False], 4)
34+
3
35+
>>> min_dist([0, 4, 2, 1], [True, True, True, True], 4)
36+
-1
37+
>>> min_dist([float('inf'), float('inf')], [False, False], 2)
38+
-1
1639
>>> min_dist([0, 1, 6], [True, False, False], 3)
1740
1
1841
"""
@@ -27,14 +50,38 @@ def min_dist(mdist, vset, v):
2750

2851
def dijkstra(graph, v, src) -> None:
2952
"""
30-
Calculate the shortest path from source to all other vertices\
31-
using Dijkstra's algorithm.
53+
Runs Dijkstra's algorithm and prints distances.
3254
55+
Calculate the shortest path from source to all other vertices
56+
using Dijkstra's algorithm.
57+
58+
>>> g = [
59+
... [0.0, 5.0, float('inf'), 10.0],
60+
... [float('inf'), 0.0, 3.0, float('inf')],
61+
... [float('inf'), float('inf'), 0.0, 1.0],
62+
... [float('inf'), float('inf'), float('inf'), 0.0],
63+
... ]
64+
>>> dijkstra(g, 4, 0)
65+
Vertex Distance
66+
0 0
67+
1 5
68+
2 8
69+
3 9
70+
>>> g2 = [
71+
... [0.0, float('inf')],
72+
... [float('inf'), 0.0],
73+
... ]
74+
>>> dijkstra(g2, 2, 0)
75+
Vertex Distance
76+
0 0
77+
1 INF
78+
>>> dijkstra([[0.0]], 1, 0)
79+
Vertex Distance
80+
0 0
3381
>>> graph = [[0.0, 1.0, 6.0],\
3482
[float("inf"), 0.0, 3.0],\
3583
[float("inf"), float("inf"), 0.0]]
3684
>>> dijkstra(graph, 3, 0) # doctest: +NORMALIZE_WHITESPACE
37-
<BLANKLINE>
3885
Vertex Distance
3986
0 0
4087
1 1

‎maths/number_of_digits.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ def num_digits_fast(n: int) -> int:
4343
5
4444
>>> num_digits_fast(123)
4545
3
46+
>>> num_digits_fast(1000)
47+
4
48+
>>> num_digits_fast(10**15)
49+
16
4650
>>> num_digits_fast(0)
4751
1
4852
>>> num_digits_fast(-1)
@@ -56,7 +60,6 @@ def num_digits_fast(n: int) -> int:
5660
"""
5761
if not isinstance(n, int):
5862
raise TypeError("Input must be an integer")
59-
6063
return 1 if n == 0 else math.floor(math.log10(abs(n)) + 1)
6164

6265

0 commit comments

Comments
 (0)