Skip to content

Commit 455509a

Browse files
pmbacclauss
authored andcommittedOct 18, 2019
Add Topological Sort (TheAlgorithms#1302)
* add topological sort * fix topological sort? * running black * renaming file
1 parent ddb0949 commit 455509a

8 files changed

+84
-27
lines changed
 

‎blockchain/chinese_remainder_theorem.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def chinese_remainder_theorem(n1, r1, n2, r2):
4444
(x, y) = extended_euclid(n1, n2)
4545
m = n1 * n2
4646
n = r2 * x * n1 + r1 * y * n2
47-
return ((n % m + m) % m)
47+
return (n % m + m) % m
4848

4949

5050
# ----------SAME SOLUTION USING InvertModulo instead ExtendedEuclid----------------
@@ -84,8 +84,8 @@ def chinese_remainder_theorem2(n1, r1, n2, r2):
8484
# import testmod for testing our function
8585
from doctest import testmod
8686

87-
if __name__ == '__main__':
88-
testmod(name='chinese_remainder_theorem', verbose=True)
89-
testmod(name='chinese_remainder_theorem2', verbose=True)
90-
testmod(name='invert_modulo', verbose=True)
91-
testmod(name='extended_euclid', verbose=True)
87+
if __name__ == "__main__":
88+
testmod(name="chinese_remainder_theorem", verbose=True)
89+
testmod(name="chinese_remainder_theorem2", verbose=True)
90+
testmod(name="invert_modulo", verbose=True)
91+
testmod(name="extended_euclid", verbose=True)

‎blockchain/diophantine_equation.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ def diophantine(a, b, c):
1717
1818
"""
1919

20-
assert c % greatest_common_divisor(a, b) == 0 # greatest_common_divisor(a,b) function implemented below
20+
assert (
21+
c % greatest_common_divisor(a, b) == 0
22+
) # greatest_common_divisor(a,b) function implemented below
2123
(d, x, y) = extended_gcd(a, b) # extended_gcd(a,b) function implemented below
2224
r = c / d
2325
return (r * x, r * y)
@@ -32,6 +34,7 @@ def diophantine(a, b, c):
3234

3335
# n is the number of solution you want, n = 2 by default
3436

37+
3538
def diophantine_all_soln(a, b, c, n=2):
3639
"""
3740
>>> diophantine_all_soln(10, 6, 14)
@@ -66,6 +69,7 @@ def diophantine_all_soln(a, b, c, n=2):
6669

6770
# Euclid's Algorithm
6871

72+
6973
def greatest_common_divisor(a, b):
7074
"""
7175
>>> greatest_common_divisor(7,5)
@@ -117,8 +121,8 @@ def extended_gcd(a, b):
117121
# import testmod for testing our function
118122
from doctest import testmod
119123

120-
if __name__ == '__main__':
121-
testmod(name='diophantine', verbose=True)
122-
testmod(name='diophantine_all_soln', verbose=True)
123-
testmod(name='extended_gcd', verbose=True)
124-
testmod(name='greatest_common_divisor', verbose=True)
124+
if __name__ == "__main__":
125+
testmod(name="diophantine", verbose=True)
126+
testmod(name="diophantine_all_soln", verbose=True)
127+
testmod(name="extended_gcd", verbose=True)
128+
testmod(name="greatest_common_divisor", verbose=True)

‎blockchain/modular_division.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ def modular_division2(a, b, n):
7070

7171
# Extended Euclid's Algorithm : If d divides a and b and d = a*x + b*y for integers x and y, then d = gcd(a,b)
7272

73+
7374
def extended_gcd(a, b):
7475
"""
7576
>>> extended_gcd(10, 6)
@@ -116,6 +117,7 @@ def extended_euclid(a, b):
116117
# Euclid's Lemma : d divides a and b, if and only if d divides a-b and b
117118
# Euclid's Algorithm
118119

120+
119121
def greatest_common_divisor(a, b):
120122
"""
121123
>>> greatest_common_divisor(7,5)
@@ -140,10 +142,10 @@ def greatest_common_divisor(a, b):
140142
# Import testmod for testing our function
141143
from doctest import testmod
142144

143-
if __name__ == '__main__':
144-
testmod(name='modular_division', verbose=True)
145-
testmod(name='modular_division2', verbose=True)
146-
testmod(name='invert_modulo', verbose=True)
147-
testmod(name='extended_gcd', verbose=True)
148-
testmod(name='extended_euclid', verbose=True)
149-
testmod(name='greatest_common_divisor', verbose=True)
145+
if __name__ == "__main__":
146+
testmod(name="modular_division", verbose=True)
147+
testmod(name="modular_division2", verbose=True)
148+
testmod(name="invert_modulo", verbose=True)
149+
testmod(name="extended_gcd", verbose=True)
150+
testmod(name="extended_euclid", verbose=True)
151+
testmod(name="greatest_common_divisor", verbose=True)

‎graphs/g_topological_sort.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Author: Phyllipe Bezerra (https://github.com/pmba)
2+
3+
clothes = {
4+
0: "underwear",
5+
1: "pants",
6+
2: "belt",
7+
3: "suit",
8+
4: "shoe",
9+
5: "socks",
10+
6: "shirt",
11+
7: "tie",
12+
8: "clock",
13+
}
14+
15+
graph = [[1, 4], [2, 4], [3], [], [], [4], [2, 7], [3], []]
16+
17+
visited = [0 for x in range(len(graph))]
18+
stack = []
19+
20+
21+
def print_stack(stack, clothes):
22+
order = 1
23+
while stack:
24+
cur_clothe = stack.pop()
25+
print(order, clothes[cur_clothe])
26+
order += 1
27+
28+
29+
def dfs(u, visited, graph):
30+
visited[u] = 1
31+
for v in graph[u]:
32+
if not visited[v]:
33+
dfs(v, visited, graph)
34+
35+
stack.append(u)
36+
37+
38+
def top_sort(graph, visited):
39+
for v in range(len(graph)):
40+
if not visited[v]:
41+
dfs(v, visited, graph)
42+
43+
44+
if __name__ == "__main__":
45+
top_sort(graph, visited)
46+
print(stack)
47+
print_stack(stack, clothes)

‎machine_learning/k_nearest_neighbours.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55

66
data = datasets.load_iris()
77

8-
X = np.array(data['data'])
9-
y = np.array(data['target'])
10-
classes = data['target_names']
8+
X = np.array(data["data"])
9+
y = np.array(data["target"])
10+
classes = data["target_names"]
1111

1212
X_train, X_test, y_train, y_test = train_test_split(X, y)
1313

14+
1415
def euclidean_distance(a, b):
1516
"""
1617
Gives the euclidean distance between two points
@@ -21,6 +22,7 @@ def euclidean_distance(a, b):
2122
"""
2223
return np.linalg.norm(np.array(a) - np.array(b))
2324

25+
2426
def classifier(train_data, train_target, classes, point, k=5):
2527
"""
2628
Classifies the point using the KNN algorithm
@@ -43,13 +45,13 @@ def classifier(train_data, train_target, classes, point, k=5):
4345
for data_point in data:
4446
distance = euclidean_distance(data_point[0], point)
4547
distances.append((distance, data_point[1]))
46-
# Choosing 'k' points with the least distances.
48+
# Choosing 'k' points with the least distances.
4749
votes = [i[1] for i in sorted(distances)[:k]]
48-
# Most commonly occuring class among them
50+
# Most commonly occuring class among them
4951
# is the class into which the point is classified
5052
result = Counter(votes).most_common(1)[0][0]
5153
return classes[result]
5254

5355

5456
if __name__ == "__main__":
55-
print(classifier(X_train, y_train, classes, [4.4, 3.1, 1.3, 1.4]))
57+
print(classifier(X_train, y_train, classes, [4.4, 3.1, 1.3, 1.4]))

‎maths/fibonacci_sequence_recursion.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def recur_fibo(n):
66
>>> [recur_fibo(i) for i in range(12)]
77
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
88
"""
9-
return n if n <= 1 else recur_fibo(n-1) + recur_fibo(n-2)
9+
return n if n <= 1 else recur_fibo(n - 1) + recur_fibo(n - 2)
1010

1111

1212
def main():

‎maths/sum_of_arithmetic_series.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def sum_of_series(first_term, common_diff, num_of_terms):
88
>>> sum_of_series(1, 10, 100)
99
49600.0
1010
"""
11-
sum = ((num_of_terms/2)*(2*first_term+(num_of_terms-1)*common_diff))
11+
sum = (num_of_terms / 2) * (2 * first_term + (num_of_terms - 1) * common_diff)
1212
# formula for sum of series
1313
return sum
1414

@@ -19,4 +19,5 @@ def main():
1919

2020
if __name__ == "__main__":
2121
import doctest
22+
2223
doctest.testmod()

‎sorts/stooge_sort.py

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def stooge(arr, i, h):
3333
# Recursively sort first 2/3 elements
3434
stooge(arr, i, (h - t))
3535

36+
3637
if __name__ == "__main__":
3738
user_input = input("Enter numbers separated by a comma:\n").strip()
3839
unsorted = [int(item) for item in user_input.split(",")]

0 commit comments

Comments
 (0)
Please sign in to comment.