diff --git a/blockchain/chinese_remainder_theorem.py b/blockchain/chinese_remainder_theorem.py index f1409530a..8c3eb9b4b 100644 --- a/blockchain/chinese_remainder_theorem.py +++ b/blockchain/chinese_remainder_theorem.py @@ -44,7 +44,7 @@ def chinese_remainder_theorem(n1, r1, n2, r2): (x, y) = extended_euclid(n1, n2) m = n1 * n2 n = r2 * x * n1 + r1 * y * n2 - return ((n % m + m) % m) + return (n % m + m) % m # ----------SAME SOLUTION USING InvertModulo instead ExtendedEuclid---------------- @@ -84,8 +84,8 @@ def chinese_remainder_theorem2(n1, r1, n2, r2): # import testmod for testing our function from doctest import testmod -if __name__ == '__main__': - testmod(name='chinese_remainder_theorem', verbose=True) - testmod(name='chinese_remainder_theorem2', verbose=True) - testmod(name='invert_modulo', verbose=True) - testmod(name='extended_euclid', verbose=True) +if __name__ == "__main__": + testmod(name="chinese_remainder_theorem", verbose=True) + testmod(name="chinese_remainder_theorem2", verbose=True) + testmod(name="invert_modulo", verbose=True) + testmod(name="extended_euclid", verbose=True) diff --git a/blockchain/diophantine_equation.py b/blockchain/diophantine_equation.py index 3ac7094ee..ec2ed26e4 100644 --- a/blockchain/diophantine_equation.py +++ b/blockchain/diophantine_equation.py @@ -17,7 +17,9 @@ def diophantine(a, b, c): """ - assert c % greatest_common_divisor(a, b) == 0 # greatest_common_divisor(a,b) function implemented below + assert ( + c % greatest_common_divisor(a, b) == 0 + ) # greatest_common_divisor(a,b) function implemented below (d, x, y) = extended_gcd(a, b) # extended_gcd(a,b) function implemented below r = c / d return (r * x, r * y) @@ -32,6 +34,7 @@ def diophantine(a, b, c): # n is the number of solution you want, n = 2 by default + def diophantine_all_soln(a, b, c, n=2): """ >>> diophantine_all_soln(10, 6, 14) @@ -66,6 +69,7 @@ def diophantine_all_soln(a, b, c, n=2): # Euclid's Algorithm + def greatest_common_divisor(a, b): """ >>> greatest_common_divisor(7,5) @@ -117,8 +121,8 @@ def extended_gcd(a, b): # import testmod for testing our function from doctest import testmod -if __name__ == '__main__': - testmod(name='diophantine', verbose=True) - testmod(name='diophantine_all_soln', verbose=True) - testmod(name='extended_gcd', verbose=True) - testmod(name='greatest_common_divisor', verbose=True) +if __name__ == "__main__": + testmod(name="diophantine", verbose=True) + testmod(name="diophantine_all_soln", verbose=True) + testmod(name="extended_gcd", verbose=True) + testmod(name="greatest_common_divisor", verbose=True) diff --git a/blockchain/modular_division.py b/blockchain/modular_division.py index 4e1623fbe..1255f0432 100644 --- a/blockchain/modular_division.py +++ b/blockchain/modular_division.py @@ -70,6 +70,7 @@ def modular_division2(a, b, n): # 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) + def extended_gcd(a, b): """ >>> extended_gcd(10, 6) @@ -116,6 +117,7 @@ def extended_euclid(a, b): # Euclid's Lemma : d divides a and b, if and only if d divides a-b and b # Euclid's Algorithm + def greatest_common_divisor(a, b): """ >>> greatest_common_divisor(7,5) @@ -140,10 +142,10 @@ def greatest_common_divisor(a, b): # Import testmod for testing our function from doctest import testmod -if __name__ == '__main__': - testmod(name='modular_division', verbose=True) - testmod(name='modular_division2', verbose=True) - testmod(name='invert_modulo', verbose=True) - testmod(name='extended_gcd', verbose=True) - testmod(name='extended_euclid', verbose=True) - testmod(name='greatest_common_divisor', verbose=True) +if __name__ == "__main__": + testmod(name="modular_division", verbose=True) + testmod(name="modular_division2", verbose=True) + testmod(name="invert_modulo", verbose=True) + testmod(name="extended_gcd", verbose=True) + testmod(name="extended_euclid", verbose=True) + testmod(name="greatest_common_divisor", verbose=True) diff --git a/graphs/g_topological_sort.py b/graphs/g_topological_sort.py new file mode 100644 index 000000000..1a2f4fa11 --- /dev/null +++ b/graphs/g_topological_sort.py @@ -0,0 +1,47 @@ +# Author: Phyllipe Bezerra (https://github.com/pmba) + +clothes = { + 0: "underwear", + 1: "pants", + 2: "belt", + 3: "suit", + 4: "shoe", + 5: "socks", + 6: "shirt", + 7: "tie", + 8: "clock", +} + +graph = [[1, 4], [2, 4], [3], [], [], [4], [2, 7], [3], []] + +visited = [0 for x in range(len(graph))] +stack = [] + + +def print_stack(stack, clothes): + order = 1 + while stack: + cur_clothe = stack.pop() + print(order, clothes[cur_clothe]) + order += 1 + + +def dfs(u, visited, graph): + visited[u] = 1 + for v in graph[u]: + if not visited[v]: + dfs(v, visited, graph) + + stack.append(u) + + +def top_sort(graph, visited): + for v in range(len(graph)): + if not visited[v]: + dfs(v, visited, graph) + + +if __name__ == "__main__": + top_sort(graph, visited) + print(stack) + print_stack(stack, clothes) diff --git a/machine_learning/k_nearest_neighbours.py b/machine_learning/k_nearest_neighbours.py index 83d8399fe..a60b744bc 100644 --- a/machine_learning/k_nearest_neighbours.py +++ b/machine_learning/k_nearest_neighbours.py @@ -5,12 +5,13 @@ from sklearn.model_selection import train_test_split data = datasets.load_iris() -X = np.array(data['data']) -y = np.array(data['target']) -classes = data['target_names'] +X = np.array(data["data"]) +y = np.array(data["target"]) +classes = data["target_names"] X_train, X_test, y_train, y_test = train_test_split(X, y) + def euclidean_distance(a, b): """ Gives the euclidean distance between two points @@ -21,6 +22,7 @@ def euclidean_distance(a, b): """ return np.linalg.norm(np.array(a) - np.array(b)) + def classifier(train_data, train_target, classes, point, k=5): """ Classifies the point using the KNN algorithm @@ -43,13 +45,13 @@ def classifier(train_data, train_target, classes, point, k=5): for data_point in data: distance = euclidean_distance(data_point[0], point) distances.append((distance, data_point[1])) - # Choosing 'k' points with the least distances. + # Choosing 'k' points with the least distances. votes = [i[1] for i in sorted(distances)[:k]] - # Most commonly occuring class among them + # Most commonly occuring class among them # is the class into which the point is classified result = Counter(votes).most_common(1)[0][0] return classes[result] if __name__ == "__main__": - print(classifier(X_train, y_train, classes, [4.4, 3.1, 1.3, 1.4])) \ No newline at end of file + print(classifier(X_train, y_train, classes, [4.4, 3.1, 1.3, 1.4])) diff --git a/maths/fibonacci_sequence_recursion.py b/maths/fibonacci_sequence_recursion.py index 2e0d835cf..91619600d 100644 --- a/maths/fibonacci_sequence_recursion.py +++ b/maths/fibonacci_sequence_recursion.py @@ -6,7 +6,7 @@ def recur_fibo(n): >>> [recur_fibo(i) for i in range(12)] [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] """ - return n if n <= 1 else recur_fibo(n-1) + recur_fibo(n-2) + return n if n <= 1 else recur_fibo(n - 1) + recur_fibo(n - 2) def main(): diff --git a/maths/sum_of_arithmetic_series.py b/maths/sum_of_arithmetic_series.py index f7ea5dc84..74eef0f18 100755 --- a/maths/sum_of_arithmetic_series.py +++ b/maths/sum_of_arithmetic_series.py @@ -8,7 +8,7 @@ def sum_of_series(first_term, common_diff, num_of_terms): >>> sum_of_series(1, 10, 100) 49600.0 """ - sum = ((num_of_terms/2)*(2*first_term+(num_of_terms-1)*common_diff)) + sum = (num_of_terms / 2) * (2 * first_term + (num_of_terms - 1) * common_diff) # formula for sum of series return sum @@ -19,4 +19,5 @@ def main(): if __name__ == "__main__": import doctest + doctest.testmod() diff --git a/sorts/stooge_sort.py b/sorts/stooge_sort.py index 089b01a4d..de997a85d 100644 --- a/sorts/stooge_sort.py +++ b/sorts/stooge_sort.py @@ -33,6 +33,7 @@ def stooge(arr, i, h): # Recursively sort first 2/3 elements stooge(arr, i, (h - t)) + if __name__ == "__main__": user_input = input("Enter numbers separated by a comma:\n").strip() unsorted = [int(item) for item in user_input.split(",")]