diff --git a/arithmetic_analysis/bisection.py b/arithmetic_analysis/bisection.py index 8bf3f09782a3..78582b025880 100644 --- a/arithmetic_analysis/bisection.py +++ b/arithmetic_analysis/bisection.py @@ -1,7 +1,9 @@ import math -def bisection(function, a, b): # finds where the function becomes 0 in [a,b] using bolzano +def bisection( + function, a, b +): # finds where the function becomes 0 in [a,b] using bolzano start = a end = b @@ -9,13 +11,15 @@ def bisection(function, a, b): # finds where the function becomes 0 in [a,b] us return a elif function(b) == 0: return b - elif function(a) * function(b) > 0: # if none of these are root and they are both positive or negative, + elif ( + function(a) * function(b) > 0 + ): # if none of these are root and they are both positive or negative, # then his algorithm can't find the root print("couldn't find root in [a,b]") return else: mid = start + (end - start) / 2.0 - while abs(start - mid) > 10**-7: # until we achieve precise equals to 10^-7 + while abs(start - mid) > 10 ** -7: # until we achieve precise equals to 10^-7 if function(mid) == 0: return mid elif function(mid) * function(start) < 0: @@ -27,7 +31,8 @@ def bisection(function, a, b): # finds where the function becomes 0 in [a,b] us def f(x): - return math.pow(x, 3) - 2*x - 5 + return math.pow(x, 3) - 2 * x - 5 + if __name__ == "__main__": print(bisection(f, 1, 1000)) diff --git a/arithmetic_analysis/in_static_equilibrium.py b/arithmetic_analysis/in_static_equilibrium.py index 48eb6135eba7..addaff888f7f 100644 --- a/arithmetic_analysis/in_static_equilibrium.py +++ b/arithmetic_analysis/in_static_equilibrium.py @@ -54,11 +54,8 @@ def in_static_equilibrium( if __name__ == "__main__": # Test to check if it works forces = array( - [ - polar_force(718.4, 180 - 30), - polar_force(879.54, 45), - polar_force(100, -90) - ]) + [polar_force(718.4, 180 - 30), polar_force(879.54, 45), polar_force(100, -90)] + ) location = array([[0, 0], [0, 0], [0, 0]]) diff --git a/arithmetic_analysis/intersection.py b/arithmetic_analysis/intersection.py index 2f25f76ebd96..0fdcfbf1943e 100644 --- a/arithmetic_analysis/intersection.py +++ b/arithmetic_analysis/intersection.py @@ -1,17 +1,24 @@ import math -def intersection(function,x0,x1): #function is the f we want to find its root and x0 and x1 are two random starting points + +def intersection( + function, x0, x1 +): # function is the f we want to find its root and x0 and x1 are two random starting points x_n = x0 x_n1 = x1 while True: - x_n2 = x_n1-(function(x_n1)/((function(x_n1)-function(x_n))/(x_n1-x_n))) - if abs(x_n2 - x_n1) < 10**-5: + x_n2 = x_n1 - ( + function(x_n1) / ((function(x_n1) - function(x_n)) / (x_n1 - x_n)) + ) + if abs(x_n2 - x_n1) < 10 ** -5: return x_n2 - x_n=x_n1 - x_n1=x_n2 + x_n = x_n1 + x_n1 = x_n2 + def f(x): - return math.pow(x , 3) - (2 * x) -5 + return math.pow(x, 3) - (2 * x) - 5 + if __name__ == "__main__": - print(intersection(f,3,3.5)) + print(intersection(f, 3, 3.5)) diff --git a/arithmetic_analysis/lu_decomposition.py b/arithmetic_analysis/lu_decomposition.py index 19e259afb826..4372621d74cb 100644 --- a/arithmetic_analysis/lu_decomposition.py +++ b/arithmetic_analysis/lu_decomposition.py @@ -28,9 +28,7 @@ def LUDecompose(table): if __name__ == "__main__": - matrix = numpy.array([[2, -2, 1], - [0, 1, 2], - [5, 3, 1]]) + matrix = numpy.array([[2, -2, 1], [0, 1, 2], [5, 3, 1]]) L, U = LUDecompose(matrix) print(L) print(U) diff --git a/arithmetic_analysis/newton_method.py b/arithmetic_analysis/newton_method.py index cf5649ee3f3b..1408a983041d 100644 --- a/arithmetic_analysis/newton_method.py +++ b/arithmetic_analysis/newton_method.py @@ -8,17 +8,17 @@ def newton(function, function1, startingInt): x_n = startingInt while True: x_n1 = x_n - function(x_n) / function1(x_n) - if abs(x_n - x_n1) < 10**-5: + if abs(x_n - x_n1) < 10 ** -5: return x_n1 x_n = x_n1 def f(x): - return (x**3) - (2 * x) - 5 + return (x ** 3) - (2 * x) - 5 def f1(x): - return 3 * (x**2) - 2 + return 3 * (x ** 2) - 2 if __name__ == "__main__": diff --git a/arithmetic_analysis/newton_raphson_method.py b/arithmetic_analysis/newton_raphson_method.py index d17b57a2e670..646b352a923c 100644 --- a/arithmetic_analysis/newton_raphson_method.py +++ b/arithmetic_analysis/newton_raphson_method.py @@ -1,33 +1,34 @@ # Implementing Newton Raphson method in Python # Author: Syed Haseeb Shah (github.com/QuantumNovice) -#The Newton-Raphson method (also known as Newton's method) is a way to -#quickly find a good approximation for the root of a real-valued function +# The Newton-Raphson method (also known as Newton's method) is a way to +# quickly find a good approximation for the root of a real-valued function from sympy import diff from decimal import Decimal + def NewtonRaphson(func, a): - ''' Finds root from the point 'a' onwards by Newton-Raphson method ''' + """ Finds root from the point 'a' onwards by Newton-Raphson method """ while True: - c = Decimal(a) - ( Decimal(eval(func)) / Decimal(eval(str(diff(func)))) ) + c = Decimal(a) - (Decimal(eval(func)) / Decimal(eval(str(diff(func))))) a = c # This number dictates the accuracy of the answer - if abs(eval(func)) < 10**-15: - return c + if abs(eval(func)) < 10 ** -15: + return c # Let's Execute -if __name__ == '__main__': +if __name__ == "__main__": # Find root of trigonometric function # Find value of pi - print('sin(x) = 0', NewtonRaphson('sin(x)', 2)) + print("sin(x) = 0", NewtonRaphson("sin(x)", 2)) # Find root of polynomial - print('x**2 - 5*x +2 = 0', NewtonRaphson('x**2 - 5*x +2', 0.4)) + print("x**2 - 5*x +2 = 0", NewtonRaphson("x**2 - 5*x +2", 0.4)) # Find Square Root of 5 - print('x**2 - 5 = 0', NewtonRaphson('x**2 - 5', 0.1)) + print("x**2 - 5 = 0", NewtonRaphson("x**2 - 5", 0.1)) # Exponential Roots - print('exp(x) - 1 = 0', NewtonRaphson('exp(x) - 1', 0)) + print("exp(x) - 1 = 0", NewtonRaphson("exp(x) - 1", 0)) diff --git a/backtracking/all_combinations.py b/backtracking/all_combinations.py index 63425aeabbd1..23fe378f5462 100644 --- a/backtracking/all_combinations.py +++ b/backtracking/all_combinations.py @@ -12,7 +12,7 @@ def generate_all_combinations(n: int, k: int) -> [[int]]: >>> generate_all_combinations(n=4, k=2) [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]] """ - + result = [] create_all_state(1, n, k, [], result) return result @@ -34,7 +34,7 @@ def print_all_state(total_list): print(*i) -if __name__ == '__main__': +if __name__ == "__main__": n = 4 k = 2 total_list = generate_all_combinations(n, k) diff --git a/backtracking/all_permutations.py b/backtracking/all_permutations.py index 299b708fef4e..b0955bf53a31 100644 --- a/backtracking/all_permutations.py +++ b/backtracking/all_permutations.py @@ -1,42 +1,42 @@ -''' +""" In this problem, we want to determine all possible permutations of the given sequence. We use backtracking to solve this problem. Time complexity: O(n! * n), where n denotes the length of the given sequence. -''' +""" def generate_all_permutations(sequence): - create_state_space_tree(sequence, [], 0, [0 for i in range(len(sequence))]) + create_state_space_tree(sequence, [], 0, [0 for i in range(len(sequence))]) def create_state_space_tree(sequence, current_sequence, index, index_used): - ''' + """ Creates a state space tree to iterate through each branch using DFS. We know that each state has exactly len(sequence) - index children. It terminates when it reaches the end of the given sequence. - ''' + """ - if index == len(sequence): - print(current_sequence) - return + if index == len(sequence): + print(current_sequence) + return - for i in range(len(sequence)): - if not index_used[i]: - current_sequence.append(sequence[i]) - index_used[i] = True - create_state_space_tree(sequence, current_sequence, index + 1, index_used) - current_sequence.pop() - index_used[i] = False + for i in range(len(sequence)): + if not index_used[i]: + current_sequence.append(sequence[i]) + index_used[i] = True + create_state_space_tree(sequence, current_sequence, index + 1, index_used) + current_sequence.pop() + index_used[i] = False -''' +""" remove the comment to take an input from the user print("Enter the elements") sequence = list(map(int, input().split())) -''' +""" sequence = [3, 1, 2, 4] generate_all_permutations(sequence) diff --git a/backtracking/all_subsequences.py b/backtracking/all_subsequences.py index d868377234a8..4a22c05d29a8 100644 --- a/backtracking/all_subsequences.py +++ b/backtracking/all_subsequences.py @@ -1,39 +1,39 @@ -''' +""" In this problem, we want to determine all possible subsequences of the given sequence. We use backtracking to solve this problem. Time complexity: O(2^n), where n denotes the length of the given sequence. -''' +""" def generate_all_subsequences(sequence): - create_state_space_tree(sequence, [], 0) + create_state_space_tree(sequence, [], 0) def create_state_space_tree(sequence, current_subsequence, index): - ''' + """ Creates a state space tree to iterate through each branch using DFS. We know that each state has exactly two children. It terminates when it reaches the end of the given sequence. - ''' + """ - if index == len(sequence): - print(current_subsequence) - return + if index == len(sequence): + print(current_subsequence) + return - create_state_space_tree(sequence, current_subsequence, index + 1) - current_subsequence.append(sequence[index]) - create_state_space_tree(sequence, current_subsequence, index + 1) - current_subsequence.pop() + create_state_space_tree(sequence, current_subsequence, index + 1) + current_subsequence.append(sequence[index]) + create_state_space_tree(sequence, current_subsequence, index + 1) + current_subsequence.pop() -''' +""" remove the comment to take an input from the user print("Enter the elements") sequence = list(map(int, input().split())) -''' +""" sequence = [3, 1, 2, 4] generate_all_subsequences(sequence) diff --git a/backtracking/minimax.py b/backtracking/minimax.py index 5168306e71fc..af07b8d8171a 100644 --- a/backtracking/minimax.py +++ b/backtracking/minimax.py @@ -1,28 +1,34 @@ -import math +import math -''' Minimax helps to achieve maximum score in a game by checking all possible moves +""" Minimax helps to achieve maximum score in a game by checking all possible moves depth is current depth in game tree. nodeIndex is index of current node in scores[]. if move is of maximizer return true else false leaves of game tree is stored in scores[] height is maximum height of Game tree -''' +""" -def minimax (Depth, nodeIndex, isMax, scores, height): - if Depth == height: - return scores[nodeIndex] +def minimax(Depth, nodeIndex, isMax, scores, height): - if isMax: - return (max(minimax(Depth + 1, nodeIndex * 2, False, scores, height), - minimax(Depth + 1, nodeIndex * 2 + 1, False, scores, height))) - return (min(minimax(Depth + 1, nodeIndex * 2, True, scores, height), - minimax(Depth + 1, nodeIndex * 2 + 1, True, scores, height))) + if Depth == height: + return scores[nodeIndex] -if __name__ == "__main__": - - scores = [90, 23, 6, 33, 21, 65, 123, 34423] - height = math.log(len(scores), 2) + if isMax: + return max( + minimax(Depth + 1, nodeIndex * 2, False, scores, height), + minimax(Depth + 1, nodeIndex * 2 + 1, False, scores, height), + ) + return min( + minimax(Depth + 1, nodeIndex * 2, True, scores, height), + minimax(Depth + 1, nodeIndex * 2 + 1, True, scores, height), + ) - print("Optimal value : ", end = "") - print(minimax(0, 0, True, scores, height)) + +if __name__ == "__main__": + + scores = [90, 23, 6, 33, 21, 65, 123, 34423] + height = math.log(len(scores), 2) + + print("Optimal value : ", end="") + print(minimax(0, 0, True, scores, height)) diff --git a/backtracking/n_queens.py b/backtracking/n_queens.py index dfd4498b166b..f95357c82e21 100644 --- a/backtracking/n_queens.py +++ b/backtracking/n_queens.py @@ -1,4 +1,4 @@ -''' +""" The nqueens problem is of placing N queens on a N * N chess board such that no queen can attack any other queens placed @@ -6,11 +6,12 @@ This means that one queen cannot have any other queen on its horizontal, vertical and diagonal lines. -''' +""" solution = [] + def isSafe(board, row, column): - ''' + """ This function returns a boolean value True if it is safe to place a queen there considering the current state of the board. @@ -21,64 +22,67 @@ def isSafe(board, row, column): Returns : Boolean Value - ''' + """ for i in range(len(board)): if board[row][i] == 1: return False for i in range(len(board)): if board[i][column] == 1: return False - for i,j in zip(range(row,-1,-1),range(column,-1,-1)): + for i, j in zip(range(row, -1, -1), range(column, -1, -1)): if board[i][j] == 1: return False - for i,j in zip(range(row,-1,-1),range(column,len(board))): + for i, j in zip(range(row, -1, -1), range(column, len(board))): if board[i][j] == 1: return False return True + def solve(board, row): - ''' + """ It creates a state space tree and calls the safe function untill it receives a False Boolean and terminates that brach and backtracks to the next poosible solution branch. - ''' + """ if row >= len(board): - ''' + """ If the row number exceeds N we have board with a successful combination and that combination is appended to the solution list and the board is printed. - ''' + """ solution.append(board) printboard(board) print() - return + return for i in range(len(board)): - ''' + """ For every row it iterates through each column to check if it is feesible to place a queen there. If all the combinations for that particaular branch are successfull the board is reinitialized for the next possible combination. - ''' - if isSafe(board,row,i): + """ + if isSafe(board, row, i): board[row][i] = 1 - solve(board,row+1) + solve(board, row + 1) board[row][i] = 0 return False + def printboard(board): - ''' + """ Prints the boards that have a successfull combination. - ''' + """ for i in range(len(board)): for j in range(len(board)): if board[i][j] == 1: - print("Q", end = " ") - else : - print(".", end = " ") + print("Q", end=" ") + else: + print(".", end=" ") print() -#n=int(input("The no. of queens")) + +# n=int(input("The no. of queens")) n = 8 -board = [[0 for i in range(n)]for j in range(n)] +board = [[0 for i in range(n)] for j in range(n)] solve(board, 0) print("The total no. of solutions are :", len(solution)) diff --git a/backtracking/sum_of_subsets.py b/backtracking/sum_of_subsets.py index b01bffbb651d..d96552d39997 100644 --- a/backtracking/sum_of_subsets.py +++ b/backtracking/sum_of_subsets.py @@ -1,36 +1,46 @@ -''' +""" The sum-of-subsetsproblem states that a set of non-negative integers, and a value M, determine all possible subsets of the given set whose summation sum equal to given M. Summation of the chosen numbers must be equal to given number M and one number can be used only once. -''' +""" + def generate_sum_of_subsets_soln(nums, max_sum): - result = [] - path = [] - num_index = 0 - remaining_nums_sum = sum(nums) - create_state_space_tree(nums, max_sum, num_index, path,result, remaining_nums_sum) - return result - -def create_state_space_tree(nums,max_sum,num_index,path,result, remaining_nums_sum): - ''' + result = [] + path = [] + num_index = 0 + remaining_nums_sum = sum(nums) + create_state_space_tree(nums, max_sum, num_index, path, result, remaining_nums_sum) + return result + + +def create_state_space_tree(nums, max_sum, num_index, path, result, remaining_nums_sum): + """ Creates a state space tree to iterate through each branch using DFS. It terminates the branching of a node when any of the two conditions given below satisfy. This algorithm follows depth-fist-search and backtracks when the node is not branchable. - ''' - if sum(path) > max_sum or (remaining_nums_sum + sum(path)) < max_sum: - return - if sum(path) == max_sum: - result.append(path) - return - for num_index in range(num_index,len(nums)): - create_state_space_tree(nums, max_sum, num_index + 1, path + [nums[num_index]], result, remaining_nums_sum - nums[num_index]) + """ + if sum(path) > max_sum or (remaining_nums_sum + sum(path)) < max_sum: + return + if sum(path) == max_sum: + result.append(path) + return + for num_index in range(num_index, len(nums)): + create_state_space_tree( + nums, + max_sum, + num_index + 1, + path + [nums[num_index]], + result, + remaining_nums_sum - nums[num_index], + ) + -''' +""" remove the comment to take an input from the user print("Enter the elements") @@ -38,8 +48,8 @@ def create_state_space_tree(nums,max_sum,num_index,path,result, remaining_nums_s print("Enter max_sum sum") max_sum = int(input()) -''' +""" nums = [3, 34, 4, 12, 5, 2] max_sum = 9 -result = generate_sum_of_subsets_soln(nums,max_sum) -print(*result) \ No newline at end of file +result = generate_sum_of_subsets_soln(nums, max_sum) +print(*result) diff --git a/boolean_algebra/quine_mc_cluskey.py b/boolean_algebra/quine_mc_cluskey.py index b7ca8da437a3..7762d712a01a 100644 --- a/boolean_algebra/quine_mc_cluskey.py +++ b/boolean_algebra/quine_mc_cluskey.py @@ -1,151 +1,168 @@ def compare_string(string1, string2): - """ + """ >>> compare_string('0010','0110') '0_10' >>> compare_string('0110','1101') -1 """ - l1 = list(string1); l2 = list(string2) - count = 0 - for i in range(len(l1)): - if l1[i] != l2[i]: - count += 1 - l1[i] = '_' - if count > 1: - return -1 - else: - return("".join(l1)) + l1 = list(string1) + l2 = list(string2) + count = 0 + for i in range(len(l1)): + if l1[i] != l2[i]: + count += 1 + l1[i] = "_" + if count > 1: + return -1 + else: + return "".join(l1) + def check(binary): - """ + """ >>> check(['0.00.01.5']) ['0.00.01.5'] """ - pi = [] - while 1: - check1 = ['$']*len(binary) - temp = [] - for i in range(len(binary)): - for j in range(i+1, len(binary)): - k=compare_string(binary[i], binary[j]) - if k != -1: - check1[i] = '*' - check1[j] = '*' - temp.append(k) - for i in range(len(binary)): - if check1[i] == '$': - pi.append(binary[i]) - if len(temp) == 0: - return pi - binary = list(set(temp)) + pi = [] + while 1: + check1 = ["$"] * len(binary) + temp = [] + for i in range(len(binary)): + for j in range(i + 1, len(binary)): + k = compare_string(binary[i], binary[j]) + if k != -1: + check1[i] = "*" + check1[j] = "*" + temp.append(k) + for i in range(len(binary)): + if check1[i] == "$": + pi.append(binary[i]) + if len(temp) == 0: + return pi + binary = list(set(temp)) + def decimal_to_binary(no_of_variable, minterms): - """ + """ >>> decimal_to_binary(3,[1.5]) ['0.00.01.5'] """ - temp = [] - s = '' - for m in minterms: - for i in range(no_of_variable): - s = str(m%2) + s - m //= 2 - temp.append(s) - s = '' - return temp + temp = [] + s = "" + for m in minterms: + for i in range(no_of_variable): + s = str(m % 2) + s + m //= 2 + temp.append(s) + s = "" + return temp + def is_for_table(string1, string2, count): - """ + """ >>> is_for_table('__1','011',2) True >>> is_for_table('01_','001',1) False """ - l1 = list(string1);l2=list(string2) - count_n = 0 - for i in range(len(l1)): - if l1[i] != l2[i]: - count_n += 1 - if count_n == count: - return True - else: - return False + l1 = list(string1) + l2 = list(string2) + count_n = 0 + for i in range(len(l1)): + if l1[i] != l2[i]: + count_n += 1 + if count_n == count: + return True + else: + return False + def selection(chart, prime_implicants): - """ + """ >>> selection([[1]],['0.00.01.5']) ['0.00.01.5'] >>> selection([[1]],['0.00.01.5']) ['0.00.01.5'] """ - temp = [] - select = [0]*len(chart) - for i in range(len(chart[0])): - count = 0 - rem = -1 - for j in range(len(chart)): - if chart[j][i] == 1: - count += 1 - rem = j - if count == 1: - select[rem] = 1 - for i in range(len(select)): - if select[i] == 1: - for j in range(len(chart[0])): - if chart[i][j] == 1: - for k in range(len(chart)): - chart[k][j] = 0 - temp.append(prime_implicants[i]) - while 1: - max_n = 0; rem = -1; count_n = 0 - for i in range(len(chart)): - count_n = chart[i].count(1) - if count_n > max_n: - max_n = count_n - rem = i - - if max_n == 0: - return temp - - temp.append(prime_implicants[rem]) - - for i in range(len(chart[0])): - if chart[rem][i] == 1: - for j in range(len(chart)): - chart[j][i] = 0 - + temp = [] + select = [0] * len(chart) + for i in range(len(chart[0])): + count = 0 + rem = -1 + for j in range(len(chart)): + if chart[j][i] == 1: + count += 1 + rem = j + if count == 1: + select[rem] = 1 + for i in range(len(select)): + if select[i] == 1: + for j in range(len(chart[0])): + if chart[i][j] == 1: + for k in range(len(chart)): + chart[k][j] = 0 + temp.append(prime_implicants[i]) + while 1: + max_n = 0 + rem = -1 + count_n = 0 + for i in range(len(chart)): + count_n = chart[i].count(1) + if count_n > max_n: + max_n = count_n + rem = i + + if max_n == 0: + return temp + + temp.append(prime_implicants[rem]) + + for i in range(len(chart[0])): + if chart[rem][i] == 1: + for j in range(len(chart)): + chart[j][i] = 0 + + def prime_implicant_chart(prime_implicants, binary): - """ + """ >>> prime_implicant_chart(['0.00.01.5'],['0.00.01.5']) [[1]] """ - chart = [[0 for x in range(len(binary))] for x in range(len(prime_implicants))] - for i in range(len(prime_implicants)): - count = prime_implicants[i].count('_') - for j in range(len(binary)): - if(is_for_table(prime_implicants[i], binary[j], count)): - chart[i][j] = 1 - - return chart + chart = [[0 for x in range(len(binary))] for x in range(len(prime_implicants))] + for i in range(len(prime_implicants)): + count = prime_implicants[i].count("_") + for j in range(len(binary)): + if is_for_table(prime_implicants[i], binary[j], count): + chart[i][j] = 1 + + return chart + def main(): - no_of_variable = int(input("Enter the no. of variables\n")) - minterms = [int(x) for x in input("Enter the decimal representation of Minterms 'Spaces Seprated'\n").split()] - binary = decimal_to_binary(no_of_variable, minterms) - - prime_implicants = check(binary) - print("Prime Implicants are:") - print(prime_implicants) - chart = prime_implicant_chart(prime_implicants, binary) - - essential_prime_implicants = selection(chart,prime_implicants) - print("Essential Prime Implicants are:") - print(essential_prime_implicants) - -if __name__ == '__main__': - import doctest - doctest.testmod() - main() + no_of_variable = int(input("Enter the no. of variables\n")) + minterms = [ + int(x) + for x in input( + "Enter the decimal representation of Minterms 'Spaces Seprated'\n" + ).split() + ] + binary = decimal_to_binary(no_of_variable, minterms) + + prime_implicants = check(binary) + print("Prime Implicants are:") + print(prime_implicants) + chart = prime_implicant_chart(prime_implicants, binary) + + essential_prime_implicants = selection(chart, prime_implicants) + print("Essential Prime Implicants are:") + print(essential_prime_implicants) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + main() diff --git a/ciphers/affine_cipher.py b/ciphers/affine_cipher.py index a5d94f087dbf..eb50acf8fc20 100644 --- a/ciphers/affine_cipher.py +++ b/ciphers/affine_cipher.py @@ -2,42 +2,56 @@ SYMBOLS = r""" !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~""" + def main(): - message = input('Enter message: ') - key = int(input('Enter key [2000 - 9000]: ')) - mode = input('Encrypt/Decrypt [E/D]: ') - - if mode.lower().startswith('e'): - mode = 'encrypt' - translated = encryptMessage(key, message) - elif mode.lower().startswith('d'): - mode = 'decrypt' - translated = decryptMessage(key, message) - print('\n%sed text: \n%s' % (mode.title(), translated)) + message = input("Enter message: ") + key = int(input("Enter key [2000 - 9000]: ")) + mode = input("Encrypt/Decrypt [E/D]: ") + + if mode.lower().startswith("e"): + mode = "encrypt" + translated = encryptMessage(key, message) + elif mode.lower().startswith("d"): + mode = "decrypt" + translated = decryptMessage(key, message) + print("\n%sed text: \n%s" % (mode.title(), translated)) + def getKeyParts(key): keyA = key // len(SYMBOLS) keyB = key % len(SYMBOLS) return (keyA, keyB) + def checkKeys(keyA, keyB, mode): - if keyA == 1 and mode == 'encrypt': - sys.exit('The affine cipher becomes weak when key A is set to 1. Choose different key') - if keyB == 0 and mode == 'encrypt': - sys.exit('The affine cipher becomes weak when key A is set to 1. Choose different key') + if keyA == 1 and mode == "encrypt": + sys.exit( + "The affine cipher becomes weak when key A is set to 1. Choose different key" + ) + if keyB == 0 and mode == "encrypt": + sys.exit( + "The affine cipher becomes weak when key A is set to 1. Choose different key" + ) if keyA < 0 or keyB < 0 or keyB > len(SYMBOLS) - 1: - sys.exit('Key A must be greater than 0 and key B must be between 0 and %s.' % (len(SYMBOLS) - 1)) + sys.exit( + "Key A must be greater than 0 and key B must be between 0 and %s." + % (len(SYMBOLS) - 1) + ) if cryptoMath.gcd(keyA, len(SYMBOLS)) != 1: - sys.exit('Key A %s and the symbol set size %s are not relatively prime. Choose a different key.' % (keyA, len(SYMBOLS))) + sys.exit( + "Key A %s and the symbol set size %s are not relatively prime. Choose a different key." + % (keyA, len(SYMBOLS)) + ) + def encryptMessage(key, message): - ''' + """ >>> encryptMessage(4545, 'The affine cipher is a type of monoalphabetic substitution cipher.') 'VL}p MM{I}p~{HL}Gp{vp pFsH}pxMpyxIx JHL O}F{~pvuOvF{FuF{xIp~{HL}Gi' - ''' + """ keyA, keyB = getKeyParts(key) - checkKeys(keyA, keyB, 'encrypt') - cipherText = '' + checkKeys(keyA, keyB, "encrypt") + cipherText = "" for symbol in message: if symbol in SYMBOLS: symIndex = SYMBOLS.find(symbol) @@ -46,14 +60,15 @@ def encryptMessage(key, message): cipherText += symbol return cipherText + def decryptMessage(key, message): - ''' + """ >>> decryptMessage(4545, 'VL}p MM{I}p~{HL}Gp{vp pFsH}pxMpyxIx JHL O}F{~pvuOvF{FuF{xIp~{HL}Gi') 'The affine cipher is a type of monoalphabetic substitution cipher.' - ''' + """ keyA, keyB = getKeyParts(key) - checkKeys(keyA, keyB, 'decrypt') - plainText = '' + checkKeys(keyA, keyB, "decrypt") + plainText = "" modInverseOfkeyA = cryptoMath.findModInverse(keyA, len(SYMBOLS)) for symbol in message: if symbol in SYMBOLS: @@ -63,6 +78,7 @@ def decryptMessage(key, message): plainText += symbol return plainText + def getRandomKey(): while True: keyA = random.randint(2, len(SYMBOLS)) @@ -70,7 +86,9 @@ def getRandomKey(): if cryptoMath.gcd(keyA, len(SYMBOLS)) == 1: return keyA * len(SYMBOLS) + keyB -if __name__ == '__main__': + +if __name__ == "__main__": import doctest + doctest.testmod() main() diff --git a/ciphers/atbash.py b/ciphers/atbash.py index 9ed47e0874f8..4cf003859856 100644 --- a/ciphers/atbash.py +++ b/ciphers/atbash.py @@ -1,15 +1,15 @@ def atbash(): - output="" + output = "" for i in input("Enter the sentence to be encrypted ").strip(): extract = ord(i) if 65 <= extract <= 90: - output += chr(155-extract) + output += chr(155 - extract) elif 97 <= extract <= 122: - output += chr(219-extract) + output += chr(219 - extract) else: output += i print(output) -if __name__ == '__main__': +if __name__ == "__main__": atbash() diff --git a/ciphers/base16.py b/ciphers/base16.py index 9bc0e5d8337a..0210315d54e6 100644 --- a/ciphers/base16.py +++ b/ciphers/base16.py @@ -1,11 +1,13 @@ import base64 + def main(): - inp = input('->') - encoded = inp.encode('utf-8') #encoded the input (we need a bytes like object) - b16encoded = base64.b16encode(encoded) #b16encoded the encoded string + inp = input("->") + encoded = inp.encode("utf-8") # encoded the input (we need a bytes like object) + b16encoded = base64.b16encode(encoded) # b16encoded the encoded string print(b16encoded) - print(base64.b16decode(b16encoded).decode('utf-8'))#decoded it + print(base64.b16decode(b16encoded).decode("utf-8")) # decoded it + -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/ciphers/base32.py b/ciphers/base32.py index 2ac29f441e94..5bba8c4dd685 100644 --- a/ciphers/base32.py +++ b/ciphers/base32.py @@ -1,11 +1,13 @@ import base64 + def main(): - inp = input('->') - encoded = inp.encode('utf-8') #encoded the input (we need a bytes like object) - b32encoded = base64.b32encode(encoded) #b32encoded the encoded string + inp = input("->") + encoded = inp.encode("utf-8") # encoded the input (we need a bytes like object) + b32encoded = base64.b32encode(encoded) # b32encoded the encoded string print(b32encoded) - print(base64.b32decode(b32encoded).decode('utf-8'))#decoded it + print(base64.b32decode(b32encoded).decode("utf-8")) # decoded it + -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/ciphers/base64_cipher.py b/ciphers/base64_cipher.py index fa3451c0cbae..9fca5b02679f 100644 --- a/ciphers/base64_cipher.py +++ b/ciphers/base64_cipher.py @@ -1,64 +1,71 @@ def encodeBase64(text): base64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" - - r = "" #the result - c = 3 - len(text) % 3 #the length of padding - p = "=" * c #the padding - s = text + "\0" * c #the text to encode - - i = 0 + + r = "" # the result + c = 3 - len(text) % 3 # the length of padding + p = "=" * c # the padding + s = text + "\0" * c # the text to encode + + i = 0 while i < len(s): if i > 0 and ((i / 3 * 4) % 76) == 0: r = r + "\r\n" - - n = (ord(s[i]) << 16) + (ord(s[i+1]) << 8 ) + ord(s[i+2]) - + + n = (ord(s[i]) << 16) + (ord(s[i + 1]) << 8) + ord(s[i + 2]) + n1 = (n >> 18) & 63 n2 = (n >> 12) & 63 - n3 = (n >> 6) & 63 + n3 = (n >> 6) & 63 n4 = n & 63 - + r += base64chars[n1] + base64chars[n2] + base64chars[n3] + base64chars[n4] i += 3 - return r[0: len(r)-len(p)] + p - + return r[0 : len(r) - len(p)] + p + + def decodeBase64(text): base64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" s = "" - + for i in text: if i in base64chars: s += i c = "" else: - if i == '=': - c += '=' - + if i == "=": + c += "=" + p = "" if c == "=": - p = 'A' + p = "A" else: if c == "==": p = "AA" - + r = "" s = s + p - + i = 0 while i < len(s): - n = (base64chars.index(s[i]) << 18) + (base64chars.index(s[i+1]) << 12) + (base64chars.index(s[i+2]) << 6) +base64chars.index(s[i+3]) - + n = ( + (base64chars.index(s[i]) << 18) + + (base64chars.index(s[i + 1]) << 12) + + (base64chars.index(s[i + 2]) << 6) + + base64chars.index(s[i + 3]) + ) + r += chr((n >> 16) & 255) + chr((n >> 8) & 255) + chr(n & 255) - + i += 4 - - return r[0: len(r) - len(p)] + + return r[0 : len(r) - len(p)] + def main(): print(encodeBase64("WELCOME to base64 encoding")) print(decodeBase64(encodeBase64("WELCOME to base64 encoding"))) - -if __name__ == '__main__': + +if __name__ == "__main__": main() diff --git a/ciphers/base85.py b/ciphers/base85.py index 5fd13837f662..ebfd0480f794 100644 --- a/ciphers/base85.py +++ b/ciphers/base85.py @@ -1,11 +1,13 @@ import base64 + def main(): - inp = input('->') - encoded = inp.encode('utf-8') #encoded the input (we need a bytes like object) - a85encoded = base64.a85encode(encoded) #a85encoded the encoded string + inp = input("->") + encoded = inp.encode("utf-8") # encoded the input (we need a bytes like object) + a85encoded = base64.a85encode(encoded) # a85encoded the encoded string print(a85encoded) - print(base64.a85decode(a85encoded).decode('utf-8'))#decoded it + print(base64.a85decode(a85encoded).decode("utf-8")) # decoded it + -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/ciphers/brute_force_caesar_cipher.py b/ciphers/brute_force_caesar_cipher.py index 3e6e975c8297..2586803ba5ff 100644 --- a/ciphers/brute_force_caesar_cipher.py +++ b/ciphers/brute_force_caesar_cipher.py @@ -42,12 +42,15 @@ def decrypt(message): translated = translated + symbol print("Decryption using Key #%s: %s" % (key, translated)) + def main(): message = input("Encrypted message: ") message = message.upper() decrypt(message) -if __name__ == '__main__': + +if __name__ == "__main__": import doctest + doctest.testmod() main() diff --git a/ciphers/caesar_cipher.py b/ciphers/caesar_cipher.py index 95d65d404266..3f24e049afb0 100644 --- a/ciphers/caesar_cipher.py +++ b/ciphers/caesar_cipher.py @@ -1,5 +1,5 @@ def encrypt(strng, key): - encrypted = '' + encrypted = "" for x in strng: indx = (ord(x) + key) % 256 if indx > 126: @@ -9,7 +9,7 @@ def encrypt(strng, key): def decrypt(strng, key): - decrypted = '' + decrypted = "" for x in strng: indx = (ord(x) - key) % 256 if indx < 32: @@ -17,9 +17,10 @@ def decrypt(strng, key): decrypted = decrypted + chr(indx) return decrypted + def brute_force(strng): key = 1 - decrypted = '' + decrypted = "" while key <= 94: for x in strng: indx = (ord(x) - key) % 256 @@ -27,39 +28,39 @@ def brute_force(strng): indx = indx + 95 decrypted = decrypted + chr(indx) print("Key: {}\t| Message: {}".format(key, decrypted)) - decrypted = '' + decrypted = "" key += 1 return None def main(): while True: - print('-' * 10 + "\n**Menu**\n" + '-' * 10) + print("-" * 10 + "\n**Menu**\n" + "-" * 10) print("1.Encrpyt") print("2.Decrypt") print("3.BruteForce") print("4.Quit") choice = input("What would you like to do?: ") - if choice not in ['1', '2', '3', '4']: + if choice not in ["1", "2", "3", "4"]: print("Invalid choice, please enter a valid choice") - elif choice == '1': + elif choice == "1": strng = input("Please enter the string to be encrypted: ") key = int(input("Please enter off-set between 1-94: ")) if key in range(1, 95): print(encrypt(strng.lower(), key)) - elif choice == '2': + elif choice == "2": strng = input("Please enter the string to be decrypted: ") key = int(input("Please enter off-set between 1-94: ")) - if key in range(1,95): + if key in range(1, 95): print(decrypt(strng, key)) - elif choice == '3': + elif choice == "3": strng = input("Please enter the string to be decrypted: ") brute_force(strng) main() - elif choice == '4': + elif choice == "4": print("Goodbye.") break -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/ciphers/cryptomath_module.py b/ciphers/cryptomath_module.py index 3e8e71b117ed..fc38e4bd2a22 100644 --- a/ciphers/cryptomath_module.py +++ b/ciphers/cryptomath_module.py @@ -3,6 +3,7 @@ def gcd(a, b): a, b = b % a, a return b + def findModInverse(a, m): if gcd(a, m) != 1: return None @@ -10,5 +11,5 @@ def findModInverse(a, m): v1, v2, v3 = 0, 1, m while v3 != 0: q = u3 // v3 - v1, v2, v3, u1, u2, u3 = (u1 - q * v1), (u2 - q * v2), (u3 - q *v3), v1, v2, v3 - return u1 % m + v1, v2, v3, u1, u2, u3 = (u1 - q * v1), (u2 - q * v2), (u3 - q * v3), v1, v2, v3 + return u1 % m diff --git a/ciphers/elgamal_key_generator.py b/ciphers/elgamal_key_generator.py index 6a8751f69524..cc6b297f2daf 100644 --- a/ciphers/elgamal_key_generator.py +++ b/ciphers/elgamal_key_generator.py @@ -7,9 +7,9 @@ def main(): - print('Making key files...') - makeKeyFiles('elgamal', 2048) - print('Key files generation successful') + print("Making key files...") + makeKeyFiles("elgamal", 2048) + print("Key files generation successful") # I have written my code naively same as definition of primitive root @@ -19,7 +19,7 @@ def main(): def primitiveRoot(p_val): print("Generating primitive root of p") while True: - g = random.randrange(3,p_val) + g = random.randrange(3, p_val) if pow(g, 2, p_val) == 1: continue if pow(g, p_val, p_val) == 1: @@ -28,7 +28,7 @@ def primitiveRoot(p_val): def generateKey(keySize): - print('Generating prime p...') + print("Generating prime p...") p = rabinMiller.generateLargePrime(keySize) # select large prime number. e_1 = primitiveRoot(p) # one primitive root on modulo p. d = random.randrange(3, p) # private_key -> have to be greater than 2 for safety. @@ -41,23 +41,28 @@ def generateKey(keySize): def makeKeyFiles(name, keySize): - if os.path.exists('%s_pubkey.txt' % name) or os.path.exists('%s_privkey.txt' % name): - print('\nWARNING:') - print('"%s_pubkey.txt" or "%s_privkey.txt" already exists. \n' - 'Use a different name or delete these files and re-run this program.' % - (name, name)) + if os.path.exists("%s_pubkey.txt" % name) or os.path.exists( + "%s_privkey.txt" % name + ): + print("\nWARNING:") + print( + '"%s_pubkey.txt" or "%s_privkey.txt" already exists. \n' + "Use a different name or delete these files and re-run this program." + % (name, name) + ) sys.exit() publicKey, privateKey = generateKey(keySize) - print('\nWriting public key to file %s_pubkey.txt...' % name) - with open('%s_pubkey.txt' % name, 'w') as fo: - fo.write('%d,%d,%d,%d' % (publicKey[0], publicKey[1], publicKey[2], publicKey[3])) + print("\nWriting public key to file %s_pubkey.txt..." % name) + with open("%s_pubkey.txt" % name, "w") as fo: + fo.write( + "%d,%d,%d,%d" % (publicKey[0], publicKey[1], publicKey[2], publicKey[3]) + ) - print('Writing private key to file %s_privkey.txt...' % name) - with open('%s_privkey.txt' % name, 'w') as fo: - fo.write('%d,%d' % (privateKey[0], privateKey[1])) + print("Writing private key to file %s_privkey.txt..." % name) + with open("%s_privkey.txt" % name, "w") as fo: + fo.write("%d,%d" % (privateKey[0], privateKey[1])) -if __name__ == '__main__': +if __name__ == "__main__": main() - \ No newline at end of file diff --git a/ciphers/hill_cipher.py b/ciphers/hill_cipher.py index 89b88beed17e..e01b6a3f48a8 100644 --- a/ciphers/hill_cipher.py +++ b/ciphers/hill_cipher.py @@ -44,7 +44,7 @@ def gcd(a, b): if a == 0: return b - return gcd(b%a, a) + return gcd(b % a, a) class HillCipher: @@ -59,25 +59,29 @@ class HillCipher: modulus = numpy.vectorize(lambda x: x % 36) toInt = numpy.vectorize(lambda x: round(x)) - + def __init__(self, encrypt_key): """ encrypt_key is an NxN numpy matrix """ - self.encrypt_key = self.modulus(encrypt_key) # mod36 calc's on the encrypt key - self.checkDeterminant() # validate the determinant of the encryption key + self.encrypt_key = self.modulus(encrypt_key) # mod36 calc's on the encrypt key + self.checkDeterminant() # validate the determinant of the encryption key self.decrypt_key = None self.break_key = encrypt_key.shape[0] def checkDeterminant(self): det = round(numpy.linalg.det(self.encrypt_key)) - + if det < 0: det = det % len(self.key_string) req_l = len(self.key_string) if gcd(det, len(self.key_string)) != 1: - raise ValueError("discriminant modular {0} of encryption key({1}) is not co prime w.r.t {2}.\nTry another key.".format(req_l, det, req_l)) + raise ValueError( + "discriminant modular {0} of encryption key({1}) is not co prime w.r.t {2}.\nTry another key.".format( + req_l, det, req_l + ) + ) def processText(self, text): text = list(text.upper()) @@ -87,25 +91,27 @@ def processText(self, text): while len(text) % self.break_key != 0: text.append(last) - return ''.join(text) - + return "".join(text) + def encrypt(self, text): text = self.processText(text.upper()) - encrypted = '' + encrypted = "" for i in range(0, len(text) - self.break_key + 1, self.break_key): - batch = text[i:i+self.break_key] + batch = text[i : i + self.break_key] batch_vec = list(map(self.replaceLetters, batch)) batch_vec = numpy.matrix([batch_vec]).T - batch_encrypted = self.modulus(self.encrypt_key.dot(batch_vec)).T.tolist()[0] - encrypted_batch = ''.join(list(map(self.replaceNumbers, batch_encrypted))) + batch_encrypted = self.modulus(self.encrypt_key.dot(batch_vec)).T.tolist()[ + 0 + ] + encrypted_batch = "".join(list(map(self.replaceNumbers, batch_encrypted))) encrypted += encrypted_batch return encrypted def makeDecryptKey(self): det = round(numpy.linalg.det(self.encrypt_key)) - + if det < 0: det = det % len(self.key_string) det_inv = None @@ -114,22 +120,27 @@ def makeDecryptKey(self): det_inv = i break - inv_key = det_inv * numpy.linalg.det(self.encrypt_key) *\ - numpy.linalg.inv(self.encrypt_key) + inv_key = ( + det_inv + * numpy.linalg.det(self.encrypt_key) + * numpy.linalg.inv(self.encrypt_key) + ) return self.toInt(self.modulus(inv_key)) - + def decrypt(self, text): self.decrypt_key = self.makeDecryptKey() text = self.processText(text.upper()) - decrypted = '' + decrypted = "" for i in range(0, len(text) - self.break_key + 1, self.break_key): - batch = text[i:i+self.break_key] + batch = text[i : i + self.break_key] batch_vec = list(map(self.replaceLetters, batch)) batch_vec = numpy.matrix([batch_vec]).T - batch_decrypted = self.modulus(self.decrypt_key.dot(batch_vec)).T.tolist()[0] - decrypted_batch = ''.join(list(map(self.replaceNumbers, batch_decrypted))) + batch_decrypted = self.modulus(self.decrypt_key.dot(batch_vec)).T.tolist()[ + 0 + ] + decrypted_batch = "".join(list(map(self.replaceNumbers, batch_decrypted))) decrypted += decrypted_batch return decrypted @@ -147,21 +158,22 @@ def main(): hc = HillCipher(numpy.matrix(hill_matrix)) print("Would you like to encrypt or decrypt some text? (1 or 2)") - option = input(""" + option = input( + """ 1. Encrypt 2. Decrypt """ - ) + ) - if option == '1': + if option == "1": text_e = input("What text would you like to encrypt?: ") print("Your encrypted text is:") print(hc.encrypt(text_e)) - elif option == '2': + elif option == "2": text_d = input("What text would you like to decrypt?: ") print("Your decrypted text is:") print(hc.decrypt(text_d)) - + if __name__ == "__main__": main() diff --git a/ciphers/morse_code_implementation.py b/ciphers/morse_code_implementation.py index 5d0e7b2779b1..6df4632af4cb 100644 --- a/ciphers/morse_code_implementation.py +++ b/ciphers/morse_code_implementation.py @@ -2,68 +2,93 @@ # Dictionary representing the morse code chart -MORSE_CODE_DICT = { 'A':'.-', 'B':'-...', - 'C':'-.-.', 'D':'-..', 'E':'.', - 'F':'..-.', 'G':'--.', 'H':'....', - 'I':'..', 'J':'.---', 'K':'-.-', - 'L':'.-..', 'M':'--', 'N':'-.', - 'O':'---', 'P':'.--.', 'Q':'--.-', - 'R':'.-.', 'S':'...', 'T':'-', - 'U':'..-', 'V':'...-', 'W':'.--', - 'X':'-..-', 'Y':'-.--', 'Z':'--..', - '1':'.----', '2':'..---', '3':'...--', - '4':'....-', '5':'.....', '6':'-....', - '7':'--...', '8':'---..', '9':'----.', - '0':'-----', ', ':'--..--', '.':'.-.-.-', - '?':'..--..', '/':'-..-.', '-':'-....-', - '(':'-.--.', ')':'-.--.-'} +MORSE_CODE_DICT = { + "A": ".-", + "B": "-...", + "C": "-.-.", + "D": "-..", + "E": ".", + "F": "..-.", + "G": "--.", + "H": "....", + "I": "..", + "J": ".---", + "K": "-.-", + "L": ".-..", + "M": "--", + "N": "-.", + "O": "---", + "P": ".--.", + "Q": "--.-", + "R": ".-.", + "S": "...", + "T": "-", + "U": "..-", + "V": "...-", + "W": ".--", + "X": "-..-", + "Y": "-.--", + "Z": "--..", + "1": ".----", + "2": "..---", + "3": "...--", + "4": "....-", + "5": ".....", + "6": "-....", + "7": "--...", + "8": "---..", + "9": "----.", + "0": "-----", + ", ": "--..--", + ".": ".-.-.-", + "?": "..--..", + "/": "-..-.", + "-": "-....-", + "(": "-.--.", + ")": "-.--.-", +} def encrypt(message): - cipher = '' + cipher = "" for letter in message: - if letter != ' ': + if letter != " ": - - cipher += MORSE_CODE_DICT[letter] + ' ' + cipher += MORSE_CODE_DICT[letter] + " " else: - cipher += ' ' + cipher += " " return cipher def decrypt(message): - message += ' ' + message += " " - decipher = '' - citext = '' + decipher = "" + citext = "" for letter in message: - if (letter != ' '): - + if letter != " ": i = 0 - citext += letter else: i += 1 + if i == 2: - if i == 2 : - - - decipher += ' ' + decipher += " " else: - - decipher += list(MORSE_CODE_DICT.keys())[list(MORSE_CODE_DICT - .values()).index(citext)] - citext = '' + decipher += list(MORSE_CODE_DICT.keys())[ + list(MORSE_CODE_DICT.values()).index(citext) + ] + citext = "" return decipher @@ -78,5 +103,5 @@ def main(): print(result) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/ciphers/onepad_cipher.py b/ciphers/onepad_cipher.py index 1dac270bda1f..5a410bfa638a 100644 --- a/ciphers/onepad_cipher.py +++ b/ciphers/onepad_cipher.py @@ -3,28 +3,28 @@ class Onepad: def encrypt(self, text): - '''Function to encrypt text using psedo-random numbers''' + """Function to encrypt text using psedo-random numbers""" plain = [ord(i) for i in text] key = [] cipher = [] for i in plain: k = random.randint(1, 300) - c = (i+k)*k + c = (i + k) * k cipher.append(c) key.append(k) return cipher, key def decrypt(self, cipher, key): - '''Function to decrypt text using psedo-random numbers.''' + """Function to decrypt text using psedo-random numbers.""" plain = [] for i in range(len(key)): - p = int((cipher[i]-(key[i])**2)/key[i]) + p = int((cipher[i] - (key[i]) ** 2) / key[i]) plain.append(chr(p)) - plain = ''.join([i for i in plain]) + plain = "".join([i for i in plain]) return plain -if __name__ == '__main__': - c, k = Onepad().encrypt('Hello') +if __name__ == "__main__": + c, k = Onepad().encrypt("Hello") print(c, k) print(Onepad().decrypt(c, k)) diff --git a/ciphers/playfair_cipher.py b/ciphers/playfair_cipher.py index 20449b161963..030fe8155a69 100644 --- a/ciphers/playfair_cipher.py +++ b/ciphers/playfair_cipher.py @@ -1,14 +1,14 @@ import string import itertools + def chunker(seq, size): it = iter(seq) while True: - chunk = tuple(itertools.islice(it, size)) - if not chunk: - return - yield chunk - + chunk = tuple(itertools.islice(it, size)) + if not chunk: + return + yield chunk def prepare_input(dirty): @@ -16,32 +16,33 @@ def prepare_input(dirty): Prepare the plaintext by up-casing it and separating repeated letters with X's """ - - dirty = ''.join([c.upper() for c in dirty if c in string.ascii_letters]) + + dirty = "".join([c.upper() for c in dirty if c in string.ascii_letters]) clean = "" - + if len(dirty) < 2: return dirty - for i in range(len(dirty)-1): + for i in range(len(dirty) - 1): clean += dirty[i] - - if dirty[i] == dirty[i+1]: - clean += 'X' - + + if dirty[i] == dirty[i + 1]: + clean += "X" + clean += dirty[-1] if len(clean) & 1: - clean += 'X' + clean += "X" return clean + def generate_table(key): # I and J are used interchangeably to allow # us to use a 5x5 table (25 letters) alphabet = "ABCDEFGHIKLMNOPQRSTUVWXYZ" - # we're using a list instead of a '2d' array because it makes the math + # we're using a list instead of a '2d' array because it makes the math # for setting up the table and doing the actual encoding/decoding simpler table = [] @@ -57,6 +58,7 @@ def generate_table(key): return table + def encode(plaintext, key): table = generate_table(key) plaintext = prepare_input(plaintext) @@ -68,14 +70,14 @@ def encode(plaintext, key): row2, col2 = divmod(table.index(char2), 5) if row1 == row2: - ciphertext += table[row1*5+(col1+1)%5] - ciphertext += table[row2*5+(col2+1)%5] + ciphertext += table[row1 * 5 + (col1 + 1) % 5] + ciphertext += table[row2 * 5 + (col2 + 1) % 5] elif col1 == col2: - ciphertext += table[((row1+1)%5)*5+col1] - ciphertext += table[((row2+1)%5)*5+col2] - else: # rectangle - ciphertext += table[row1*5+col2] - ciphertext += table[row2*5+col1] + ciphertext += table[((row1 + 1) % 5) * 5 + col1] + ciphertext += table[((row2 + 1) % 5) * 5 + col2] + else: # rectangle + ciphertext += table[row1 * 5 + col2] + ciphertext += table[row2 * 5 + col1] return ciphertext @@ -90,13 +92,13 @@ def decode(ciphertext, key): row2, col2 = divmod(table.index(char2), 5) if row1 == row2: - plaintext += table[row1*5+(col1-1)%5] - plaintext += table[row2*5+(col2-1)%5] + plaintext += table[row1 * 5 + (col1 - 1) % 5] + plaintext += table[row2 * 5 + (col2 - 1) % 5] elif col1 == col2: - plaintext += table[((row1-1)%5)*5+col1] - plaintext += table[((row2-1)%5)*5+col2] - else: # rectangle - plaintext += table[row1*5+col2] - plaintext += table[row2*5+col1] + plaintext += table[((row1 - 1) % 5) * 5 + col1] + plaintext += table[((row2 - 1) % 5) * 5 + col2] + else: # rectangle + plaintext += table[row1 * 5 + col2] + plaintext += table[row2 * 5 + col1] return plaintext diff --git a/ciphers/rabin_miller.py b/ciphers/rabin_miller.py index 21378cff6885..c544abdf9acc 100644 --- a/ciphers/rabin_miller.py +++ b/ciphers/rabin_miller.py @@ -2,6 +2,7 @@ import random + def rabinMiller(num): s = num - 1 t = 0 @@ -23,24 +24,181 @@ def rabinMiller(num): v = (v ** 2) % num return True + def isPrime(num): - if (num < 2): + if num < 2: return False - lowPrimes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, - 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, - 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, - 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, - 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, - 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, - 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, - 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, - 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, - 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, - 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, - 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, - 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, - 971, 977, 983, 991, 997] + lowPrimes = [ + 2, + 3, + 5, + 7, + 11, + 13, + 17, + 19, + 23, + 29, + 31, + 37, + 41, + 43, + 47, + 53, + 59, + 61, + 67, + 71, + 73, + 79, + 83, + 89, + 97, + 101, + 103, + 107, + 109, + 113, + 127, + 131, + 137, + 139, + 149, + 151, + 157, + 163, + 167, + 173, + 179, + 181, + 191, + 193, + 197, + 199, + 211, + 223, + 227, + 229, + 233, + 239, + 241, + 251, + 257, + 263, + 269, + 271, + 277, + 281, + 283, + 293, + 307, + 311, + 313, + 317, + 331, + 337, + 347, + 349, + 353, + 359, + 367, + 373, + 379, + 383, + 389, + 397, + 401, + 409, + 419, + 421, + 431, + 433, + 439, + 443, + 449, + 457, + 461, + 463, + 467, + 479, + 487, + 491, + 499, + 503, + 509, + 521, + 523, + 541, + 547, + 557, + 563, + 569, + 571, + 577, + 587, + 593, + 599, + 601, + 607, + 613, + 617, + 619, + 631, + 641, + 643, + 647, + 653, + 659, + 661, + 673, + 677, + 683, + 691, + 701, + 709, + 719, + 727, + 733, + 739, + 743, + 751, + 757, + 761, + 769, + 773, + 787, + 797, + 809, + 811, + 821, + 823, + 827, + 829, + 839, + 853, + 857, + 859, + 863, + 877, + 881, + 883, + 887, + 907, + 911, + 919, + 929, + 937, + 941, + 947, + 953, + 967, + 971, + 977, + 983, + 991, + 997, + ] if num in lowPrimes: return True @@ -51,13 +209,15 @@ def isPrime(num): return rabinMiller(num) -def generateLargePrime(keysize = 1024): + +def generateLargePrime(keysize=1024): while True: num = random.randrange(2 ** (keysize - 1), 2 ** (keysize)) if isPrime(num): return num -if __name__ == '__main__': + +if __name__ == "__main__": num = generateLargePrime() - print(('Prime number:', num)) - print(('isPrime:', isPrime(num))) + print(("Prime number:", num)) + print(("isPrime:", isPrime(num))) diff --git a/ciphers/rot13.py b/ciphers/rot13.py index 208de4890e67..a7b546511967 100644 --- a/ciphers/rot13.py +++ b/ciphers/rot13.py @@ -1,17 +1,17 @@ def dencrypt(s, n): - out = '' + out = "" for c in s: - if c >= 'A' and c <= 'Z': - out += chr(ord('A') + (ord(c) - ord('A') + n) % 26) - elif c >= 'a' and c <= 'z': - out += chr(ord('a') + (ord(c) - ord('a') + n) % 26) + if c >= "A" and c <= "Z": + out += chr(ord("A") + (ord(c) - ord("A") + n) % 26) + elif c >= "a" and c <= "z": + out += chr(ord("a") + (ord(c) - ord("a") + n) % 26) else: out += c return out def main(): - s0 = 'HELLO' + s0 = "HELLO" s1 = dencrypt(s0, 13) print(s1) # URYYB @@ -20,5 +20,5 @@ def main(): print(s2) # HELLO -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/ciphers/rsa_cipher.py b/ciphers/rsa_cipher.py index 02e5d95d1e95..a9b2dcc55daa 100644 --- a/ciphers/rsa_cipher.py +++ b/ciphers/rsa_cipher.py @@ -3,41 +3,42 @@ DEFAULT_BLOCK_SIZE = 128 BYTE_SIZE = 256 + def main(): - filename = 'encrypted_file.txt' - response = input(r'Encrypte\Decrypt [e\d]: ') + filename = "encrypted_file.txt" + response = input(r"Encrypte\Decrypt [e\d]: ") - if response.lower().startswith('e'): - mode = 'encrypt' - elif response.lower().startswith('d'): - mode = 'decrypt' + if response.lower().startswith("e"): + mode = "encrypt" + elif response.lower().startswith("d"): + mode = "decrypt" - if mode == 'encrypt': - if not os.path.exists('rsa_pubkey.txt'): - rkg.makeKeyFiles('rsa', 1024) + if mode == "encrypt": + if not os.path.exists("rsa_pubkey.txt"): + rkg.makeKeyFiles("rsa", 1024) - message = input('\nEnter message: ') - pubKeyFilename = 'rsa_pubkey.txt' - print('Encrypting and writing to %s...' % (filename)) + message = input("\nEnter message: ") + pubKeyFilename = "rsa_pubkey.txt" + print("Encrypting and writing to %s..." % (filename)) encryptedText = encryptAndWriteToFile(filename, pubKeyFilename, message) - print('\nEncrypted text:') + print("\nEncrypted text:") print(encryptedText) - elif mode == 'decrypt': - privKeyFilename = 'rsa_privkey.txt' - print('Reading from %s and decrypting...' % (filename)) + elif mode == "decrypt": + privKeyFilename = "rsa_privkey.txt" + print("Reading from %s and decrypting..." % (filename)) decryptedText = readFromFileAndDecrypt(filename, privKeyFilename) - print('writing decryption to rsa_decryption.txt...') - with open('rsa_decryption.txt', 'w') as dec: + print("writing decryption to rsa_decryption.txt...") + with open("rsa_decryption.txt", "w") as dec: dec.write(decryptedText) - print('\nDecryption:') + print("\nDecryption:") print(decryptedText) def getBlocksFromText(message, blockSize=DEFAULT_BLOCK_SIZE): - messageBytes = message.encode('ascii') + messageBytes = message.encode("ascii") blockInts = [] for blockStart in range(0, len(messageBytes), blockSize): @@ -58,7 +59,7 @@ def getTextFromBlocks(blockInts, messageLength, blockSize=DEFAULT_BLOCK_SIZE): blockInt = blockInt % (BYTE_SIZE ** i) blockMessage.insert(0, chr(asciiNumber)) message.extend(blockMessage) - return ''.join(message) + return "".join(message) def encryptMessage(message, key, blockSize=DEFAULT_BLOCK_SIZE): @@ -81,22 +82,27 @@ def decryptMessage(encryptedBlocks, messageLength, key, blockSize=DEFAULT_BLOCK_ def readKeyFile(keyFilename): with open(keyFilename) as fo: content = fo.read() - keySize, n, EorD = content.split(',') + keySize, n, EorD = content.split(",") return (int(keySize), int(n), int(EorD)) -def encryptAndWriteToFile(messageFilename, keyFilename, message, blockSize=DEFAULT_BLOCK_SIZE): +def encryptAndWriteToFile( + messageFilename, keyFilename, message, blockSize=DEFAULT_BLOCK_SIZE +): keySize, n, e = readKeyFile(keyFilename) if keySize < blockSize * 8: - sys.exit('ERROR: Block size is %s bits and key size is %s bits. The RSA cipher requires the block size to be equal to or greater than the key size. Either decrease the block size or use different keys.' % (blockSize * 8, keySize)) + sys.exit( + "ERROR: Block size is %s bits and key size is %s bits. The RSA cipher requires the block size to be equal to or greater than the key size. Either decrease the block size or use different keys." + % (blockSize * 8, keySize) + ) encryptedBlocks = encryptMessage(message, (n, e), blockSize) for i in range(len(encryptedBlocks)): encryptedBlocks[i] = str(encryptedBlocks[i]) - encryptedContent = ','.join(encryptedBlocks) - encryptedContent = '%s_%s_%s' % (len(message), blockSize, encryptedContent) - with open(messageFilename, 'w') as fo: + encryptedContent = ",".join(encryptedBlocks) + encryptedContent = "%s_%s_%s" % (len(message), blockSize, encryptedContent) + with open(messageFilename, "w") as fo: fo.write(encryptedContent) return encryptedContent @@ -105,18 +111,22 @@ def readFromFileAndDecrypt(messageFilename, keyFilename): keySize, n, d = readKeyFile(keyFilename) with open(messageFilename) as fo: content = fo.read() - messageLength, blockSize, encryptedMessage = content.split('_') + messageLength, blockSize, encryptedMessage = content.split("_") messageLength = int(messageLength) blockSize = int(blockSize) if keySize < blockSize * 8: - sys.exit('ERROR: Block size is %s bits and key size is %s bits. The RSA cipher requires the block size to be equal to or greater than the key size. Did you specify the correct key file and encrypted file?' % (blockSize * 8, keySize)) + sys.exit( + "ERROR: Block size is %s bits and key size is %s bits. The RSA cipher requires the block size to be equal to or greater than the key size. Did you specify the correct key file and encrypted file?" + % (blockSize * 8, keySize) + ) encryptedBlocks = [] - for block in encryptedMessage.split(','): + for block in encryptedMessage.split(","): encryptedBlocks.append(int(block)) return decryptMessage(encryptedBlocks, messageLength, (n, d), blockSize) -if __name__ == '__main__': + +if __name__ == "__main__": main() diff --git a/ciphers/rsa_key_generator.py b/ciphers/rsa_key_generator.py index 7cd7163b68d5..ce7c1f3dd12b 100644 --- a/ciphers/rsa_key_generator.py +++ b/ciphers/rsa_key_generator.py @@ -1,45 +1,54 @@ import random, sys, os import rabin_miller as rabinMiller, cryptomath_module as cryptoMath + def main(): - print('Making key files...') - makeKeyFiles('rsa', 1024) - print('Key files generation successful.') + print("Making key files...") + makeKeyFiles("rsa", 1024) + print("Key files generation successful.") + def generateKey(keySize): - print('Generating prime p...') + print("Generating prime p...") p = rabinMiller.generateLargePrime(keySize) - print('Generating prime q...') + print("Generating prime q...") q = rabinMiller.generateLargePrime(keySize) n = p * q - print('Generating e that is relatively prime to (p - 1) * (q - 1)...') + print("Generating e that is relatively prime to (p - 1) * (q - 1)...") while True: e = random.randrange(2 ** (keySize - 1), 2 ** (keySize)) if cryptoMath.gcd(e, (p - 1) * (q - 1)) == 1: break - print('Calculating d that is mod inverse of e...') + print("Calculating d that is mod inverse of e...") d = cryptoMath.findModInverse(e, (p - 1) * (q - 1)) publicKey = (n, e) privateKey = (n, d) return (publicKey, privateKey) + def makeKeyFiles(name, keySize): - if os.path.exists('%s_pubkey.txt' % (name)) or os.path.exists('%s_privkey.txt' % (name)): - print('\nWARNING:') - print('"%s_pubkey.txt" or "%s_privkey.txt" already exists. \nUse a different name or delete these files and re-run this program.' % (name, name)) + if os.path.exists("%s_pubkey.txt" % (name)) or os.path.exists( + "%s_privkey.txt" % (name) + ): + print("\nWARNING:") + print( + '"%s_pubkey.txt" or "%s_privkey.txt" already exists. \nUse a different name or delete these files and re-run this program.' + % (name, name) + ) sys.exit() publicKey, privateKey = generateKey(keySize) - print('\nWriting public key to file %s_pubkey.txt...' % name) - with open('%s_pubkey.txt' % name, 'w') as fo: - fo.write('%s,%s,%s' % (keySize, publicKey[0], publicKey[1])) + print("\nWriting public key to file %s_pubkey.txt..." % name) + with open("%s_pubkey.txt" % name, "w") as fo: + fo.write("%s,%s,%s" % (keySize, publicKey[0], publicKey[1])) + + print("Writing private key to file %s_privkey.txt..." % name) + with open("%s_privkey.txt" % name, "w") as fo: + fo.write("%s,%s,%s" % (keySize, privateKey[0], privateKey[1])) - print('Writing private key to file %s_privkey.txt...' % name) - with open('%s_privkey.txt' % name, 'w') as fo: - fo.write('%s,%s,%s' % (keySize, privateKey[0], privateKey[1])) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/ciphers/simple_substitution_cipher.py b/ciphers/simple_substitution_cipher.py index 5da07f8526b9..12511cc39bbc 100644 --- a/ciphers/simple_substitution_cipher.py +++ b/ciphers/simple_substitution_cipher.py @@ -1,22 +1,24 @@ import sys, random -LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' +LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + def main(): - message = input('Enter message: ') - key = 'LFWOAYUISVKMNXPBDCRJTQEGHZ' - resp = input('Encrypt/Decrypt [e/d]: ') + message = input("Enter message: ") + key = "LFWOAYUISVKMNXPBDCRJTQEGHZ" + resp = input("Encrypt/Decrypt [e/d]: ") checkValidKey(key) - if resp.lower().startswith('e'): - mode = 'encrypt' + if resp.lower().startswith("e"): + mode = "encrypt" translated = encryptMessage(key, message) - elif resp.lower().startswith('d'): - mode = 'decrypt' + elif resp.lower().startswith("d"): + mode = "decrypt" translated = decryptMessage(key, message) - print('\n%sion: \n%s' % (mode.title(), translated)) + print("\n%sion: \n%s" % (mode.title(), translated)) + def checkValidKey(key): keyList = list(key) @@ -25,28 +27,31 @@ def checkValidKey(key): lettersList.sort() if keyList != lettersList: - sys.exit('Error in the key or symbol set.') + sys.exit("Error in the key or symbol set.") + def encryptMessage(key, message): """ >>> encryptMessage('LFWOAYUISVKMNXPBDCRJTQEGHZ', 'Harshil Darji') 'Ilcrism Olcvs' """ - return translateMessage(key, message, 'encrypt') + return translateMessage(key, message, "encrypt") + def decryptMessage(key, message): """ >>> decryptMessage('LFWOAYUISVKMNXPBDCRJTQEGHZ', 'Ilcrism Olcvs') 'Harshil Darji' """ - return translateMessage(key, message, 'decrypt') + return translateMessage(key, message, "decrypt") + def translateMessage(key, message, mode): - translated = '' + translated = "" charsA = LETTERS charsB = key - if mode == 'decrypt': + if mode == "decrypt": charsA, charsB = charsB, charsA for symbol in message: @@ -61,10 +66,12 @@ def translateMessage(key, message, mode): return translated + def getRandomKey(): key = list(LETTERS) random.shuffle(key) - return ''.join(key) + return "".join(key) + -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/ciphers/trafid_cipher.py b/ciphers/trafid_cipher.py index 53f4d288bfe2..0add9ee74beb 100644 --- a/ciphers/trafid_cipher.py +++ b/ciphers/trafid_cipher.py @@ -1,4 +1,5 @@ -#https://en.wikipedia.org/wiki/Trifid_cipher +# https://en.wikipedia.org/wiki/Trifid_cipher + def __encryptPart(messagePart, character2Number): one, two, three = "", "", "" @@ -12,7 +13,8 @@ def __encryptPart(messagePart, character2Number): two += each[1] three += each[2] - return one+two+three + return one + two + three + def __decryptPart(messagePart, character2Number): tmp, thisPart = "", "" @@ -29,20 +31,49 @@ def __decryptPart(messagePart, character2Number): return result[0], result[1], result[2] + def __prepare(message, alphabet): - #Validate message and alphabet, set to upper and remove spaces + # Validate message and alphabet, set to upper and remove spaces alphabet = alphabet.replace(" ", "").upper() message = message.replace(" ", "").upper() - #Check length and characters + # Check length and characters if len(alphabet) != 27: raise KeyError("Length of alphabet has to be 27.") for each in message: if each not in alphabet: raise ValueError("Each message character has to be included in alphabet!") - #Generate dictionares - numbers = ("111","112","113","121","122","123","131","132","133","211","212","213","221","222","223","231","232","233","311","312","313","321","322","323","331","332","333") + # Generate dictionares + numbers = ( + "111", + "112", + "113", + "121", + "122", + "123", + "131", + "132", + "133", + "211", + "212", + "213", + "221", + "222", + "223", + "231", + "232", + "233", + "311", + "312", + "313", + "321", + "322", + "323", + "331", + "332", + "333", + ) character2Number = {} number2Character = {} for letter, number in zip(alphabet, numbers): @@ -51,36 +82,39 @@ def __prepare(message, alphabet): return message, alphabet, character2Number, number2Character -def encryptMessage(message, alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.", period=5): + +def encryptMessage(message, alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ.", period=5): message, alphabet, character2Number, number2Character = __prepare(message, alphabet) encrypted, encrypted_numeric = "", "" - for i in range(0, len(message)+1, period): - encrypted_numeric += __encryptPart(message[i:i+period], character2Number) + for i in range(0, len(message) + 1, period): + encrypted_numeric += __encryptPart(message[i : i + period], character2Number) for i in range(0, len(encrypted_numeric), 3): - encrypted += number2Character[encrypted_numeric[i:i+3]] + encrypted += number2Character[encrypted_numeric[i : i + 3]] return encrypted -def decryptMessage(message, alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.", period=5): + +def decryptMessage(message, alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ.", period=5): message, alphabet, character2Number, number2Character = __prepare(message, alphabet) decrypted_numeric = [] decrypted = "" - for i in range(0, len(message)+1, period): - a,b,c = __decryptPart(message[i:i+period], character2Number) + for i in range(0, len(message) + 1, period): + a, b, c = __decryptPart(message[i : i + period], character2Number) for j in range(0, len(a)): - decrypted_numeric.append(a[j]+b[j]+c[j]) + decrypted_numeric.append(a[j] + b[j] + c[j]) for each in decrypted_numeric: decrypted += number2Character[each] return decrypted -if __name__ == '__main__': + +if __name__ == "__main__": msg = "DEFEND THE EAST WALL OF THE CASTLE." - encrypted = encryptMessage(msg,"EPSDUCVWYM.ZLKXNBTFGORIJHAQ") + encrypted = encryptMessage(msg, "EPSDUCVWYM.ZLKXNBTFGORIJHAQ") decrypted = decryptMessage(encrypted, "EPSDUCVWYM.ZLKXNBTFGORIJHAQ") print("Encrypted: {}\nDecrypted: {}".format(encrypted, decrypted)) diff --git a/ciphers/transposition_cipher.py b/ciphers/transposition_cipher.py index 1c2ed0aa0452..b6c9195b5dee 100644 --- a/ciphers/transposition_cipher.py +++ b/ciphers/transposition_cipher.py @@ -1,30 +1,33 @@ import math + def main(): - message = input('Enter message: ') - key = int(input('Enter key [2-%s]: ' % (len(message) - 1))) - mode = input('Encryption/Decryption [e/d]: ') + message = input("Enter message: ") + key = int(input("Enter key [2-%s]: " % (len(message) - 1))) + mode = input("Encryption/Decryption [e/d]: ") - if mode.lower().startswith('e'): + if mode.lower().startswith("e"): text = encryptMessage(key, message) - elif mode.lower().startswith('d'): + elif mode.lower().startswith("d"): text = decryptMessage(key, message) # Append pipe symbol (vertical bar) to identify spaces at the end. - print('Output:\n%s' %(text + '|')) + print("Output:\n%s" % (text + "|")) + def encryptMessage(key, message): """ >>> encryptMessage(6, 'Harshil Darji') 'Hlia rDsahrij' """ - cipherText = [''] * key + cipherText = [""] * key for col in range(key): pointer = col while pointer < len(message): cipherText[col] += message[pointer] pointer += key - return ''.join(cipherText) + return "".join(cipherText) + def decryptMessage(key, message): """ @@ -35,19 +38,26 @@ def decryptMessage(key, message): numRows = key numShadedBoxes = (numCols * numRows) - len(message) plainText = [""] * numCols - col = 0; row = 0; + col = 0 + row = 0 for symbol in message: plainText[col] += symbol col += 1 - if (col == numCols) or (col == numCols - 1) and (row >= numRows - numShadedBoxes): + if ( + (col == numCols) + or (col == numCols - 1) + and (row >= numRows - numShadedBoxes) + ): col = 0 row += 1 return "".join(plainText) -if __name__ == '__main__': + +if __name__ == "__main__": import doctest + doctest.testmod() main() diff --git a/ciphers/transposition_cipher_encrypt_decrypt_file.py b/ciphers/transposition_cipher_encrypt_decrypt_file.py index 8ebfc1ea7e0c..775df354e117 100644 --- a/ciphers/transposition_cipher_encrypt_decrypt_file.py +++ b/ciphers/transposition_cipher_encrypt_decrypt_file.py @@ -1,36 +1,38 @@ import time, os, sys import transposition_cipher as transCipher + def main(): - inputFile = 'Prehistoric Men.txt' - outputFile = 'Output.txt' - key = int(input('Enter key: ')) - mode = input('Encrypt/Decrypt [e/d]: ') + inputFile = "Prehistoric Men.txt" + outputFile = "Output.txt" + key = int(input("Enter key: ")) + mode = input("Encrypt/Decrypt [e/d]: ") if not os.path.exists(inputFile): - print('File %s does not exist. Quitting...' % inputFile) + print("File %s does not exist. Quitting..." % inputFile) sys.exit() if os.path.exists(outputFile): - print('Overwrite %s? [y/n]' % outputFile) - response = input('> ') - if not response.lower().startswith('y'): + print("Overwrite %s? [y/n]" % outputFile) + response = input("> ") + if not response.lower().startswith("y"): sys.exit() startTime = time.time() - if mode.lower().startswith('e'): + if mode.lower().startswith("e"): with open(inputFile) as f: content = f.read() translated = transCipher.encryptMessage(key, content) - elif mode.lower().startswith('d'): + elif mode.lower().startswith("d"): with open(outputFile) as f: content = f.read() - translated =transCipher .decryptMessage(key, content) + translated = transCipher.decryptMessage(key, content) - with open(outputFile, 'w') as outputObj: + with open(outputFile, "w") as outputObj: outputObj.write(translated) totalTime = round(time.time() - startTime, 2) - print(('Done (', totalTime, 'seconds )')) + print(("Done (", totalTime, "seconds )")) + -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/ciphers/vigenere_cipher.py b/ciphers/vigenere_cipher.py index 95eeb431109f..6c10e7d773f2 100644 --- a/ciphers/vigenere_cipher.py +++ b/ciphers/vigenere_cipher.py @@ -1,33 +1,37 @@ -LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' +LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + def main(): - message = input('Enter message: ') - key = input('Enter key [alphanumeric]: ') - mode = input('Encrypt/Decrypt [e/d]: ') + message = input("Enter message: ") + key = input("Enter key [alphanumeric]: ") + mode = input("Encrypt/Decrypt [e/d]: ") - if mode.lower().startswith('e'): - mode = 'encrypt' + if mode.lower().startswith("e"): + mode = "encrypt" translated = encryptMessage(key, message) - elif mode.lower().startswith('d'): - mode = 'decrypt' + elif mode.lower().startswith("d"): + mode = "decrypt" translated = decryptMessage(key, message) - print('\n%sed message:' % mode.title()) + print("\n%sed message:" % mode.title()) print(translated) + def encryptMessage(key, message): - ''' + """ >>> encryptMessage('HDarji', 'This is Harshil Darji from Dharmaj.') 'Akij ra Odrjqqs Gaisq muod Mphumrs.' - ''' - return translateMessage(key, message, 'encrypt') + """ + return translateMessage(key, message, "encrypt") + def decryptMessage(key, message): - ''' + """ >>> decryptMessage('HDarji', 'Akij ra Odrjqqs Gaisq muod Mphumrs.') 'This is Harshil Darji from Dharmaj.' - ''' - return translateMessage(key, message, 'decrypt') + """ + return translateMessage(key, message, "decrypt") + def translateMessage(key, message, mode): translated = [] @@ -37,9 +41,9 @@ def translateMessage(key, message, mode): for symbol in message: num = LETTERS.find(symbol.upper()) if num != -1: - if mode == 'encrypt': + if mode == "encrypt": num += LETTERS.find(key[keyIndex]) - elif mode == 'decrypt': + elif mode == "decrypt": num -= LETTERS.find(key[keyIndex]) num %= len(LETTERS) @@ -54,7 +58,8 @@ def translateMessage(key, message, mode): keyIndex = 0 else: translated.append(symbol) - return ''.join(translated) + return "".join(translated) + -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/ciphers/xor_cipher.py b/ciphers/xor_cipher.py index 8bb94212c15a..7d8dbe41fdea 100644 --- a/ciphers/xor_cipher.py +++ b/ciphers/xor_cipher.py @@ -16,121 +16,120 @@ - encrypt_file : boolean - decrypt_file : boolean """ -class XORCipher(object): - def __init__(self, key = 0): - """ + +class XORCipher(object): + def __init__(self, key=0): + """ simple constructor that receives a key or uses default key = 0 """ - #private field - self.__key = key + # private field + self.__key = key - def encrypt(self, content, key): - """ + def encrypt(self, content, key): + """ input: 'content' of type string and 'key' of type int output: encrypted string 'content' as a list of chars if key not passed the method uses the key by the constructor. otherwise key = 1 """ - # precondition - assert (isinstance(key,int) and isinstance(content,str)) + # precondition + assert isinstance(key, int) and isinstance(content, str) - key = key or self.__key or 1 + key = key or self.__key or 1 - # make sure key can be any size - while (key > 255): - key -= 255 + # make sure key can be any size + while key > 255: + key -= 255 - # This will be returned - ans = [] + # This will be returned + ans = [] - for ch in content: - ans.append(chr(ord(ch) ^ key)) + for ch in content: + ans.append(chr(ord(ch) ^ key)) - return ans + return ans - def decrypt(self,content,key): - """ + def decrypt(self, content, key): + """ input: 'content' of type list and 'key' of type int output: decrypted string 'content' as a list of chars if key not passed the method uses the key by the constructor. otherwise key = 1 """ - # precondition - assert (isinstance(key,int) and isinstance(content,list)) + # precondition + assert isinstance(key, int) and isinstance(content, list) - key = key or self.__key or 1 + key = key or self.__key or 1 - # make sure key can be any size - while (key > 255): - key -= 255 + # make sure key can be any size + while key > 255: + key -= 255 - # This will be returned - ans = [] + # This will be returned + ans = [] - for ch in content: - ans.append(chr(ord(ch) ^ key)) + for ch in content: + ans.append(chr(ord(ch) ^ key)) - return ans + return ans - - def encrypt_string(self,content, key = 0): - """ + def encrypt_string(self, content, key=0): + """ input: 'content' of type string and 'key' of type int output: encrypted string 'content' if key not passed the method uses the key by the constructor. otherwise key = 1 """ - # precondition - assert (isinstance(key,int) and isinstance(content,str)) + # precondition + assert isinstance(key, int) and isinstance(content, str) - key = key or self.__key or 1 + key = key or self.__key or 1 - # make sure key can be any size - while (key > 255): - key -= 255 + # make sure key can be any size + while key > 255: + key -= 255 - # This will be returned - ans = "" + # This will be returned + ans = "" - for ch in content: - ans += chr(ord(ch) ^ key) + for ch in content: + ans += chr(ord(ch) ^ key) - return ans + return ans - def decrypt_string(self,content,key = 0): - """ + def decrypt_string(self, content, key=0): + """ input: 'content' of type string and 'key' of type int output: decrypted string 'content' if key not passed the method uses the key by the constructor. otherwise key = 1 """ - # precondition - assert (isinstance(key,int) and isinstance(content,str)) - - key = key or self.__key or 1 + # precondition + assert isinstance(key, int) and isinstance(content, str) - # make sure key can be any size - while (key > 255): - key -= 255 + key = key or self.__key or 1 - # This will be returned - ans = "" + # make sure key can be any size + while key > 255: + key -= 255 - for ch in content: - ans += chr(ord(ch) ^ key) + # This will be returned + ans = "" - return ans + for ch in content: + ans += chr(ord(ch) ^ key) + return ans - def encrypt_file(self, file, key = 0): - """ + def encrypt_file(self, file, key=0): + """ input: filename (str) and a key (int) output: returns true if encrypt process was successful otherwise false @@ -138,25 +137,24 @@ def encrypt_file(self, file, key = 0): otherwise key = 1 """ - #precondition - assert (isinstance(file,str) and isinstance(key,int)) - - try: - with open(file,"r") as fin: - with open("encrypt.out","w+") as fout: + # precondition + assert isinstance(file, str) and isinstance(key, int) - # actual encrypt-process - for line in fin: - fout.write(self.encrypt_string(line,key)) + try: + with open(file, "r") as fin: + with open("encrypt.out", "w+") as fout: - except: - return False + # actual encrypt-process + for line in fin: + fout.write(self.encrypt_string(line, key)) - return True + except: + return False + return True - def decrypt_file(self,file, key): - """ + def decrypt_file(self, file, key): + """ input: filename (str) and a key (int) output: returns true if decrypt process was successful otherwise false @@ -164,23 +162,21 @@ def decrypt_file(self,file, key): otherwise key = 1 """ - #precondition - assert (isinstance(file,str) and isinstance(key,int)) - - try: - with open(file,"r") as fin: - with open("decrypt.out","w+") as fout: - - # actual encrypt-process - for line in fin: - fout.write(self.decrypt_string(line,key)) + # precondition + assert isinstance(file, str) and isinstance(key, int) - except: - return False + try: + with open(file, "r") as fin: + with open("decrypt.out", "w+") as fout: - return True + # actual encrypt-process + for line in fin: + fout.write(self.decrypt_string(line, key)) + except: + return False + return True # Tests diff --git a/compression/burrows_wheeler.py b/compression/burrows_wheeler.py index fabeab39adf8..50ee62aa0cb3 100644 --- a/compression/burrows_wheeler.py +++ b/compression/burrows_wheeler.py @@ -141,15 +141,10 @@ def reverse_bwt(bwt_string: str, idx_original_string: int) -> str: ) ) if idx_original_string < 0: - raise ValueError( - "The parameter idx_original_string must not be lower than 0." - ) + raise ValueError("The parameter idx_original_string must not be lower than 0.") if idx_original_string >= len(bwt_string): raise ValueError( - ( - "The parameter idx_original_string must be lower than" - " len(bwt_string)." - ) + ("The parameter idx_original_string must be lower than" " len(bwt_string).") ) ordered_rotations = [""] * len(bwt_string) @@ -166,9 +161,7 @@ def reverse_bwt(bwt_string: str, idx_original_string: int) -> str: result = bwt_transform(s) bwt_output_msg = "Burrows Wheeler tranform for string '{}' results in '{}'" print(bwt_output_msg.format(s, result["bwt_string"])) - original_string = reverse_bwt( - result["bwt_string"], result["idx_original_string"] - ) + original_string = reverse_bwt(result["bwt_string"], result["idx_original_string"]) fmt = ( "Reversing Burrows Wheeler tranform for entry '{}' we get original" " string '{}'" diff --git a/compression/huffman.py b/compression/huffman.py index 7417551ba209..73c084351c85 100644 --- a/compression/huffman.py +++ b/compression/huffman.py @@ -1,5 +1,6 @@ import sys + class Letter: def __init__(self, letter, freq): self.letter = letter @@ -7,7 +8,7 @@ def __init__(self, letter, freq): self.bitstring = "" def __repr__(self): - return f'{self.letter}:{self.freq}' + return f"{self.letter}:{self.freq}" class TreeNode: @@ -31,6 +32,7 @@ def parse_file(file_path): chars[c] = chars[c] + 1 if c in chars.keys() else 1 return sorted([Letter(c, f) for c, f in chars.items()], key=lambda l: l.freq) + def build_tree(letters): """ Run through the list of Letters and build the min heap @@ -45,6 +47,7 @@ def build_tree(letters): letters.sort(key=lambda l: l.freq) return letters[0] + def traverse_tree(root, bitstring): """ Recursively traverse the Huffman Tree to set each @@ -58,6 +61,7 @@ def traverse_tree(root, bitstring): letters += traverse_tree(root.right, bitstring + "1") return letters + def huffman(file_path): """ Parse the file, build the tree, then run through the file @@ -67,7 +71,7 @@ def huffman(file_path): letters_list = parse_file(file_path) root = build_tree(letters_list) letters = traverse_tree(root, "") - print(f'Huffman Coding of {file_path}: ') + print(f"Huffman Coding of {file_path}: ") with open(file_path) as f: while True: c = f.read(1) @@ -77,6 +81,7 @@ def huffman(file_path): print(le.bitstring, end=" ") print() + if __name__ == "__main__": # pass the file path to the huffman function huffman(sys.argv[1]) diff --git a/compression/peak_signal_to_noise_ratio.py b/compression/peak_signal_to_noise_ratio.py index b0efb1462dcc..418832a8127c 100644 --- a/compression/peak_signal_to_noise_ratio.py +++ b/compression/peak_signal_to_noise_ratio.py @@ -9,6 +9,7 @@ import cv2 import numpy as np + def psnr(original, contrast): mse = np.mean((original - contrast) ** 2) if mse == 0: @@ -21,11 +22,13 @@ def psnr(original, contrast): def main(): dir_path = os.path.dirname(os.path.realpath(__file__)) # Loading images (original image and compressed image) - original = cv2.imread(os.path.join(dir_path, 'image_data/original_image.png')) - contrast = cv2.imread(os.path.join(dir_path, 'image_data/compressed_image.png'), 1) + original = cv2.imread(os.path.join(dir_path, "image_data/original_image.png")) + contrast = cv2.imread(os.path.join(dir_path, "image_data/compressed_image.png"), 1) - original2 = cv2.imread(os.path.join(dir_path, 'image_data/PSNR-example-base.png')) - contrast2 = cv2.imread(os.path.join(dir_path, 'image_data/PSNR-example-comp-10.jpg'), 1) + original2 = cv2.imread(os.path.join(dir_path, "image_data/PSNR-example-base.png")) + contrast2 = cv2.imread( + os.path.join(dir_path, "image_data/PSNR-example-comp-10.jpg"), 1 + ) # Value expected: 29.73dB print("-- First Test --") @@ -36,5 +39,5 @@ def main(): print(f"PSNR value is {psnr(original2, contrast2)} dB") -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/conversions/decimal_to_binary.py b/conversions/decimal_to_binary.py index 934cf0dfb363..ad4ba166745d 100644 --- a/conversions/decimal_to_binary.py +++ b/conversions/decimal_to_binary.py @@ -55,4 +55,5 @@ def decimal_to_binary(num): if __name__ == "__main__": import doctest + doctest.testmod() diff --git a/conversions/decimal_to_hexadecimal.py b/conversions/decimal_to_hexadecimal.py index e6435f1ef570..a70e3c7b97bf 100644 --- a/conversions/decimal_to_hexadecimal.py +++ b/conversions/decimal_to_hexadecimal.py @@ -2,24 +2,25 @@ # set decimal value for each hexadecimal digit values = { - 0:'0', - 1:'1', - 2:'2', - 3:'3', - 4:'4', - 5:'5', - 6:'6', - 7:'7', - 8:'8', - 9:'9', - 10:'a', - 11:'b', - 12:'c', - 13:'d', - 14:'e', - 15:'f' + 0: "0", + 1: "1", + 2: "2", + 3: "3", + 4: "4", + 5: "5", + 6: "6", + 7: "7", + 8: "8", + 9: "9", + 10: "a", + 11: "b", + 12: "c", + 13: "d", + 14: "e", + 15: "f", } + def decimal_to_hexadecimal(decimal): """ take integer decimal value, return hexadecimal representation as str beginning with 0x @@ -56,7 +57,7 @@ def decimal_to_hexadecimal(decimal): True """ assert type(decimal) in (int, float) and decimal == int(decimal) - hexadecimal = '' + hexadecimal = "" negative = False if decimal < 0: negative = True @@ -64,11 +65,13 @@ def decimal_to_hexadecimal(decimal): while decimal > 0: decimal, remainder = divmod(decimal, 16) hexadecimal = values[remainder] + hexadecimal - hexadecimal = '0x' + hexadecimal + hexadecimal = "0x" + hexadecimal if negative: - hexadecimal = '-' + hexadecimal + hexadecimal = "-" + hexadecimal return hexadecimal -if __name__ == '__main__': + +if __name__ == "__main__": import doctest + doctest.testmod() diff --git a/conversions/decimal_to_octal.py b/conversions/decimal_to_octal.py index 187a0300e33a..0b005429d9d7 100644 --- a/conversions/decimal_to_octal.py +++ b/conversions/decimal_to_octal.py @@ -16,7 +16,7 @@ def decimal_to_octal(num): counter += 1 num = math.floor(num / 8) # basically /= 8 without remainder if any # This formatting removes trailing '.0' from `octal`. - return'{0:g}'.format(float(octal)) + return "{0:g}".format(float(octal)) def main(): @@ -34,5 +34,5 @@ def main(): print("\n") -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/data_structures/binary_tree/avl_tree.py b/data_structures/binary_tree/avl_tree.py index ff44963d1690..31d12c811105 100644 --- a/data_structures/binary_tree/avl_tree.py +++ b/data_structures/binary_tree/avl_tree.py @@ -1,71 +1,88 @@ # -*- coding: utf-8 -*- -''' +""" An auto-balanced binary tree! -''' +""" import math import random + + class my_queue: def __init__(self): self.data = [] self.head = 0 self.tail = 0 + def isEmpty(self): return self.head == self.tail - def push(self,data): + + def push(self, data): self.data.append(data) self.tail = self.tail + 1 + def pop(self): ret = self.data[self.head] self.head = self.head + 1 return ret + def count(self): return self.tail - self.head + def print(self): print(self.data) print("**************") - print(self.data[self.head:self.tail]) - + print(self.data[self.head : self.tail]) + + class my_node: - def __init__(self,data): + def __init__(self, data): self.data = data self.left = None self.right = None self.height = 1 + def getdata(self): return self.data + def getleft(self): return self.left + def getright(self): return self.right + def getheight(self): return self.height - def setdata(self,data): + + def setdata(self, data): self.data = data return - def setleft(self,node): + + def setleft(self, node): self.left = node return - def setright(self,node): + + def setright(self, node): self.right = node return - def setheight(self,height): + + def setheight(self, height): self.height = height return + def getheight(node): if node is None: return 0 return node.getheight() -def my_max(a,b): + +def my_max(a, b): if a > b: return a return b - def leftrotation(node): - r''' + r""" A B / \ / \ B C Bl A @@ -75,33 +92,35 @@ def leftrotation(node): UB UB = unbalanced node - ''' - print("left rotation node:",node.getdata()) + """ + print("left rotation node:", node.getdata()) ret = node.getleft() node.setleft(ret.getright()) ret.setright(node) - h1 = my_max(getheight(node.getright()),getheight(node.getleft())) + 1 + h1 = my_max(getheight(node.getright()), getheight(node.getleft())) + 1 node.setheight(h1) - h2 = my_max(getheight(ret.getright()),getheight(ret.getleft())) + 1 + h2 = my_max(getheight(ret.getright()), getheight(ret.getleft())) + 1 ret.setheight(h2) return ret + def rightrotation(node): - ''' + """ a mirror symmetry rotation of the leftrotation - ''' - print("right rotation node:",node.getdata()) + """ + print("right rotation node:", node.getdata()) ret = node.getright() node.setright(ret.getleft()) ret.setleft(node) - h1 = my_max(getheight(node.getright()),getheight(node.getleft())) + 1 + h1 = my_max(getheight(node.getright()), getheight(node.getleft())) + 1 node.setheight(h1) - h2 = my_max(getheight(ret.getright()),getheight(ret.getleft())) + 1 + h2 = my_max(getheight(ret.getright()), getheight(ret.getleft())) + 1 ret.setheight(h2) return ret + def rlrotation(node): - r''' + r""" A A Br / \ / \ / \ B C RR Br C LR B A @@ -110,51 +129,60 @@ def rlrotation(node): \ / UB Bl RR = rightrotation LR = leftrotation - ''' + """ node.setleft(rightrotation(node.getleft())) return leftrotation(node) + def lrrotation(node): node.setright(leftrotation(node.getright())) return rightrotation(node) -def insert_node(node,data): +def insert_node(node, data): if node is None: return my_node(data) if data < node.getdata(): - node.setleft(insert_node(node.getleft(),data)) - if getheight(node.getleft()) - getheight(node.getright()) == 2: #an unbalance detected - if data < node.getleft().getdata(): #new node is the left child of the left child + node.setleft(insert_node(node.getleft(), data)) + if ( + getheight(node.getleft()) - getheight(node.getright()) == 2 + ): # an unbalance detected + if ( + data < node.getleft().getdata() + ): # new node is the left child of the left child node = leftrotation(node) else: - node = rlrotation(node) #new node is the right child of the left child + node = rlrotation(node) # new node is the right child of the left child else: - node.setright(insert_node(node.getright(),data)) + node.setright(insert_node(node.getright(), data)) if getheight(node.getright()) - getheight(node.getleft()) == 2: if data < node.getright().getdata(): node = lrrotation(node) else: node = rightrotation(node) - h1 = my_max(getheight(node.getright()),getheight(node.getleft())) + 1 + h1 = my_max(getheight(node.getright()), getheight(node.getleft())) + 1 node.setheight(h1) return node + def getRightMost(root): while root.getright() is not None: root = root.getright() return root.getdata() + + def getLeftMost(root): while root.getleft() is not None: root = root.getleft() return root.getdata() -def del_node(root,data): + +def del_node(root, data): if root.getdata() == data: if root.getleft() is not None and root.getright() is not None: temp_data = getLeftMost(root.getright()) root.setdata(temp_data) - root.setright(del_node(root.getright(),temp_data)) + root.setright(del_node(root.getright(), temp_data)) elif root.getleft() is not None: root = root.getleft() else: @@ -164,12 +192,12 @@ def del_node(root,data): print("No such data") return root else: - root.setleft(del_node(root.getleft(),data)) + root.setleft(del_node(root.getleft(), data)) elif root.getdata() < data: if root.getright() is None: return root else: - root.setright(del_node(root.getright(),data)) + root.setright(del_node(root.getright(), data)) if root is None: return root if getheight(root.getright()) - getheight(root.getleft()) == 2: @@ -182,27 +210,31 @@ def del_node(root,data): root = leftrotation(root) else: root = rlrotation(root) - height = my_max(getheight(root.getright()),getheight(root.getleft())) + 1 + height = my_max(getheight(root.getright()), getheight(root.getleft())) + 1 root.setheight(height) return root + class AVLtree: def __init__(self): self.root = None + def getheight(self): -# print("yyy") + # print("yyy") return getheight(self.root) - def insert(self,data): - print("insert:"+str(data)) - self.root = insert_node(self.root,data) - - def del_node(self,data): - print("delete:"+str(data)) + + def insert(self, data): + print("insert:" + str(data)) + self.root = insert_node(self.root, data) + + def del_node(self, data): + print("delete:" + str(data)) if self.root is None: print("Tree is empty!") return - self.root = del_node(self.root,data) - def traversale(self): #a level traversale, gives a more intuitive look on the tree + self.root = del_node(self.root, data) + + def traversale(self): # a level traversale, gives a more intuitive look on the tree q = my_queue() q.push(self.root) layer = self.getheight() @@ -211,21 +243,21 @@ def traversale(self): #a level traversale, gives a more intuitive look on the tr cnt = 0 while not q.isEmpty(): node = q.pop() - space = " "*int(math.pow(2,layer-1)) - print(space,end = "") + space = " " * int(math.pow(2, layer - 1)) + print(space, end="") if node is None: - print("*",end = "") + print("*", end="") q.push(None) q.push(None) else: - print(node.getdata(),end = "") + print(node.getdata(), end="") q.push(node.getleft()) q.push(node.getright()) - print(space,end = "") + print(space, end="") cnt = cnt + 1 for i in range(100): - if cnt == math.pow(2,i) - 1: - layer = layer -1 + if cnt == math.pow(2, i) - 1: + layer = layer - 1 if layer == 0: print() print("*************************************") @@ -235,11 +267,13 @@ def traversale(self): #a level traversale, gives a more intuitive look on the tr print() print("*************************************") return - + def test(self): getheight(None) print("****") self.getheight() + + if __name__ == "__main__": t = AVLtree() t.traversale() @@ -248,7 +282,7 @@ def test(self): for i in l: t.insert(i) t.traversale() - + random.shuffle(l) for i in l: t.del_node(i) diff --git a/data_structures/binary_tree/basic_binary_tree.py b/data_structures/binary_tree/basic_binary_tree.py index 7c6240fb4dd4..6b7de7803704 100644 --- a/data_structures/binary_tree/basic_binary_tree.py +++ b/data_structures/binary_tree/basic_binary_tree.py @@ -1,12 +1,13 @@ -class Node: # This is the Class Node with constructor that contains data variable to type data and left,right pointers. +class Node: # This is the Class Node with constructor that contains data variable to type data and left,right pointers. def __init__(self, data): self.data = data self.left = None self.right = None -def display(tree): #In Order traversal of the tree - if tree is None: +def display(tree): # In Order traversal of the tree + + if tree is None: return if tree.left is not None: @@ -19,7 +20,10 @@ def display(tree): #In Order traversal of the tree return -def depth_of_tree(tree): #This is the recursive function to find the depth of binary tree. + +def depth_of_tree( + tree +): # This is the recursive function to find the depth of binary tree. if tree is None: return 0 else: @@ -31,18 +35,20 @@ def depth_of_tree(tree): #This is the recursive function to find the depth of bi return 1 + depth_r_tree -def is_full_binary_tree(tree): # This functions returns that is it full binary tree or not? +def is_full_binary_tree( + tree +): # This functions returns that is it full binary tree or not? if tree is None: return True if (tree.left is None) and (tree.right is None): return True if (tree.left is not None) and (tree.right is not None): - return (is_full_binary_tree(tree.left) and is_full_binary_tree(tree.right)) + return is_full_binary_tree(tree.left) and is_full_binary_tree(tree.right) else: return False -def main(): # Main func for testing. +def main(): # Main func for testing. tree = Node(1) tree.left = Node(2) tree.right = Node(3) @@ -59,5 +65,5 @@ def main(): # Main func for testing. display(tree) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/data_structures/binary_tree/binary_search_tree.py b/data_structures/binary_tree/binary_search_tree.py index 634b6cbcc231..c6e037880bb6 100644 --- a/data_structures/binary_tree/binary_search_tree.py +++ b/data_structures/binary_tree/binary_search_tree.py @@ -1,13 +1,14 @@ -''' +""" A binary search Tree -''' -class Node: +""" + +class Node: def __init__(self, label, parent): self.label = label self.left = None self.right = None - #Added in order to delete a node easier + # Added in order to delete a node easier self.parent = parent def getLabel(self): @@ -34,8 +35,8 @@ def getParent(self): def setParent(self, parent): self.parent = parent -class BinarySearchTree: +class BinarySearchTree: def __init__(self): self.root = None @@ -46,90 +47,90 @@ def insert(self, label): if self.empty(): self.root = new_node else: - #If Tree is not empty + # If Tree is not empty curr_node = self.root - #While we don't get to a leaf + # While we don't get to a leaf while curr_node is not None: - #We keep reference of the parent node + # We keep reference of the parent node parent_node = curr_node - #If node label is less than current node + # If node label is less than current node if new_node.getLabel() < curr_node.getLabel(): - #We go left + # We go left curr_node = curr_node.getLeft() else: - #Else we go right + # Else we go right curr_node = curr_node.getRight() - #We insert the new node in a leaf + # We insert the new node in a leaf if new_node.getLabel() < parent_node.getLabel(): parent_node.setLeft(new_node) else: parent_node.setRight(new_node) - #Set parent to the new node + # Set parent to the new node new_node.setParent(parent_node) def delete(self, label): - if (not self.empty()): - #Look for the node with that label + if not self.empty(): + # Look for the node with that label node = self.getNode(label) - #If the node exists - if(node is not None): - #If it has no children - if(node.getLeft() is None and node.getRight() is None): + # If the node exists + if node is not None: + # If it has no children + if node.getLeft() is None and node.getRight() is None: self.__reassignNodes(node, None) node = None - #Has only right children - elif(node.getLeft() is None and node.getRight() is not None): + # Has only right children + elif node.getLeft() is None and node.getRight() is not None: self.__reassignNodes(node, node.getRight()) - #Has only left children - elif(node.getLeft() is not None and node.getRight() is None): + # Has only left children + elif node.getLeft() is not None and node.getRight() is None: self.__reassignNodes(node, node.getLeft()) - #Has two children + # Has two children else: - #Gets the max value of the left branch + # Gets the max value of the left branch tmpNode = self.getMax(node.getLeft()) - #Deletes the tmpNode + # Deletes the tmpNode self.delete(tmpNode.getLabel()) - #Assigns the value to the node to delete and keesp tree structure + # Assigns the value to the node to delete and keesp tree structure node.setLabel(tmpNode.getLabel()) def getNode(self, label): curr_node = None - #If the tree is not empty - if(not self.empty()): - #Get tree root + # If the tree is not empty + if not self.empty(): + # Get tree root curr_node = self.getRoot() - #While we don't find the node we look for - #I am using lazy evaluation here to avoid NoneType Attribute error + # While we don't find the node we look for + # I am using lazy evaluation here to avoid NoneType Attribute error while curr_node is not None and curr_node.getLabel() is not label: - #If node label is less than current node + # If node label is less than current node if label < curr_node.getLabel(): - #We go left + # We go left curr_node = curr_node.getLeft() else: - #Else we go right + # Else we go right curr_node = curr_node.getRight() return curr_node - def getMax(self, root = None): - if(root is not None): + def getMax(self, root=None): + if root is not None: curr_node = root else: - #We go deep on the right branch + # We go deep on the right branch curr_node = self.getRoot() - if(not self.empty()): - while(curr_node.getRight() is not None): + if not self.empty(): + while curr_node.getRight() is not None: curr_node = curr_node.getRight() return curr_node - def getMin(self, root = None): - if(root is not None): + def getMin(self, root=None): + if root is not None: curr_node = root else: - #We go deep on the left branch + # We go deep on the left branch curr_node = self.getRoot() - if(not self.empty()): + if not self.empty(): curr_node = self.getRoot() - while(curr_node.getLeft() is not None): + while curr_node.getLeft() is not None: curr_node = curr_node.getLeft() return curr_node @@ -150,34 +151,34 @@ def getRoot(self): return self.root def __isRightChildren(self, node): - if(node == node.getParent().getRight()): + if node == node.getParent().getRight(): return True return False def __reassignNodes(self, node, newChildren): - if(newChildren is not None): + if newChildren is not None: newChildren.setParent(node.getParent()) - if(node.getParent() is not None): - #If it is the Right Children - if(self.__isRightChildren(node)): + if node.getParent() is not None: + # If it is the Right Children + if self.__isRightChildren(node): node.getParent().setRight(newChildren) else: - #Else it is the left children + # Else it is the left children node.getParent().setLeft(newChildren) - #This function traversal the tree. By default it returns an - #In order traversal list. You can pass a function to traversal - #The tree as needed by client code - def traversalTree(self, traversalFunction = None, root = None): - if(traversalFunction is None): - #Returns a list of nodes in preOrder by default + # This function traversal the tree. By default it returns an + # In order traversal list. You can pass a function to traversal + # The tree as needed by client code + def traversalTree(self, traversalFunction=None, root=None): + if traversalFunction is None: + # Returns a list of nodes in preOrder by default return self.__InOrderTraversal(self.root) else: - #Returns a list of nodes in the order that the users wants to + # Returns a list of nodes in the order that the users wants to return traversalFunction(self.root) - #Returns an string of all the nodes labels in the list - #In Order Traversal + # Returns an string of all the nodes labels in the list + # In Order Traversal def __str__(self): list = self.__InOrderTraversal(self.root) str = "" @@ -185,6 +186,7 @@ def __str__(self): str = str + " " + x.getLabel().__str__() return str + def InPreOrder(curr_node): nodeList = [] if curr_node is not None: @@ -193,8 +195,9 @@ def InPreOrder(curr_node): nodeList = nodeList + InPreOrder(curr_node.getRight()) return nodeList + def testBinarySearchTree(): - r''' + r""" Example 8 / \ @@ -203,15 +206,15 @@ def testBinarySearchTree(): 1 6 14 / \ / 4 7 13 - ''' + """ - r''' + r""" Example After Deletion 7 / \ 1 4 - ''' + """ t = BinarySearchTree() t.insert(8) t.insert(3) @@ -223,20 +226,20 @@ def testBinarySearchTree(): t.insert(4) t.insert(7) - #Prints all the elements of the list in order traversal + # Prints all the elements of the list in order traversal print(t.__str__()) - if(t.getNode(6) is not None): + if t.getNode(6) is not None: print("The label 6 exists") else: print("The label 6 doesn't exist") - if(t.getNode(-1) is not None): + if t.getNode(-1) is not None: print("The label -1 exists") else: print("The label -1 doesn't exist") - if(not t.empty()): + if not t.empty(): print(("Max Value: ", t.getMax().getLabel())) print(("Min Value: ", t.getMin().getLabel())) @@ -247,11 +250,12 @@ def testBinarySearchTree(): t.delete(6) t.delete(14) - #Gets all the elements of the tree In pre order - #And it prints them + # Gets all the elements of the tree In pre order + # And it prints them list = t.traversalTree(InPreOrder, t.root) for x in list: print(x) + if __name__ == "__main__": testBinarySearchTree() diff --git a/data_structures/binary_tree/fenwick_tree.py b/data_structures/binary_tree/fenwick_tree.py index 30a87fbd7fcf..54f0f07ac68d 100644 --- a/data_structures/binary_tree/fenwick_tree.py +++ b/data_structures/binary_tree/fenwick_tree.py @@ -1,28 +1,28 @@ class FenwickTree: - - def __init__(self, SIZE): # create fenwick tree with size SIZE + def __init__(self, SIZE): # create fenwick tree with size SIZE self.Size = SIZE - self.ft = [0 for i in range (0,SIZE)] + self.ft = [0 for i in range(0, SIZE)] - def update(self, i, val): # update data (adding) in index i in O(lg N) - while (i < self.Size): + def update(self, i, val): # update data (adding) in index i in O(lg N) + while i < self.Size: self.ft[i] += val i += i & (-i) - def query(self, i): # query cumulative data from index 0 to i in O(lg N) + def query(self, i): # query cumulative data from index 0 to i in O(lg N) ret = 0 - while (i > 0): + while i > 0: ret += self.ft[i] i -= i & (-i) return ret -if __name__ == '__main__': + +if __name__ == "__main__": f = FenwickTree(100) - f.update(1,20) - f.update(4,4) + f.update(1, 20) + f.update(4, 4) print(f.query(1)) print(f.query(3)) print(f.query(4)) - f.update(2,-5) + f.update(2, -5) print(f.query(1)) print(f.query(3)) diff --git a/data_structures/binary_tree/lazy_segment_tree.py b/data_structures/binary_tree/lazy_segment_tree.py index bbe37a6eb97f..acd551b41b96 100644 --- a/data_structures/binary_tree/lazy_segment_tree.py +++ b/data_structures/binary_tree/lazy_segment_tree.py @@ -1,34 +1,38 @@ import math -class SegmentTree: +class SegmentTree: def __init__(self, N): self.N = N - self.st = [0 for i in range(0,4*N)] # approximate the overall size of segment tree with array N - self.lazy = [0 for i in range(0,4*N)] # create array to store lazy update - self.flag = [0 for i in range(0,4*N)] # flag for lazy update + self.st = [ + 0 for i in range(0, 4 * N) + ] # approximate the overall size of segment tree with array N + self.lazy = [0 for i in range(0, 4 * N)] # create array to store lazy update + self.flag = [0 for i in range(0, 4 * N)] # flag for lazy update def left(self, idx): - return idx*2 + return idx * 2 def right(self, idx): - return idx*2 + 1 + return idx * 2 + 1 def build(self, idx, l, r, A): - if l==r: - self.st[idx] = A[l-1] - else : - mid = (l+r)//2 - self.build(self.left(idx),l,mid, A) - self.build(self.right(idx),mid+1,r, A) - self.st[idx] = max(self.st[self.left(idx)] , self.st[self.right(idx)]) + if l == r: + self.st[idx] = A[l - 1] + else: + mid = (l + r) // 2 + self.build(self.left(idx), l, mid, A) + self.build(self.right(idx), mid + 1, r, A) + self.st[idx] = max(self.st[self.left(idx)], self.st[self.right(idx)]) # update with O(lg N) (Normal segment tree without lazy update will take O(Nlg N) for each update) - def update(self, idx, l, r, a, b, val): # update(1, 1, N, a, b, v) for update val v to [a,b] + def update( + self, idx, l, r, a, b, val + ): # update(1, 1, N, a, b, v) for update val v to [a,b] if self.flag[idx] == True: self.st[idx] = self.lazy[idx] self.flag[idx] = False - if l!=r: + if l != r: self.lazy[self.left(idx)] = self.lazy[idx] self.lazy[self.right(idx)] = self.lazy[idx] self.flag[self.left(idx)] = True @@ -36,22 +40,22 @@ def update(self, idx, l, r, a, b, val): # update(1, 1, N, a, b, v) for update va if r < a or l > b: return True - if l >= a and r <= b : + if l >= a and r <= b: self.st[idx] = val - if l!=r: + if l != r: self.lazy[self.left(idx)] = val self.lazy[self.right(idx)] = val self.flag[self.left(idx)] = True self.flag[self.right(idx)] = True return True - mid = (l+r)//2 - self.update(self.left(idx),l,mid,a,b,val) - self.update(self.right(idx),mid+1,r,a,b,val) - self.st[idx] = max(self.st[self.left(idx)] , self.st[self.right(idx)]) + mid = (l + r) // 2 + self.update(self.left(idx), l, mid, a, b, val) + self.update(self.right(idx), mid + 1, r, a, b, val) + self.st[idx] = max(self.st[self.left(idx)], self.st[self.right(idx)]) return True # query with O(lg N) - def query(self, idx, l, r, a, b): #query(1, 1, N, a, b) for query max of [a,b] + def query(self, idx, l, r, a, b): # query(1, 1, N, a, b) for query max of [a,b] if self.flag[idx] == True: self.st[idx] = self.lazy[idx] self.flag[idx] = False @@ -64,27 +68,27 @@ def query(self, idx, l, r, a, b): #query(1, 1, N, a, b) for query max of [a,b] return -math.inf if l >= a and r <= b: return self.st[idx] - mid = (l+r)//2 - q1 = self.query(self.left(idx),l,mid,a,b) - q2 = self.query(self.right(idx),mid+1,r,a,b) - return max(q1,q2) + mid = (l + r) // 2 + q1 = self.query(self.left(idx), l, mid, a, b) + q2 = self.query(self.right(idx), mid + 1, r, a, b) + return max(q1, q2) def showData(self): showList = [] - for i in range(1,N+1): + for i in range(1, N + 1): showList += [self.query(1, 1, self.N, i, i)] print(showList) -if __name__ == '__main__': - A = [1,2,-4,7,3,-5,6,11,-20,9,14,15,5,2,-8] +if __name__ == "__main__": + A = [1, 2, -4, 7, 3, -5, 6, 11, -20, 9, 14, 15, 5, 2, -8] N = 15 segt = SegmentTree(N) - segt.build(1,1,N,A) - print(segt.query(1,1,N,4,6)) - print(segt.query(1,1,N,7,11)) - print(segt.query(1,1,N,7,12)) - segt.update(1,1,N,1,3,111) - print(segt.query(1,1,N,1,15)) - segt.update(1,1,N,7,8,235) + segt.build(1, 1, N, A) + print(segt.query(1, 1, N, 4, 6)) + print(segt.query(1, 1, N, 7, 11)) + print(segt.query(1, 1, N, 7, 12)) + segt.update(1, 1, N, 1, 3, 111) + print(segt.query(1, 1, N, 1, 15)) + segt.update(1, 1, N, 7, 8, 235) segt.showData() diff --git a/data_structures/binary_tree/lca.py b/data_structures/binary_tree/lca.py index 9c9d8ca629c7..c18f1e944bab 100644 --- a/data_structures/binary_tree/lca.py +++ b/data_structures/binary_tree/lca.py @@ -75,7 +75,7 @@ def main(): 10: [], 11: [], 12: [], - 13: [] + 13: [], } level, parent = bfs(level, parent, max_node, graph, 1) parent = creatSparse(max_node, parent) diff --git a/data_structures/binary_tree/red_black_tree.py b/data_structures/binary_tree/red_black_tree.py index 526f5ec27987..908f13cd581e 100644 --- a/data_structures/binary_tree/red_black_tree.py +++ b/data_structures/binary_tree/red_black_tree.py @@ -700,7 +700,6 @@ def main(): print_results("Tree traversal", test_tree_chaining()) - print("Testing tree balancing...") print("This should only be a few seconds.") test_insertion_speed() diff --git a/data_structures/binary_tree/segment_tree.py b/data_structures/binary_tree/segment_tree.py index da3d15f26b6a..ad9476b4514b 100644 --- a/data_structures/binary_tree/segment_tree.py +++ b/data_structures/binary_tree/segment_tree.py @@ -1,10 +1,12 @@ import math -class SegmentTree: +class SegmentTree: def __init__(self, A): self.N = len(A) - self.st = [0] * (4 * self.N) # approximate the overall size of segment tree with array N + self.st = [0] * ( + 4 * self.N + ) # approximate the overall size of segment tree with array N self.build(1, 0, self.N - 1) def left(self, idx): @@ -20,51 +22,55 @@ def build(self, idx, l, r): mid = (l + r) // 2 self.build(self.left(idx), l, mid) self.build(self.right(idx), mid + 1, r) - self.st[idx] = max(self.st[self.left(idx)] , self.st[self.right(idx)]) + self.st[idx] = max(self.st[self.left(idx)], self.st[self.right(idx)]) def update(self, a, b, val): return self.update_recursive(1, 0, self.N - 1, a - 1, b - 1, val) - def update_recursive(self, idx, l, r, a, b, val): # update(1, 1, N, a, b, v) for update val v to [a,b] + def update_recursive( + self, idx, l, r, a, b, val + ): # update(1, 1, N, a, b, v) for update val v to [a,b] if r < a or l > b: return True - if l == r : + if l == r: self.st[idx] = val return True - mid = (l+r)//2 + mid = (l + r) // 2 self.update_recursive(self.left(idx), l, mid, a, b, val) - self.update_recursive(self.right(idx), mid+1, r, a, b, val) - self.st[idx] = max(self.st[self.left(idx)] , self.st[self.right(idx)]) + self.update_recursive(self.right(idx), mid + 1, r, a, b, val) + self.st[idx] = max(self.st[self.left(idx)], self.st[self.right(idx)]) return True def query(self, a, b): return self.query_recursive(1, 0, self.N - 1, a - 1, b - 1) - def query_recursive(self, idx, l, r, a, b): #query(1, 1, N, a, b) for query max of [a,b] + def query_recursive( + self, idx, l, r, a, b + ): # query(1, 1, N, a, b) for query max of [a,b] if r < a or l > b: return -math.inf if l >= a and r <= b: return self.st[idx] - mid = (l+r)//2 + mid = (l + r) // 2 q1 = self.query_recursive(self.left(idx), l, mid, a, b) q2 = self.query_recursive(self.right(idx), mid + 1, r, a, b) return max(q1, q2) def showData(self): showList = [] - for i in range(1,N+1): + for i in range(1, N + 1): showList += [self.query(i, i)] print(showList) -if __name__ == '__main__': - A = [1,2,-4,7,3,-5,6,11,-20,9,14,15,5,2,-8] +if __name__ == "__main__": + A = [1, 2, -4, 7, 3, -5, 6, 11, -20, 9, 14, 15, 5, 2, -8] N = 15 segt = SegmentTree(A) print(segt.query(4, 6)) print(segt.query(7, 11)) print(segt.query(7, 12)) - segt.update(1,3,111) + segt.update(1, 3, 111) print(segt.query(1, 15)) - segt.update(7,8,235) + segt.update(7, 8, 235) segt.showData() diff --git a/data_structures/binary_tree/treap.py b/data_structures/binary_tree/treap.py index 0399ff67030a..5d34abc3c931 100644 --- a/data_structures/binary_tree/treap.py +++ b/data_structures/binary_tree/treap.py @@ -7,6 +7,7 @@ class Node: Treap's node Treap is a binary tree by key and heap by priority """ + def __init__(self, key: int): self.key = key self.prior = random() diff --git a/data_structures/hashing/double_hash.py b/data_structures/hashing/double_hash.py index 7a0ce0b3a67b..6c3699cc9950 100644 --- a/data_structures/hashing/double_hash.py +++ b/data_structures/hashing/double_hash.py @@ -8,13 +8,17 @@ class DoubleHash(HashTable): """ Hash Table example with open addressing and Double Hash """ + def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) def __hash_function_2(self, value, data): - next_prime_gt = next_prime(value % self.size_table) \ - if not check_prime(value % self.size_table) else value % self.size_table #gt = bigger than + next_prime_gt = ( + next_prime(value % self.size_table) + if not check_prime(value % self.size_table) + else value % self.size_table + ) # gt = bigger than return next_prime_gt - (data % next_prime_gt) def __hash_double_function(self, key, data, increment): @@ -25,9 +29,14 @@ def _colision_resolution(self, key, data=None): new_key = self.hash_function(data) while self.values[new_key] is not None and self.values[new_key] != key: - new_key = self.__hash_double_function(key, data, i) if \ - self.balanced_factor() >= self.lim_charge else None - if new_key is None: break - else: i += 1 + new_key = ( + self.__hash_double_function(key, data, i) + if self.balanced_factor() >= self.lim_charge + else None + ) + if new_key is None: + break + else: + i += 1 return new_key diff --git a/data_structures/hashing/hash_table.py b/data_structures/hashing/hash_table.py index f0de128d1ad1..ab473dc52324 100644 --- a/data_structures/hashing/hash_table.py +++ b/data_structures/hashing/hash_table.py @@ -19,8 +19,9 @@ def keys(self): return self._keys def balanced_factor(self): - return sum([1 for slot in self.values - if slot is not None]) / (self.size_table * self.charge_factor) + return sum([1 for slot in self.values if slot is not None]) / ( + self.size_table * self.charge_factor + ) def hash_function(self, key): return key % self.size_table @@ -46,8 +47,7 @@ def _set_value(self, key, data): def _colision_resolution(self, key, data=None): new_key = self.hash_function(key + 1) - while self.values[new_key] is not None \ - and self.values[new_key] != key: + while self.values[new_key] is not None and self.values[new_key] != key: if self.values.count(None) > 0: new_key = self.hash_function(new_key + 1) @@ -61,7 +61,7 @@ def rehashing(self): survivor_values = [value for value in self.values if value is not None] self.size_table = next_prime(self.size_table, factor=2) self._keys.clear() - self.values = [None] * self.size_table #hell's pointers D: don't DRY ;/ + self.values = [None] * self.size_table # hell's pointers D: don't DRY ;/ map(self.insert_data, survivor_values) def insert_data(self, data): @@ -80,5 +80,3 @@ def insert_data(self, data): else: self.rehashing() self.insert_data(data) - - diff --git a/data_structures/hashing/hash_table_with_linked_list.py b/data_structures/hashing/hash_table_with_linked_list.py index a45876df49bd..236985b69ac6 100644 --- a/data_structures/hashing/hash_table_with_linked_list.py +++ b/data_structures/hashing/hash_table_with_linked_list.py @@ -7,18 +7,20 @@ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) def _set_value(self, key, data): - self.values[key] = deque([]) if self.values[key] is None else self.values[key] + self.values[key] = deque([]) if self.values[key] is None else self.values[key] self.values[key].appendleft(data) self._keys[key] = self.values[key] def balanced_factor(self): - return sum([self.charge_factor - len(slot) for slot in self.values])\ - / self.size_table * self.charge_factor - + return ( + sum([self.charge_factor - len(slot) for slot in self.values]) + / self.size_table + * self.charge_factor + ) + def _colision_resolution(self, key, data=None): - if not (len(self.values[key]) == self.charge_factor - and self.values.count(None) == 0): + if not ( + len(self.values[key]) == self.charge_factor and self.values.count(None) == 0 + ): return key return super()._colision_resolution(key, data) - - diff --git a/data_structures/hashing/number_theory/prime_numbers.py b/data_structures/hashing/number_theory/prime_numbers.py index 8a521bc45758..2a966e0da7f2 100644 --- a/data_structures/hashing/number_theory/prime_numbers.py +++ b/data_structures/hashing/number_theory/prime_numbers.py @@ -5,25 +5,25 @@ def check_prime(number): - """ + """ it's not the best solution """ - special_non_primes = [0,1,2] - if number in special_non_primes[:2]: - return 2 - elif number == special_non_primes[-1]: - return 3 - - return all([number % i for i in range(2, number)]) + special_non_primes = [0, 1, 2] + if number in special_non_primes[:2]: + return 2 + elif number == special_non_primes[-1]: + return 3 + + return all([number % i for i in range(2, number)]) def next_prime(value, factor=1, **kwargs): value = factor * value first_value_val = value - + while not check_prime(value): value += 1 if not ("desc" in kwargs.keys() and kwargs["desc"] is True) else -1 - + if value == first_value_val: return next_prime(value + 1, **kwargs) return value diff --git a/data_structures/hashing/quadratic_probing.py b/data_structures/hashing/quadratic_probing.py index 1e61100a81fa..ac966e1cd67e 100644 --- a/data_structures/hashing/quadratic_probing.py +++ b/data_structures/hashing/quadratic_probing.py @@ -7,18 +7,21 @@ class QuadraticProbing(HashTable): """ Basic Hash Table example with open addressing using Quadratic Probing """ + def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) def _colision_resolution(self, key, data=None): i = 1 - new_key = self.hash_function(key + i*i) + new_key = self.hash_function(key + i * i) - while self.values[new_key] is not None \ - and self.values[new_key] != key: + while self.values[new_key] is not None and self.values[new_key] != key: i += 1 - new_key = self.hash_function(key + i*i) if not \ - self.balanced_factor() >= self.lim_charge else None + new_key = ( + self.hash_function(key + i * i) + if not self.balanced_factor() >= self.lim_charge + else None + ) if new_key is None: break diff --git a/data_structures/heap/binomial_heap.py b/data_structures/heap/binomial_heap.py index 0154390d7707..e1a005487e34 100644 --- a/data_structures/heap/binomial_heap.py +++ b/data_structures/heap/binomial_heap.py @@ -26,9 +26,7 @@ def mergeTrees(self, other): In-place merge of two binomial trees of equal size. Returns the root of the resulting tree """ - assert ( - self.left_tree_size == other.left_tree_size - ), "Unequal Sizes of Blocks" + assert self.left_tree_size == other.left_tree_size, "Unequal Sizes of Blocks" if self.val < other.val: other.left = self.right @@ -36,9 +34,7 @@ def mergeTrees(self, other): if self.right: self.right.parent = other self.right = other - self.left_tree_size = ( - self.left_tree_size * 2 + 1 - ) + self.left_tree_size = self.left_tree_size * 2 + 1 return self else: self.left = other.right @@ -46,9 +42,7 @@ def mergeTrees(self, other): if other.right: other.right.parent = self other.right = self - other.left_tree_size = ( - other.left_tree_size * 2 + 1 - ) + other.left_tree_size = other.left_tree_size * 2 + 1 return other @@ -132,9 +126,7 @@ class BinomialHeap: """ - def __init__( - self, bottom_root=None, min_node=None, heap_size=0 - ): + def __init__(self, bottom_root=None, min_node=None, heap_size=0): self.size = heap_size self.bottom_root = bottom_root self.min_node = min_node @@ -165,10 +157,7 @@ def mergeHeaps(self, other): combined_roots_list = [] i, j = self.bottom_root, other.bottom_root while i or j: - if i and ( - (not j) - or i.left_tree_size < j.left_tree_size - ): + if i and ((not j) or i.left_tree_size < j.left_tree_size): combined_roots_list.append((i, True)) i = i.parent else: @@ -176,29 +165,17 @@ def mergeHeaps(self, other): j = j.parent # Insert links between them for i in range(len(combined_roots_list) - 1): - if ( - combined_roots_list[i][1] - != combined_roots_list[i + 1][1] - ): - combined_roots_list[i][ - 0 - ].parent = combined_roots_list[i + 1][0] - combined_roots_list[i + 1][ - 0 - ].left = combined_roots_list[i][0] + if combined_roots_list[i][1] != combined_roots_list[i + 1][1]: + combined_roots_list[i][0].parent = combined_roots_list[i + 1][0] + combined_roots_list[i + 1][0].left = combined_roots_list[i][0] # Consecutively merge roots with same left_tree_size i = combined_roots_list[0][0] while i.parent: if ( - ( - i.left_tree_size - == i.parent.left_tree_size - ) - and (not i.parent.parent) + (i.left_tree_size == i.parent.left_tree_size) and (not i.parent.parent) ) or ( i.left_tree_size == i.parent.left_tree_size - and i.left_tree_size - != i.parent.parent.left_tree_size + and i.left_tree_size != i.parent.parent.left_tree_size ): # Neighbouring Nodes @@ -264,9 +241,7 @@ def insert(self, val): next_node = self.bottom_root.parent.parent # Merge - self.bottom_root = self.bottom_root.mergeTrees( - self.bottom_root.parent - ) + self.bottom_root = self.bottom_root.mergeTrees(self.bottom_root.parent) # Update Links self.bottom_root.parent = next_node @@ -337,9 +312,7 @@ def deleteMin(self): if bottom_of_new.val < min_of_new.val: min_of_new = bottom_of_new # Corner case of single root on top left path - if (not self.min_node.left) and ( - not self.min_node.parent - ): + if (not self.min_node.left) and (not self.min_node.parent): self.size = size_of_new self.bottom_root = bottom_of_new self.min_node = min_of_new @@ -348,9 +321,7 @@ def deleteMin(self): # Remaining cases # Construct heap of right subtree newHeap = BinomialHeap( - bottom_root=bottom_of_new, - min_node=min_of_new, - heap_size=size_of_new, + bottom_root=bottom_of_new, min_node=min_of_new, heap_size=size_of_new ) # Update size @@ -411,12 +382,8 @@ def __traversal(self, curr_node, preorder, level=0): """ if curr_node: preorder.append((curr_node.val, level)) - self.__traversal( - curr_node.left, preorder, level + 1 - ) - self.__traversal( - curr_node.right, preorder, level + 1 - ) + self.__traversal(curr_node.left, preorder, level + 1) + self.__traversal(curr_node.right, preorder, level + 1) else: preorder.append(("#", level)) @@ -429,10 +396,7 @@ def __str__(self): return "" preorder_heap = self.preOrder() - return "\n".join( - ("-" * level + str(value)) - for value, level in preorder_heap - ) + return "\n".join(("-" * level + str(value)) for value, level in preorder_heap) # Unit Tests diff --git a/data_structures/heap/heap.py b/data_structures/heap/heap.py index 2373d71bb897..b020ab067cc8 100644 --- a/data_structures/heap/heap.py +++ b/data_structures/heap/heap.py @@ -2,83 +2,85 @@ # This heap class start from here. class Heap: - def __init__(self): # Default constructor of heap class. - self.h = [] - self.currsize = 0 + def __init__(self): # Default constructor of heap class. + self.h = [] + self.currsize = 0 - def leftChild(self,i): - if 2*i+1 < self.currsize: - return 2*i+1 - return None + def leftChild(self, i): + if 2 * i + 1 < self.currsize: + return 2 * i + 1 + return None - def rightChild(self,i): - if 2*i+2 < self.currsize: - return 2*i+2 - return None + def rightChild(self, i): + if 2 * i + 2 < self.currsize: + return 2 * i + 2 + return None - def maxHeapify(self,node): - if node < self.currsize: - m = node - lc = self.leftChild(node) - rc = self.rightChild(node) - if lc is not None and self.h[lc] > self.h[m]: - m = lc - if rc is not None and self.h[rc] > self.h[m]: - m = rc - if m!=node: - temp = self.h[node] - self.h[node] = self.h[m] - self.h[m] = temp - self.maxHeapify(m) + def maxHeapify(self, node): + if node < self.currsize: + m = node + lc = self.leftChild(node) + rc = self.rightChild(node) + if lc is not None and self.h[lc] > self.h[m]: + m = lc + if rc is not None and self.h[rc] > self.h[m]: + m = rc + if m != node: + temp = self.h[node] + self.h[node] = self.h[m] + self.h[m] = temp + self.maxHeapify(m) - def buildHeap(self,a): #This function is used to build the heap from the data container 'a'. - self.currsize = len(a) - self.h = list(a) - for i in range(self.currsize//2,-1,-1): - self.maxHeapify(i) + def buildHeap( + self, a + ): # This function is used to build the heap from the data container 'a'. + self.currsize = len(a) + self.h = list(a) + for i in range(self.currsize // 2, -1, -1): + self.maxHeapify(i) - def getMax(self): #This function is used to get maximum value from the heap. - if self.currsize >= 1: - me = self.h[0] - temp = self.h[0] - self.h[0] = self.h[self.currsize-1] - self.h[self.currsize-1] = temp - self.currsize -= 1 - self.maxHeapify(0) - return me - return None + def getMax(self): # This function is used to get maximum value from the heap. + if self.currsize >= 1: + me = self.h[0] + temp = self.h[0] + self.h[0] = self.h[self.currsize - 1] + self.h[self.currsize - 1] = temp + self.currsize -= 1 + self.maxHeapify(0) + return me + return None - def heapSort(self): #This function is used to sort the heap. - size = self.currsize - while self.currsize-1 >= 0: - temp = self.h[0] - self.h[0] = self.h[self.currsize-1] - self.h[self.currsize-1] = temp - self.currsize -= 1 - self.maxHeapify(0) - self.currsize = size + def heapSort(self): # This function is used to sort the heap. + size = self.currsize + while self.currsize - 1 >= 0: + temp = self.h[0] + self.h[0] = self.h[self.currsize - 1] + self.h[self.currsize - 1] = temp + self.currsize -= 1 + self.maxHeapify(0) + self.currsize = size - def insert(self,data): #This function is used to insert data in the heap. - self.h.append(data) - curr = self.currsize - self.currsize+=1 - while self.h[curr] > self.h[curr/2]: - temp = self.h[curr/2] - self.h[curr/2] = self.h[curr] - self.h[curr] = temp - curr = curr/2 + def insert(self, data): # This function is used to insert data in the heap. + self.h.append(data) + curr = self.currsize + self.currsize += 1 + while self.h[curr] > self.h[curr / 2]: + temp = self.h[curr / 2] + self.h[curr / 2] = self.h[curr] + self.h[curr] = temp + curr = curr / 2 - def display(self): #This function is used to print the heap. - print(self.h) + def display(self): # This function is used to print the heap. + print(self.h) -def main(): - l = list(map(int, input().split())) - h = Heap() - h.buildHeap(l) - h.heapSort() - h.display() -if __name__=='__main__': - main() +def main(): + l = list(map(int, input().split())) + h = Heap() + h.buildHeap(l) + h.heapSort() + h.display() +if __name__ == "__main__": + main() diff --git a/data_structures/linked_list/__init__.py b/data_structures/linked_list/__init__.py index 6d50f23c1f1a..a050adba42b2 100644 --- a/data_structures/linked_list/__init__.py +++ b/data_structures/linked_list/__init__.py @@ -3,6 +3,7 @@ def __init__(self, item, next): self.item = item self.next = next + class LinkedList: def __init__(self): self.head = None diff --git a/data_structures/linked_list/doubly_linked_list.py b/data_structures/linked_list/doubly_linked_list.py index 23d91383fa0e..38fff867b416 100644 --- a/data_structures/linked_list/doubly_linked_list.py +++ b/data_structures/linked_list/doubly_linked_list.py @@ -1,76 +1,81 @@ -''' +""" - A linked list is similar to an array, it holds values. However, links in a linked list do not have indexes. - This is an example of a double ended, doubly linked list. - Each link references the next link and the previous one. - A Doubly Linked List (DLL) contains an extra pointer, typically called previous pointer, together with next pointer and data which are there in singly linked list. - - Advantages over SLL - IT can be traversed in both forward and backward direction.,Delete operation is more efficent''' + - Advantages over SLL - IT can be traversed in both forward and backward direction.,Delete operation is more efficent""" -class LinkedList: #making main class named linked list +class LinkedList: # making main class named linked list def __init__(self): self.head = None self.tail = None def insertHead(self, x): - newLink = Link(x) #Create a new link with a value attached to it - if(self.isEmpty() == True): #Set the first element added to be the tail + newLink = Link(x) # Create a new link with a value attached to it + if self.isEmpty() == True: # Set the first element added to be the tail self.tail = newLink else: - self.head.previous = newLink # newLink <-- currenthead(head) - newLink.next = self.head # newLink <--> currenthead(head) - self.head = newLink # newLink(head) <--> oldhead + self.head.previous = newLink # newLink <-- currenthead(head) + newLink.next = self.head # newLink <--> currenthead(head) + self.head = newLink # newLink(head) <--> oldhead def deleteHead(self): temp = self.head - self.head = self.head.next # oldHead <--> 2ndElement(head) - self.head.previous = None # oldHead --> 2ndElement(head) nothing pointing at it so the old head will be removed - if(self.head is None): - self.tail = None #if empty linked list + self.head = self.head.next # oldHead <--> 2ndElement(head) + self.head.previous = ( + None + ) # oldHead --> 2ndElement(head) nothing pointing at it so the old head will be removed + if self.head is None: + self.tail = None # if empty linked list return temp def insertTail(self, x): newLink = Link(x) - newLink.next = None # currentTail(tail) newLink --> - self.tail.next = newLink # currentTail(tail) --> newLink --> - newLink.previous = self.tail #currentTail(tail) <--> newLink --> - self.tail = newLink # oldTail <--> newLink(tail) --> + newLink.next = None # currentTail(tail) newLink --> + self.tail.next = newLink # currentTail(tail) --> newLink --> + newLink.previous = self.tail # currentTail(tail) <--> newLink --> + self.tail = newLink # oldTail <--> newLink(tail) --> def deleteTail(self): temp = self.tail - self.tail = self.tail.previous # 2ndLast(tail) <--> oldTail --> None - self.tail.next = None # 2ndlast(tail) --> None + self.tail = self.tail.previous # 2ndLast(tail) <--> oldTail --> None + self.tail.next = None # 2ndlast(tail) --> None return temp def delete(self, x): current = self.head - while(current.value != x): # Find the position to delete + while current.value != x: # Find the position to delete current = current.next - if(current == self.head): + if current == self.head: self.deleteHead() - elif(current == self.tail): + elif current == self.tail: self.deleteTail() - else: #Before: 1 <--> 2(current) <--> 3 - current.previous.next = current.next # 1 --> 3 - current.next.previous = current.previous # 1 <--> 3 + else: # Before: 1 <--> 2(current) <--> 3 + current.previous.next = current.next # 1 --> 3 + current.next.previous = current.previous # 1 <--> 3 - def isEmpty(self): #Will return True if the list is empty - return(self.head is None) + def isEmpty(self): # Will return True if the list is empty + return self.head is None - def display(self): #Prints contents of the list + def display(self): # Prints contents of the list current = self.head - while(current != None): + while current != None: current.displayLink() current = current.next print() + class Link: - next = None #This points to the link in front of the new link - previous = None #This points to the link behind the new link + next = None # This points to the link in front of the new link + previous = None # This points to the link behind the new link + def __init__(self, x): self.value = x + def displayLink(self): print("{}".format(self.value), end=" ") diff --git a/data_structures/linked_list/singly_linked_list.py b/data_structures/linked_list/singly_linked_list.py index 5943b88d5964..16436ff90274 100644 --- a/data_structures/linked_list/singly_linked_list.py +++ b/data_structures/linked_list/singly_linked_list.py @@ -6,21 +6,22 @@ def __init__(self, data): class Linked_List: def __init__(self): - self.Head = None # Initialize Head to None + self.Head = None # Initialize Head to None def insert_tail(self, data): - if(self.Head is None): self.insert_head(data) #If this is first node, call insert_head + if self.Head is None: + self.insert_head(data) # If this is first node, call insert_head else: temp = self.Head - while(temp.next != None): #traverse to last node + while temp.next != None: # traverse to last node temp = temp.next - temp.next = Node(data) #create node & link to tail + temp.next = Node(data) # create node & link to tail def insert_head(self, data): - newNod = Node(data) # create a new node + newNod = Node(data) # create a new node if self.Head != None: - newNod.next = self.Head # link newNode to head - self.Head = newNod # make NewNode as Head + newNod.next = self.Head # link newNode to head + self.Head = newNod # make NewNode as Head def printList(self): # print every node data tamp = self.Head @@ -38,12 +39,15 @@ def delete_head(self): # delete from head def delete_tail(self): # delete from tail tamp = self.Head if self.Head != None: - if(self.Head.next is None): # if Head is the only Node in the Linked List + if self.Head.next is None: # if Head is the only Node in the Linked List self.Head = None else: while tamp.next.next is not None: # find the 2nd last element tamp = tamp.next - tamp.next, tamp = None, tamp.next #(2nd last element).next = None and tamp = last element + tamp.next, tamp = ( + None, + tamp.next, + ) # (2nd last element).next = None and tamp = last element return tamp def isEmpty(self): @@ -65,21 +69,22 @@ def reverse(self): # Return prev in order to put the head at the end self.Head = prev + def main(): A = Linked_List() print("Inserting 1st at Head") - a1=input() + a1 = input() A.insert_head(a1) print("Inserting 2nd at Head") - a2=input() + a2 = input() A.insert_head(a2) print("\nPrint List : ") A.printList() print("\nInserting 1st at Tail") - a3=input() + a3 = input() A.insert_tail(a3) print("Inserting 2nd at Tail") - a4=input() + a4 = input() A.insert_tail(a4) print("\nPrint List : ") A.printList() @@ -94,5 +99,6 @@ def main(): print("\nPrint List : ") A.printList() -if __name__ == '__main__': - main() + +if __name__ == "__main__": + main() diff --git a/data_structures/linked_list/swap_nodes.py b/data_structures/linked_list/swap_nodes.py index ce2543bc46d8..a6a50091e3e0 100644 --- a/data_structures/linked_list/swap_nodes.py +++ b/data_structures/linked_list/swap_nodes.py @@ -1,6 +1,6 @@ class Node: def __init__(self, data): - self.data = data; + self.data = data self.next = None @@ -14,13 +14,13 @@ def print_list(self): print(temp.data) temp = temp.next -# adding nodes + # adding nodes def push(self, new_data): new_node = Node(new_data) new_node.next = self.head self.head = new_node -# swapping nodes + # swapping nodes def swapNodes(self, d1, d2): prevD1 = None prevD2 = None @@ -53,11 +53,11 @@ def swapNodes(self, d1, d2): D1.next = D2.next D2.next = temp -# swapping code ends here +# swapping code ends here -if __name__ == '__main__': +if __name__ == "__main__": list = Linkedlist() list.push(5) list.push(4) @@ -70,6 +70,3 @@ def swapNodes(self, d1, d2): list.swapNodes(1, 4) print("After swapping") list.print_list() - - - diff --git a/data_structures/queue/double_ended_queue.py b/data_structures/queue/double_ended_queue.py index a3cfa7230710..dd003b7c98ac 100644 --- a/data_structures/queue/double_ended_queue.py +++ b/data_structures/queue/double_ended_queue.py @@ -5,11 +5,11 @@ import collections # initializing deque -de = collections.deque([1, 2, 3,]) +de = collections.deque([1, 2, 3]) # using extend() to add numbers to right end # adds 4,5,6 to right end -de.extend([4,5,6]) +de.extend([4, 5, 6]) # printing modified deque print("The deque after extending deque at end is : ") @@ -17,7 +17,7 @@ # using extendleft() to add numbers to left end # adds 7,8,9 to right end -de.extendleft([7,8,9]) +de.extendleft([7, 8, 9]) # printing modified deque print("The deque after extending deque at beginning is : ") diff --git a/data_structures/queue/queue_on_list.py b/data_structures/queue/queue_on_list.py index 2ec9bac8398a..bb44e08ad6c5 100644 --- a/data_structures/queue/queue_on_list.py +++ b/data_structures/queue/queue_on_list.py @@ -1,46 +1,52 @@ """Queue represented by a python list""" -class Queue(): + + +class Queue: def __init__(self): self.entries = [] self.length = 0 - self.front=0 + self.front = 0 def __str__(self): - printed = '<' + str(self.entries)[1:-1] + '>' + printed = "<" + str(self.entries)[1:-1] + ">" return printed """Enqueues {@code item} @param item item to enqueue""" + def put(self, item): self.entries.append(item) self.length = self.length + 1 - """Dequeues {@code item} @requirement: |self.length| > 0 @return dequeued item that was dequeued""" + def get(self): self.length = self.length - 1 dequeued = self.entries[self.front] - #self.front-=1 - #self.entries = self.entries[self.front:] + # self.front-=1 + # self.entries = self.entries[self.front:] self.entries = self.entries[1:] return dequeued """Rotates the queue {@code rotation} times @param rotation number of times to rotate queue""" + def rotate(self, rotation): for i in range(rotation): self.put(self.get()) """Enqueues {@code item} @return item at front of self.entries""" + def front(self): return self.entries[0] """Returns the length of this.entries""" + def size(self): return self.length diff --git a/data_structures/queue/queue_on_pseudo_stack.py b/data_structures/queue/queue_on_pseudo_stack.py index b69fbcc988f7..7fa2fb2566af 100644 --- a/data_structures/queue/queue_on_pseudo_stack.py +++ b/data_structures/queue/queue_on_pseudo_stack.py @@ -1,16 +1,19 @@ """Queue represented by a pseudo stack (represented by a list with pop and append)""" -class Queue(): + + +class Queue: def __init__(self): self.stack = [] self.length = 0 def __str__(self): - printed = '<' + str(self.stack)[1:-1] + '>' + printed = "<" + str(self.stack)[1:-1] + ">" return printed """Enqueues {@code item} @param item item to enqueue""" + def put(self, item): self.stack.append(item) self.length = self.length + 1 @@ -19,17 +22,19 @@ def put(self, item): @requirement: |self.length| > 0 @return dequeued item that was dequeued""" + def get(self): self.rotate(1) - dequeued = self.stack[self.length-1] + dequeued = self.stack[self.length - 1] self.stack = self.stack[:-1] - self.rotate(self.length-1) - self.length = self.length -1 + self.rotate(self.length - 1) + self.length = self.length - 1 return dequeued """Rotates the queue {@code rotation} times @param rotation number of times to rotate queue""" + def rotate(self, rotation): for i in range(rotation): temp = self.stack[0] @@ -39,12 +44,14 @@ def rotate(self, rotation): """Reports item at the front of self @return item at front of self.stack""" + def front(self): front = self.get() self.put(front) - self.rotate(self.length-1) + self.rotate(self.length - 1) return front """Returns the length of this.stack""" + def size(self): return self.length diff --git a/data_structures/stacks/__init__.py b/data_structures/stacks/__init__.py index f7e92ae2d269..f6995cf98977 100644 --- a/data_structures/stacks/__init__.py +++ b/data_structures/stacks/__init__.py @@ -1,23 +1,22 @@ class Stack: + def __init__(self): + self.stack = [] + self.top = 0 - def __init__(self): - self.stack = [] - self.top = 0 + def is_empty(self): + return self.top == 0 - def is_empty(self): - return (self.top == 0) + def push(self, item): + if self.top < len(self.stack): + self.stack[self.top] = item + else: + self.stack.append(item) - def push(self, item): - if self.top < len(self.stack): - self.stack[self.top] = item - else: - self.stack.append(item) + self.top += 1 - self.top += 1 - - def pop(self): - if self.is_empty(): - return None - else: - self.top -= 1 - return self.stack[self.top] + def pop(self): + if self.is_empty(): + return None + else: + self.top -= 1 + return self.stack[self.top] diff --git a/data_structures/stacks/balanced_parentheses.py b/data_structures/stacks/balanced_parentheses.py index 3f43ccbf5760..7aacd5969277 100644 --- a/data_structures/stacks/balanced_parentheses.py +++ b/data_structures/stacks/balanced_parentheses.py @@ -1,23 +1,23 @@ from .stack import Stack -__author__ = 'Omkar Pathak' +__author__ = "Omkar Pathak" def balanced_parentheses(parentheses): """ Use a stack to check if a string of parentheses is balanced.""" stack = Stack(len(parentheses)) for parenthesis in parentheses: - if parenthesis == '(': + if parenthesis == "(": stack.push(parenthesis) - elif parenthesis == ')': + elif parenthesis == ")": if stack.is_empty(): return False stack.pop() return stack.is_empty() -if __name__ == '__main__': - examples = ['((()))', '((())', '(()))'] - print('Balanced parentheses demonstration:\n') +if __name__ == "__main__": + examples = ["((()))", "((())", "(()))"] + print("Balanced parentheses demonstration:\n") for example in examples: - print(example + ': ' + str(balanced_parentheses(example))) + print(example + ": " + str(balanced_parentheses(example))) diff --git a/data_structures/stacks/infix_to_postfix_conversion.py b/data_structures/stacks/infix_to_postfix_conversion.py index 84a5d1480a24..61114402377a 100644 --- a/data_structures/stacks/infix_to_postfix_conversion.py +++ b/data_structures/stacks/infix_to_postfix_conversion.py @@ -2,7 +2,7 @@ from .stack import Stack -__author__ = 'Omkar Pathak' +__author__ = "Omkar Pathak" def is_operand(char): @@ -15,9 +15,7 @@ def precedence(char): https://en.wikipedia.org/wiki/Order_of_operations """ - dictionary = {'+': 1, '-': 1, - '*': 2, '/': 2, - '^': 3} + dictionary = {"+": 1, "-": 1, "*": 2, "/": 2, "^": 3} return dictionary.get(char, -1) @@ -34,29 +32,28 @@ def infix_to_postfix(expression): for char in expression: if is_operand(char): postfix.append(char) - elif char not in {'(', ')'}: - while (not stack.is_empty() - and precedence(char) <= precedence(stack.peek())): + elif char not in {"(", ")"}: + while not stack.is_empty() and precedence(char) <= precedence(stack.peek()): postfix.append(stack.pop()) stack.push(char) - elif char == '(': + elif char == "(": stack.push(char) - elif char == ')': - while not stack.is_empty() and stack.peek() != '(': + elif char == ")": + while not stack.is_empty() and stack.peek() != "(": postfix.append(stack.pop()) # Pop '(' from stack. If there is no '(', there is a mismatched # parentheses. - if stack.peek() != '(': - raise ValueError('Mismatched parentheses') + if stack.peek() != "(": + raise ValueError("Mismatched parentheses") stack.pop() while not stack.is_empty(): postfix.append(stack.pop()) - return ' '.join(postfix) + return " ".join(postfix) -if __name__ == '__main__': - expression = 'a+b*(c^d-e)^(f+g*h)-i' +if __name__ == "__main__": + expression = "a+b*(c^d-e)^(f+g*h)-i" - print('Infix to Postfix Notation demonstration:\n') - print('Infix notation: ' + expression) - print('Postfix notation: ' + infix_to_postfix(expression)) + print("Infix to Postfix Notation demonstration:\n") + print("Infix notation: " + expression) + print("Postfix notation: " + infix_to_postfix(expression)) diff --git a/data_structures/stacks/infix_to_prefix_conversion.py b/data_structures/stacks/infix_to_prefix_conversion.py index da5fc261fb9f..4f0e1ab8adfa 100644 --- a/data_structures/stacks/infix_to_prefix_conversion.py +++ b/data_structures/stacks/infix_to_prefix_conversion.py @@ -14,48 +14,82 @@ a+b^c (Infix) -> +a^bc (Prefix) """ + def infix_2_postfix(Infix): Stack = [] Postfix = [] - priority = {'^':3, '*':2, '/':2, '%':2, '+':1, '-':1} # Priority of each operator - print_width = len(Infix) if(len(Infix)>7) else 7 + priority = { + "^": 3, + "*": 2, + "/": 2, + "%": 2, + "+": 1, + "-": 1, + } # Priority of each operator + print_width = len(Infix) if (len(Infix) > 7) else 7 # Print table header for output - print('Symbol'.center(8), 'Stack'.center(print_width), 'Postfix'.center(print_width), sep = " | ") - print('-'*(print_width*3+7)) + print( + "Symbol".center(8), + "Stack".center(print_width), + "Postfix".center(print_width), + sep=" | ", + ) + print("-" * (print_width * 3 + 7)) for x in Infix: - if(x.isalpha() or x.isdigit()): Postfix.append(x) # if x is Alphabet / Digit, add it to Postfix - elif(x == '('): Stack.append(x) # if x is "(" push to Stack - elif(x == ')'): # if x is ")" pop stack until "(" is encountered - while(Stack[-1] != '('): - Postfix.append( Stack.pop() ) #Pop stack & add the content to Postfix + if x.isalpha() or x.isdigit(): + Postfix.append(x) # if x is Alphabet / Digit, add it to Postfix + elif x == "(": + Stack.append(x) # if x is "(" push to Stack + elif x == ")": # if x is ")" pop stack until "(" is encountered + while Stack[-1] != "(": + Postfix.append(Stack.pop()) # Pop stack & add the content to Postfix Stack.pop() else: - if(len(Stack)==0): Stack.append(x) #If stack is empty, push x to stack + if len(Stack) == 0: + Stack.append(x) # If stack is empty, push x to stack else: - while( len(Stack) > 0 and priority[x] <= priority[Stack[-1]]): # while priority of x is not greater than priority of element in the stack - Postfix.append( Stack.pop() ) # pop stack & add to Postfix - Stack.append(x) # push x to stack + while ( + len(Stack) > 0 and priority[x] <= priority[Stack[-1]] + ): # while priority of x is not greater than priority of element in the stack + Postfix.append(Stack.pop()) # pop stack & add to Postfix + Stack.append(x) # push x to stack + + print( + x.center(8), + ("".join(Stack)).ljust(print_width), + ("".join(Postfix)).ljust(print_width), + sep=" | ", + ) # Output in tabular format - print(x.center(8), (''.join(Stack)).ljust(print_width), (''.join(Postfix)).ljust(print_width), sep = " | ") # Output in tabular format + while len(Stack) > 0: # while stack is not empty + Postfix.append(Stack.pop()) # pop stack & add to Postfix + print( + " ".center(8), + ("".join(Stack)).ljust(print_width), + ("".join(Postfix)).ljust(print_width), + sep=" | ", + ) # Output in tabular format - while(len(Stack) > 0): # while stack is not empty - Postfix.append( Stack.pop() ) # pop stack & add to Postfix - print(' '.center(8), (''.join(Stack)).ljust(print_width), (''.join(Postfix)).ljust(print_width), sep = " | ") # Output in tabular format + return "".join(Postfix) # return Postfix as str - return "".join(Postfix) # return Postfix as str def infix_2_prefix(Infix): - Infix = list(Infix[::-1]) # reverse the infix equation - + Infix = list(Infix[::-1]) # reverse the infix equation + for i in range(len(Infix)): - if(Infix[i] == '('): Infix[i] = ')' # change "(" to ")" - elif(Infix[i] == ')'): Infix[i] = '(' # change ")" to "(" - - return (infix_2_postfix("".join(Infix)))[::-1] # call infix_2_postfix on Infix, return reverse of Postfix + if Infix[i] == "(": + Infix[i] = ")" # change "(" to ")" + elif Infix[i] == ")": + Infix[i] = "(" # change ")" to "(" + + return (infix_2_postfix("".join(Infix)))[ + ::-1 + ] # call infix_2_postfix on Infix, return reverse of Postfix + if __name__ == "__main__": - Infix = input("\nEnter an Infix Equation = ") #Input an Infix equation - Infix = "".join(Infix.split()) #Remove spaces from the input + Infix = input("\nEnter an Infix Equation = ") # Input an Infix equation + Infix = "".join(Infix.split()) # Remove spaces from the input print("\n\t", Infix, "(Infix) -> ", infix_2_prefix(Infix), "(Prefix)") diff --git a/data_structures/stacks/next_greater_element.py b/data_structures/stacks/next_greater_element.py index 2e67f1764a5a..02a86196f5bf 100644 --- a/data_structures/stacks/next_greater_element.py +++ b/data_structures/stacks/next_greater_element.py @@ -4,13 +4,14 @@ def printNGE(arr): for i in range(0, len(arr), 1): next = -1 - for j in range(i+1, len(arr), 1): + for j in range(i + 1, len(arr), 1): if arr[i] < arr[j]: next = arr[j] break print(str(arr[i]) + " -- " + str(next)) + # Driver program to test above function -arr = [11,13,21,3] +arr = [11, 13, 21, 3] printNGE(arr) diff --git a/data_structures/stacks/postfix_evaluation.py b/data_structures/stacks/postfix_evaluation.py index 1786e71dd383..0f3d5c76d6a3 100644 --- a/data_structures/stacks/postfix_evaluation.py +++ b/data_structures/stacks/postfix_evaluation.py @@ -19,32 +19,52 @@ import operator as op + def Solve(Postfix): Stack = [] - Div = lambda x, y: int(x/y) # integer division operation - Opr = {'^':op.pow, '*':op.mul, '/':Div, '+':op.add, '-':op.sub} # operators & their respective operation + Div = lambda x, y: int(x / y) # integer division operation + Opr = { + "^": op.pow, + "*": op.mul, + "/": Div, + "+": op.add, + "-": op.sub, + } # operators & their respective operation # print table header - print('Symbol'.center(8), 'Action'.center(12), 'Stack', sep = " | ") - print('-'*(30+len(Postfix))) + print("Symbol".center(8), "Action".center(12), "Stack", sep=" | ") + print("-" * (30 + len(Postfix))) for x in Postfix: - if( x.isdigit() ): # if x in digit - Stack.append(x) # append x to stack - print(x.rjust(8), ('push('+x+')').ljust(12), ','.join(Stack), sep = " | ") # output in tabular format + if x.isdigit(): # if x in digit + Stack.append(x) # append x to stack + print( + x.rjust(8), ("push(" + x + ")").ljust(12), ",".join(Stack), sep=" | " + ) # output in tabular format else: - B = Stack.pop() # pop stack - print("".rjust(8), ('pop('+B+')').ljust(12), ','.join(Stack), sep = " | ") # output in tabular format + B = Stack.pop() # pop stack + print( + "".rjust(8), ("pop(" + B + ")").ljust(12), ",".join(Stack), sep=" | " + ) # output in tabular format - A = Stack.pop() # pop stack - print("".rjust(8), ('pop('+A+')').ljust(12), ','.join(Stack), sep = " | ") # output in tabular format + A = Stack.pop() # pop stack + print( + "".rjust(8), ("pop(" + A + ")").ljust(12), ",".join(Stack), sep=" | " + ) # output in tabular format - Stack.append( str(Opr[x](int(A), int(B))) ) # evaluate the 2 values poped from stack & push result to stack - print(x.rjust(8), ('push('+A+x+B+')').ljust(12), ','.join(Stack), sep = " | ") # output in tabular format + Stack.append( + str(Opr[x](int(A), int(B))) + ) # evaluate the 2 values poped from stack & push result to stack + print( + x.rjust(8), + ("push(" + A + x + B + ")").ljust(12), + ",".join(Stack), + sep=" | ", + ) # output in tabular format return int(Stack[0]) if __name__ == "__main__": - Postfix = input("\n\nEnter a Postfix Equation (space separated) = ").split(' ') + Postfix = input("\n\nEnter a Postfix Equation (space separated) = ").split(" ") print("\n\tResult = ", Solve(Postfix)) diff --git a/data_structures/stacks/stack.py b/data_structures/stacks/stack.py index 387367db2fcc..9f5b279710c6 100644 --- a/data_structures/stacks/stack.py +++ b/data_structures/stacks/stack.py @@ -1,4 +1,4 @@ -__author__ = 'Omkar Pathak' +__author__ = "Omkar Pathak" class Stack(object): @@ -32,7 +32,7 @@ def pop(self): if self.stack: return self.stack.pop() else: - raise IndexError('pop from an empty stack') + raise IndexError("pop from an empty stack") def peek(self): """ Peek at the top-most element of the stack.""" @@ -52,17 +52,17 @@ class StackOverflowError(BaseException): pass -if __name__ == '__main__': +if __name__ == "__main__": stack = Stack() for i in range(10): stack.push(i) - print('Stack demonstration:\n') - print('Initial stack: ' + str(stack)) - print('pop(): ' + str(stack.pop())) - print('After pop(), the stack is now: ' + str(stack)) - print('peek(): ' + str(stack.peek())) + print("Stack demonstration:\n") + print("Initial stack: " + str(stack)) + print("pop(): " + str(stack.pop())) + print("After pop(), the stack is now: " + str(stack)) + print("peek(): " + str(stack.peek())) stack.push(100) - print('After push(100), the stack is now: ' + str(stack)) - print('is_empty(): ' + str(stack.is_empty())) - print('size(): ' + str(stack.size())) + print("After push(100), the stack is now: " + str(stack)) + print("is_empty(): " + str(stack.is_empty())) + print("size(): " + str(stack.size())) diff --git a/data_structures/stacks/stock_span_problem.py b/data_structures/stacks/stock_span_problem.py index 47d916fde9ed..45cd6bae1282 100644 --- a/data_structures/stacks/stock_span_problem.py +++ b/data_structures/stacks/stock_span_problem.py @@ -1,11 +1,13 @@ -''' +""" The stock span problem is a financial problem where we have a series of n daily price quotes for a stock and we need to calculate span of stock's price for all n days. The span Si of the stock's price on a given day i is defined as the maximum number of consecutive days just before the given day, for which the price of the stock on the current day is less than or equal to its price on the given day. -''' +""" + + def calculateSpan(price, S): n = len(price) @@ -21,14 +23,14 @@ def calculateSpan(price, S): # Pop elements from stack whlie stack is not # empty and top of stack is smaller than price[i] - while( len(st) > 0 and price[st[0]] <= price[i]): + while len(st) > 0 and price[st[0]] <= price[i]: st.pop() # If stack becomes empty, then price[i] is greater # than all elements on left of it, i.e. price[0], # price[1], ..price[i-1]. Else the price[i] is # greater than elements after top of stack - S[i] = i+1 if len(st) <= 0 else (i - st[0]) + S[i] = i + 1 if len(st) <= 0 else (i - st[0]) # Push this element to stack st.append(i) @@ -36,13 +38,13 @@ def calculateSpan(price, S): # A utility function to print elements of array def printArray(arr, n): - for i in range(0,n): - print(arr[i],end =" ") + for i in range(0, n): + print(arr[i], end=" ") # Driver program to test above function price = [10, 4, 5, 90, 120, 80] -S = [0 for i in range(len(price)+1)] +S = [0 for i in range(len(price) + 1)] # Fill the span values in array S[] calculateSpan(price, S) diff --git a/digital_image_processing/edge_detection/canny.py b/digital_image_processing/edge_detection/canny.py index 7fde75a90a48..6f98fee6308e 100644 --- a/digital_image_processing/edge_detection/canny.py +++ b/digital_image_processing/edge_detection/canny.py @@ -8,8 +8,12 @@ def gen_gaussian_kernel(k_size, sigma): center = k_size // 2 - x, y = np.mgrid[0 - center:k_size - center, 0 - center:k_size - center] - g = 1 / (2 * np.pi * sigma) * np.exp(-(np.square(x) + np.square(y)) / (2 * np.square(sigma))) + x, y = np.mgrid[0 - center : k_size - center, 0 - center : k_size - center] + g = ( + 1 + / (2 * np.pi * sigma) + * np.exp(-(np.square(x) + np.square(y)) / (2 * np.square(sigma))) + ) return g @@ -34,27 +38,33 @@ def canny(image, threshold_low=15, threshold_high=30, weak=128, strong=255): if ( 0 <= direction < 22.5 - or 15 * PI / 8 <= direction <= 2 * PI - or 7 * PI / 8 <= direction <= 9 * PI / 8 + or 15 * PI / 8 <= direction <= 2 * PI + or 7 * PI / 8 <= direction <= 9 * PI / 8 ): W = sobel_grad[row, col - 1] E = sobel_grad[row, col + 1] if sobel_grad[row, col] >= W and sobel_grad[row, col] >= E: dst[row, col] = sobel_grad[row, col] - elif (PI / 8 <= direction < 3 * PI / 8) or (9 * PI / 8 <= direction < 11 * PI / 8): + elif (PI / 8 <= direction < 3 * PI / 8) or ( + 9 * PI / 8 <= direction < 11 * PI / 8 + ): SW = sobel_grad[row + 1, col - 1] NE = sobel_grad[row - 1, col + 1] if sobel_grad[row, col] >= SW and sobel_grad[row, col] >= NE: dst[row, col] = sobel_grad[row, col] - elif (3 * PI / 8 <= direction < 5 * PI / 8) or (11 * PI / 8 <= direction < 13 * PI / 8): + elif (3 * PI / 8 <= direction < 5 * PI / 8) or ( + 11 * PI / 8 <= direction < 13 * PI / 8 + ): N = sobel_grad[row - 1, col] S = sobel_grad[row + 1, col] if sobel_grad[row, col] >= N and sobel_grad[row, col] >= S: dst[row, col] = sobel_grad[row, col] - elif (5 * PI / 8 <= direction < 7 * PI / 8) or (13 * PI / 8 <= direction < 15 * PI / 8): + elif (5 * PI / 8 <= direction < 7 * PI / 8) or ( + 13 * PI / 8 <= direction < 15 * PI / 8 + ): NW = sobel_grad[row - 1, col - 1] SE = sobel_grad[row + 1, col + 1] if sobel_grad[row, col] >= NW and sobel_grad[row, col] >= SE: @@ -82,14 +92,14 @@ def canny(image, threshold_low=15, threshold_high=30, weak=128, strong=255): for col in range(1, image_col): if dst[row, col] == weak: if 255 in ( - dst[row, col + 1], - dst[row, col - 1], - dst[row - 1, col], - dst[row + 1, col], - dst[row - 1, col - 1], - dst[row + 1, col - 1], - dst[row - 1, col + 1], - dst[row + 1, col + 1], + dst[row, col + 1], + dst[row, col - 1], + dst[row - 1, col], + dst[row + 1, col], + dst[row - 1, col - 1], + dst[row + 1, col - 1], + dst[row - 1, col + 1], + dst[row + 1, col + 1], ): dst[row, col] = strong else: @@ -98,10 +108,10 @@ def canny(image, threshold_low=15, threshold_high=30, weak=128, strong=255): return dst -if __name__ == '__main__': +if __name__ == "__main__": # read original image in gray mode - lena = cv2.imread(r'../image_data/lena.jpg', 0) + lena = cv2.imread(r"../image_data/lena.jpg", 0) # canny edge detection canny_dst = canny(lena) - cv2.imshow('canny', canny_dst) + cv2.imshow("canny", canny_dst) cv2.waitKey(0) diff --git a/digital_image_processing/filters/convolve.py b/digital_image_processing/filters/convolve.py index b7600d74c294..ec500d940366 100644 --- a/digital_image_processing/filters/convolve.py +++ b/digital_image_processing/filters/convolve.py @@ -13,7 +13,7 @@ def im2col(image, block_size): row = 0 for i in range(0, dst_height): for j in range(0, dst_width): - window = ravel(image[i:i + block_size[0], j:j + block_size[1]]) + window = ravel(image[i : i + block_size[0], j : j + block_size[1]]) image_array[row, :] = window row += 1 @@ -23,9 +23,9 @@ def im2col(image, block_size): def img_convolve(image, filter_kernel): height, width = image.shape[0], image.shape[1] k_size = filter_kernel.shape[0] - pad_size = k_size//2 + pad_size = k_size // 2 # Pads image with the edge values of array. - image_tmp = pad(image, pad_size, mode='edge') + image_tmp = pad(image, pad_size, mode="edge") # im2col, turn the k_size*k_size pixels into a row and np.vstack all rows image_array = im2col(image_tmp, (k_size, k_size)) @@ -37,13 +37,13 @@ def img_convolve(image, filter_kernel): return dst -if __name__ == '__main__': +if __name__ == "__main__": # read original image - img = imread(r'../image_data/lena.jpg') + img = imread(r"../image_data/lena.jpg") # turn image in gray scale value gray = cvtColor(img, COLOR_BGR2GRAY) # Laplace operator Laplace_kernel = array([[0, 1, 0], [1, -4, 1], [0, 1, 0]]) out = img_convolve(gray, Laplace_kernel).astype(uint8) - imshow('Laplacian', out) + imshow("Laplacian", out) waitKey(0) diff --git a/digital_image_processing/filters/gaussian_filter.py b/digital_image_processing/filters/gaussian_filter.py index ff85ce047220..b800f0a7edc8 100644 --- a/digital_image_processing/filters/gaussian_filter.py +++ b/digital_image_processing/filters/gaussian_filter.py @@ -7,23 +7,23 @@ def gen_gaussian_kernel(k_size, sigma): center = k_size // 2 - x, y = mgrid[0-center:k_size-center, 0-center:k_size-center] - g = 1/(2*pi*sigma) * exp(-(square(x) + square(y))/(2*square(sigma))) + x, y = mgrid[0 - center : k_size - center, 0 - center : k_size - center] + g = 1 / (2 * pi * sigma) * exp(-(square(x) + square(y)) / (2 * square(sigma))) return g def gaussian_filter(image, k_size, sigma): height, width = image.shape[0], image.shape[1] # dst image height and width - dst_height = height-k_size+1 - dst_width = width-k_size+1 + dst_height = height - k_size + 1 + dst_width = width - k_size + 1 # im2col, turn the k_size*k_size pixels into a row and np.vstack all rows - image_array = zeros((dst_height*dst_width, k_size*k_size)) + image_array = zeros((dst_height * dst_width, k_size * k_size)) row = 0 for i in range(0, dst_height): for j in range(0, dst_width): - window = ravel(image[i:i + k_size, j:j + k_size]) + window = ravel(image[i : i + k_size, j : j + k_size]) image_array[row, :] = window row += 1 @@ -37,9 +37,9 @@ def gaussian_filter(image, k_size, sigma): return dst -if __name__ == '__main__': +if __name__ == "__main__": # read original image - img = imread(r'../image_data/lena.jpg') + img = imread(r"../image_data/lena.jpg") # turn image in gray scale value gray = cvtColor(img, COLOR_BGR2GRAY) @@ -48,6 +48,6 @@ def gaussian_filter(image, k_size, sigma): gaussian5x5 = gaussian_filter(gray, 5, sigma=0.8) # show result images - imshow('gaussian filter with 3x3 mask', gaussian3x3) - imshow('gaussian filter with 5x5 mask', gaussian5x5) + imshow("gaussian filter with 3x3 mask", gaussian3x3) + imshow("gaussian filter with 5x5 mask", gaussian5x5) waitKey() diff --git a/digital_image_processing/filters/median_filter.py b/digital_image_processing/filters/median_filter.py index 4b21b96b080b..151ef8a55df1 100644 --- a/digital_image_processing/filters/median_filter.py +++ b/digital_image_processing/filters/median_filter.py @@ -19,16 +19,16 @@ def median_filter(gray_img, mask=3): for i in range(bd, gray_img.shape[0] - bd): for j in range(bd, gray_img.shape[1] - bd): # get mask according with mask - kernel = ravel(gray_img[i - bd:i + bd + 1, j - bd:j + bd + 1]) + kernel = ravel(gray_img[i - bd : i + bd + 1, j - bd : j + bd + 1]) # calculate mask median median = sort(kernel)[int8(divide((multiply(mask, mask)), 2) + 1)] median_img[i, j] = median return median_img -if __name__ == '__main__': +if __name__ == "__main__": # read original image - img = imread('../image_data/lena.jpg') + img = imread("../image_data/lena.jpg") # turn image in gray scale value gray = cvtColor(img, COLOR_BGR2GRAY) @@ -37,6 +37,6 @@ def median_filter(gray_img, mask=3): median5x5 = median_filter(gray, 5) # show result images - imshow('median filter with 3x3 mask', median3x3) - imshow('median filter with 5x5 mask', median5x5) + imshow("median filter with 3x3 mask", median3x3) + imshow("median filter with 5x5 mask", median5x5) waitKey(0) diff --git a/digital_image_processing/filters/sobel_filter.py b/digital_image_processing/filters/sobel_filter.py index f3ef407d49e5..822d49fe38a1 100644 --- a/digital_image_processing/filters/sobel_filter.py +++ b/digital_image_processing/filters/sobel_filter.py @@ -13,26 +13,26 @@ def sobel_filter(image): dst_x = np.abs(img_convolve(image, kernel_x)) dst_y = np.abs(img_convolve(image, kernel_y)) # modify the pix within [0, 255] - dst_x = dst_x * 255/np.max(dst_x) - dst_y = dst_y * 255/np.max(dst_y) + dst_x = dst_x * 255 / np.max(dst_x) + dst_y = dst_y * 255 / np.max(dst_y) dst_xy = np.sqrt((np.square(dst_x)) + (np.square(dst_y))) - dst_xy = dst_xy * 255/np.max(dst_xy) + dst_xy = dst_xy * 255 / np.max(dst_xy) dst = dst_xy.astype(np.uint8) theta = np.arctan2(dst_y, dst_x) return dst, theta -if __name__ == '__main__': +if __name__ == "__main__": # read original image - img = imread('../image_data/lena.jpg') + img = imread("../image_data/lena.jpg") # turn image in gray scale value gray = cvtColor(img, COLOR_BGR2GRAY) sobel_grad, sobel_theta = sobel_filter(gray) # show result images - imshow('sobel filter', sobel_grad) - imshow('sobel theta', sobel_theta) + imshow("sobel filter", sobel_grad) + imshow("sobel theta", sobel_theta) waitKey(0) diff --git a/divide_and_conquer/closest_pair_of_points.py b/divide_and_conquer/closest_pair_of_points.py index 11dac7e0ab2a..eecf53a7450e 100644 --- a/divide_and_conquer/closest_pair_of_points.py +++ b/divide_and_conquer/closest_pair_of_points.py @@ -28,15 +28,15 @@ def euclidean_distance_sqr(point1, point2): return (point1[0] - point2[0]) ** 2 + (point1[1] - point2[1]) ** 2 -def column_based_sort(array, column = 0): +def column_based_sort(array, column=0): """ >>> column_based_sort([(5, 1), (4, 2), (3, 0)], 1) [(3, 0), (5, 1), (4, 2)] """ - return sorted(array, key = lambda x: x[column]) + return sorted(array, key=lambda x: x[column]) -def dis_between_closest_pair(points, points_counts, min_dis = float("inf")): +def dis_between_closest_pair(points, points_counts, min_dis=float("inf")): """ brute force approach to find distance between closest pair points @@ -52,14 +52,14 @@ def dis_between_closest_pair(points, points_counts, min_dis = float("inf")): """ for i in range(points_counts - 1): - for j in range(i+1, points_counts): + for j in range(i + 1, points_counts): current_dis = euclidean_distance_sqr(points[i], points[j]) if current_dis < min_dis: min_dis = current_dis return min_dis -def dis_between_closest_in_strip(points, points_counts, min_dis = float("inf")): +def dis_between_closest_in_strip(points, points_counts, min_dis=float("inf")): """ closest pair of points in strip @@ -74,7 +74,7 @@ def dis_between_closest_in_strip(points, points_counts, min_dis = float("inf")): """ for i in range(min(6, points_counts - 1), points_counts): - for j in range(max(0, i-6), i): + for j in range(max(0, i - 6), i): current_dis = euclidean_distance_sqr(points[i], points[j]) if current_dis < min_dis: min_dis = current_dis @@ -99,13 +99,13 @@ def closest_pair_of_points_sqr(points_sorted_on_x, points_sorted_on_y, points_co return dis_between_closest_pair(points_sorted_on_x, points_counts) # recursion - mid = points_counts//2 - closest_in_left = closest_pair_of_points_sqr(points_sorted_on_x, - points_sorted_on_y[:mid], - mid) - closest_in_right = closest_pair_of_points_sqr(points_sorted_on_y, - points_sorted_on_y[mid:], - points_counts - mid) + mid = points_counts // 2 + closest_in_left = closest_pair_of_points_sqr( + points_sorted_on_x, points_sorted_on_y[:mid], mid + ) + closest_in_right = closest_pair_of_points_sqr( + points_sorted_on_y, points_sorted_on_y[mid:], points_counts - mid + ) closest_pair_dis = min(closest_in_left, closest_in_right) """ @@ -118,8 +118,9 @@ def closest_pair_of_points_sqr(points_sorted_on_x, points_sorted_on_y, points_co if abs(point[0] - points_sorted_on_x[mid][0]) < closest_pair_dis: cross_strip.append(point) - closest_in_strip = dis_between_closest_in_strip(cross_strip, - len(cross_strip), closest_pair_dis) + closest_in_strip = dis_between_closest_in_strip( + cross_strip, len(cross_strip), closest_pair_dis + ) return min(closest_pair_dis, closest_in_strip) @@ -128,11 +129,13 @@ def closest_pair_of_points(points, points_counts): >>> closest_pair_of_points([(2, 3), (12, 30)], len([(2, 3), (12, 30)])) 28.792360097775937 """ - points_sorted_on_x = column_based_sort(points, column = 0) - points_sorted_on_y = column_based_sort(points, column = 1) - return (closest_pair_of_points_sqr(points_sorted_on_x, - points_sorted_on_y, - points_counts)) ** 0.5 + points_sorted_on_x = column_based_sort(points, column=0) + points_sorted_on_y = column_based_sort(points, column=1) + return ( + closest_pair_of_points_sqr( + points_sorted_on_x, points_sorted_on_y, points_counts + ) + ) ** 0.5 if __name__ == "__main__": diff --git a/divide_and_conquer/convex_hull.py b/divide_and_conquer/convex_hull.py index 534ebda2c780..bd88256ab01c 100644 --- a/divide_and_conquer/convex_hull.py +++ b/divide_and_conquer/convex_hull.py @@ -1,4 +1,5 @@ from numbers import Number + """ The convex hull problem is problem of finding all the vertices of convex polygon, P of a set of points in a plane such that all the points are either on the vertices of P or @@ -47,8 +48,10 @@ def __init__(self, x, y): try: x, y = float(x), float(y) except ValueError as e: - e.args = ("x and y must be both numeric types " - "but got {}, {} instead".format(type(x), type(y)), ) + e.args = ( + "x and y must be both numeric types " + "but got {}, {} instead".format(type(x), type(y)), + ) raise self.x = x @@ -85,7 +88,7 @@ def __le__(self, other): return False def __repr__(self): - return "({}, {})".format(self.x, self.y) + return "({}, {})".format(self.x, self.y) def __hash__(self): return hash(self.x) @@ -132,8 +135,10 @@ def _construct_points(list_of_tuples): try: points.append(Point(p[0], p[1])) except (IndexError, TypeError): - print("Ignoring deformed point {}. All points" - " must have at least 2 coordinates.".format(p)) + print( + "Ignoring deformed point {}. All points" + " must have at least 2 coordinates.".format(p) + ) return points @@ -189,12 +194,15 @@ def _validate_input(points): if isinstance(points[0], (list, tuple)): points = _construct_points(points) else: - raise ValueError("Expecting an iterable of type Point, list or tuple. " - "Found objects of type {} instead" - .format(type(points[0]))) + raise ValueError( + "Expecting an iterable of type Point, list or tuple. " + "Found objects of type {} instead".format(type(points[0])) + ) elif not hasattr(points, "__iter__"): - raise ValueError("Expecting an iterable object " - "but got an non-iterable type {}".format(points)) + raise ValueError( + "Expecting an iterable object " + "but got an non-iterable type {}".format(points) + ) except TypeError as e: print("Expecting an iterable of type Point, list or tuple.") raise @@ -277,7 +285,7 @@ def convex_hull_bf(points): n = len(points) convex_set = set() - for i in range(n-1): + for i in range(n - 1): for j in range(i + 1, n): points_left_of_ij = points_right_of_ij = False ij_part_of_convex_hull = True @@ -353,13 +361,13 @@ def convex_hull_recursive(points): # convex hull left_most_point = points[0] - right_most_point = points[n-1] + right_most_point = points[n - 1] convex_set = {left_most_point, right_most_point} upperhull = [] lowerhull = [] - for i in range(1, n-1): + for i in range(1, n - 1): det = _det(left_most_point, right_most_point, points[i]) if det > 0: @@ -394,7 +402,7 @@ def _construct_hull(points, left, right, convex_set): """ if points: extreme_point = None - extreme_point_distance = float('-inf') + extreme_point_distance = float("-inf") candidate_points = [] for p in points: @@ -414,8 +422,18 @@ def _construct_hull(points, left, right, convex_set): def main(): - points = [(0, 3), (2, 2), (1, 1), (2, 1), (3, 0), - (0, 0), (3, 3), (2, -1), (2, -4), (1, -3)] + points = [ + (0, 3), + (2, 2), + (1, 1), + (2, 1), + (3, 0), + (0, 0), + (3, 3), + (2, -1), + (2, -4), + (1, -3), + ] # the convex set of points is # [(0, 0), (0, 3), (1, -3), (2, -4), (3, 0), (3, 3)] results_recursive = convex_hull_recursive(points) @@ -425,5 +443,5 @@ def main(): print(results_bf) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/divide_and_conquer/inversions.py b/divide_and_conquer/inversions.py index e4d50b7d4729..9bb656229321 100644 --- a/divide_and_conquer/inversions.py +++ b/divide_and_conquer/inversions.py @@ -38,7 +38,7 @@ def count_inversions_bf(arr): num_inversions = 0 n = len(arr) - for i in range(n-1): + for i in range(n - 1): for j in range(i + 1, n): if arr[i] > arr[j]: num_inversions += 1 @@ -73,7 +73,7 @@ def count_inversions_recursive(arr): if len(arr) <= 1: return arr, 0 else: - mid = len(arr)//2 + mid = len(arr) // 2 P = arr[0:mid] Q = arr[mid:] @@ -119,7 +119,7 @@ def _count_cross_inversions(P, Q): # if P[1] > Q[j], then P[k] > Q[k] for all i < k <= len(P) # These are all inversions. The claim emerges from the # property that P is sorted. - num_inversion += (len(P) - i) + num_inversion += len(P) - i R.append(Q[j]) j += 1 else: @@ -127,9 +127,9 @@ def _count_cross_inversions(P, Q): i += 1 if i < len(P): - R.extend(P[i:]) + R.extend(P[i:]) else: - R.extend(Q[j:]) + R.extend(Q[j:]) return R, num_inversion @@ -166,6 +166,4 @@ def main(): if __name__ == "__main__": - main() - - + main() diff --git a/divide_and_conquer/max_subarray_sum.py b/divide_and_conquer/max_subarray_sum.py index 0428f4e13768..9e81c83649a6 100644 --- a/divide_and_conquer/max_subarray_sum.py +++ b/divide_and_conquer/max_subarray_sum.py @@ -40,8 +40,8 @@ def max_cross_array_sum(array, left, mid, right): """ - max_sum_of_left = max_sum_from_start(array[left:mid+1][::-1]) - max_sum_of_right = max_sum_from_start(array[mid+1: right+1]) + max_sum_of_left = max_sum_from_start(array[left : mid + 1][::-1]) + max_sum_of_right = max_sum_from_start(array[mid + 1 : right + 1]) return max_sum_of_left + max_sum_of_right @@ -60,7 +60,7 @@ def max_subarray_sum(array, left, right): # base case: array has only one element if left == right: return array[right] - + # Recursion mid = (left + right) // 2 left_half_sum = max_subarray_sum(array, left, mid) @@ -71,5 +71,6 @@ def max_subarray_sum(array, left, right): array = [-2, -5, 6, -2, -3, 1, 5, -6] array_length = len(array) -print("Maximum sum of contiguous subarray:", max_subarray_sum(array, 0, array_length - 1)) - +print( + "Maximum sum of contiguous subarray:", max_subarray_sum(array, 0, array_length - 1) +) diff --git a/dynamic_programming/bitmask.py b/dynamic_programming/bitmask.py index 6685e1c68ee6..5c1ed36cb42a 100644 --- a/dynamic_programming/bitmask.py +++ b/dynamic_programming/bitmask.py @@ -13,54 +13,55 @@ class AssignmentUsingBitmask: - def __init__(self,task_performed,total): + def __init__(self, task_performed, total): - self.total_tasks = total #total no of tasks (N) + self.total_tasks = total # total no of tasks (N) # DP table will have a dimension of (2^M)*N # initially all values are set to -1 - self.dp = [[-1 for i in range(total+1)] for j in range(2**len(task_performed))] + self.dp = [ + [-1 for i in range(total + 1)] for j in range(2 ** len(task_performed)) + ] - self.task = defaultdict(list) #stores the list of persons for each task + self.task = defaultdict(list) # stores the list of persons for each task - #finalmask is used to check if all persons are included by setting all bits to 1 - self.finalmask = (1< self.total_tasks: return 0 - #if case already considered - if self.dp[mask][taskno]!=-1: + # if case already considered + if self.dp[mask][taskno] != -1: return self.dp[mask][taskno] # Number of ways when we dont this task in the arrangement - total_ways_util = self.CountWaysUtil(mask,taskno+1) + total_ways_util = self.CountWaysUtil(mask, taskno + 1) # now assign the tasks one by one to all possible persons and recursively assign for the remaining tasks. if taskno in self.task: for p in self.task[taskno]: # if p is already given a task - if mask & (1<-1): + if x == -1: + return y + 1 + elif y == -1: + return x + 1 + elif self.dp[x][y] > -1: return self.dp[x][y] else: - if (self.A[x]==self.B[y]): - self.dp[x][y] = self.__solveDP(x-1,y-1) + if self.A[x] == self.B[y]: + self.dp[x][y] = self.__solveDP(x - 1, y - 1) else: - self.dp[x][y] = 1+min(self.__solveDP(x,y-1), self.__solveDP(x-1,y), self.__solveDP(x-1,y-1)) + self.dp[x][y] = 1 + min( + self.__solveDP(x, y - 1), + self.__solveDP(x - 1, y), + self.__solveDP(x - 1, y - 1), + ) return self.dp[x][y] def solve(self, A, B): - if isinstance(A,bytes): - A = A.decode('ascii') + if isinstance(A, bytes): + A = A.decode("ascii") - if isinstance(B,bytes): - B = B.decode('ascii') + if isinstance(B, bytes): + B = B.decode("ascii") self.A = str(A) self.B = str(B) self.__prepare__(len(A), len(B)) - return self.__solveDP(len(A)-1, len(B)-1) + return self.__solveDP(len(A) - 1, len(B) - 1) def min_distance_bottom_up(word1: str, word2: str) -> int: @@ -63,38 +67,37 @@ def min_distance_bottom_up(word1: str, word2: str) -> int: """ m = len(word1) n = len(word2) - dp = [[0 for _ in range(n+1) ] for _ in range(m+1)] - for i in range(m+1): - for j in range(n+1): + dp = [[0 for _ in range(n + 1)] for _ in range(m + 1)] + for i in range(m + 1): + for j in range(n + 1): - if i == 0: #first string is empty + if i == 0: # first string is empty dp[i][j] = j - elif j == 0: #second string is empty + elif j == 0: # second string is empty dp[i][j] = i - elif word1[i-1] == word2[j-1]: #last character of both substing is equal - dp[i][j] = dp[i-1][j-1] + elif ( + word1[i - 1] == word2[j - 1] + ): # last character of both substing is equal + dp[i][j] = dp[i - 1][j - 1] else: - insert = dp[i][j-1] - delete = dp[i-1][j] - replace = dp[i-1][j-1] + insert = dp[i][j - 1] + delete = dp[i - 1][j] + replace = dp[i - 1][j - 1] dp[i][j] = 1 + min(insert, delete, replace) return dp[m][n] -if __name__ == '__main__': - solver = EditDistance() - - print("****************** Testing Edit Distance DP Algorithm ******************") - print() - - S1 = input("Enter the first string: ").strip() - S2 = input("Enter the second string: ").strip() - - print() - print("The minimum Edit Distance is: %d" % (solver.solve(S1, S2))) - print("The minimum Edit Distance is: %d" % (min_distance_bottom_up(S1, S2))) - print() - print("*************** End of Testing Edit Distance DP Algorithm ***************") +if __name__ == "__main__": + solver = EditDistance() + print("****************** Testing Edit Distance DP Algorithm ******************") + print() + S1 = input("Enter the first string: ").strip() + S2 = input("Enter the second string: ").strip() + print() + print("The minimum Edit Distance is: %d" % (solver.solve(S1, S2))) + print("The minimum Edit Distance is: %d" % (min_distance_bottom_up(S1, S2))) + print() + print("*************** End of Testing Edit Distance DP Algorithm ***************") diff --git a/dynamic_programming/factorial.py b/dynamic_programming/factorial.py index 7c6541ee2a74..0269014e7a18 100644 --- a/dynamic_programming/factorial.py +++ b/dynamic_programming/factorial.py @@ -1,6 +1,8 @@ -#Factorial of a number using memoization -result=[-1]*10 -result[0]=result[1]=1 +# Factorial of a number using memoization +result = [-1] * 10 +result[0] = result[1] = 1 + + def factorial(num): """ >>> factorial(7) @@ -10,19 +12,20 @@ def factorial(num): >>> [factorial(i) for i in range(5)] [1, 1, 2, 6, 24] """ - - if num<0: + + if num < 0: return "Number should not be negative." - if result[num]!=-1: + if result[num] != -1: return result[num] else: - result[num]=num*factorial(num-1) - #uncomment the following to see how recalculations are avoided - #print(result) + result[num] = num * factorial(num - 1) + # uncomment the following to see how recalculations are avoided + # print(result) return result[num] -#factorial of num -#uncomment the following to see how recalculations are avoided + +# factorial of num +# uncomment the following to see how recalculations are avoided ##result=[-1]*10 ##result[0]=result[1]=1 ##print(factorial(5)) @@ -31,4 +34,5 @@ def factorial(num): if __name__ == "__main__": import doctest + doctest.testmod() diff --git a/dynamic_programming/fibonacci.py b/dynamic_programming/fibonacci.py index 90fe6386044a..2dd1c2555f3e 100644 --- a/dynamic_programming/fibonacci.py +++ b/dynamic_programming/fibonacci.py @@ -4,7 +4,6 @@ class Fibonacci: - def __init__(self, N=None): self.fib_array = [] if N: @@ -19,14 +18,14 @@ def __init__(self, N=None): def get(self, sequence_no=None): if sequence_no != None: if sequence_no < len(self.fib_array): - return print(self.fib_array[:sequence_no + 1]) + return print(self.fib_array[: sequence_no + 1]) else: print("Out of bound.") else: print("Please specify a value") -if __name__ == '__main__': +if __name__ == "__main__": print("\n********* Fibonacci Series Using Dynamic Programming ************\n") print("\n Enter the upper limit for the fibonacci sequence: ", end="") try: @@ -34,7 +33,8 @@ def get(self, sequence_no=None): fib = Fibonacci(N) print( "\n********* Enter different values to get the corresponding fibonacci " - "sequence, enter any negative number to exit. ************\n") + "sequence, enter any negative number to exit. ************\n" + ) while True: try: i = int(input("Enter value: ").strip()) diff --git a/dynamic_programming/floyd_warshall.py b/dynamic_programming/floyd_warshall.py index 038499ca03b6..a4b6c6a82568 100644 --- a/dynamic_programming/floyd_warshall.py +++ b/dynamic_programming/floyd_warshall.py @@ -1,37 +1,42 @@ import math + class Graph: - - def __init__(self, N = 0): # a graph with Node 0,1,...,N-1 + def __init__(self, N=0): # a graph with Node 0,1,...,N-1 self.N = N - self.W = [[math.inf for j in range(0,N)] for i in range(0,N)] # adjacency matrix for weight - self.dp = [[math.inf for j in range(0,N)] for i in range(0,N)] # dp[i][j] stores minimum distance from i to j + self.W = [ + [math.inf for j in range(0, N)] for i in range(0, N) + ] # adjacency matrix for weight + self.dp = [ + [math.inf for j in range(0, N)] for i in range(0, N) + ] # dp[i][j] stores minimum distance from i to j def addEdge(self, u, v, w): self.dp[u][v] = w def floyd_warshall(self): - for k in range(0,self.N): - for i in range(0,self.N): - for j in range(0,self.N): + for k in range(0, self.N): + for i in range(0, self.N): + for j in range(0, self.N): self.dp[i][j] = min(self.dp[i][j], self.dp[i][k] + self.dp[k][j]) def showMin(self, u, v): return self.dp[u][v] - -if __name__ == '__main__': + + +if __name__ == "__main__": graph = Graph(5) - graph.addEdge(0,2,9) - graph.addEdge(0,4,10) - graph.addEdge(1,3,5) - graph.addEdge(2,3,7) - graph.addEdge(3,0,10) - graph.addEdge(3,1,2) - graph.addEdge(3,2,1) - graph.addEdge(3,4,6) - graph.addEdge(4,1,3) - graph.addEdge(4,2,4) - graph.addEdge(4,3,9) + graph.addEdge(0, 2, 9) + graph.addEdge(0, 4, 10) + graph.addEdge(1, 3, 5) + graph.addEdge(2, 3, 7) + graph.addEdge(3, 0, 10) + graph.addEdge(3, 1, 2) + graph.addEdge(3, 2, 1) + graph.addEdge(3, 4, 6) + graph.addEdge(4, 1, 3) + graph.addEdge(4, 2, 4) + graph.addEdge(4, 3, 9) graph.floyd_warshall() - graph.showMin(1,4) - graph.showMin(0,3) + graph.showMin(1, 4) + graph.showMin(0, 3) diff --git a/dynamic_programming/fractional_knapsack.py b/dynamic_programming/fractional_knapsack.py index 74e85b4b4708..881b6a3969d0 100644 --- a/dynamic_programming/fractional_knapsack.py +++ b/dynamic_programming/fractional_knapsack.py @@ -1,12 +1,20 @@ from itertools import accumulate from bisect import bisect + def fracKnapsack(vl, wt, W, n): - r = list(sorted(zip(vl,wt), key=lambda x:x[0]/x[1],reverse=True)) - vl , wt = [i[0] for i in r],[i[1] for i in r] - acc=list(accumulate(wt)) - k = bisect(acc,W) - return 0 if k == 0 else sum(vl[:k])+(W-acc[k-1])*(vl[k])/(wt[k]) if k!=n else sum(vl[:k]) + r = list(sorted(zip(vl, wt), key=lambda x: x[0] / x[1], reverse=True)) + vl, wt = [i[0] for i in r], [i[1] for i in r] + acc = list(accumulate(wt)) + k = bisect(acc, W) + return ( + 0 + if k == 0 + else sum(vl[:k]) + (W - acc[k - 1]) * (vl[k]) / (wt[k]) + if k != n + else sum(vl[:k]) + ) + -print("%.0f"%fracKnapsack([60, 100, 120],[10, 20, 30],50,3)) +print("%.0f" % fracKnapsack([60, 100, 120], [10, 20, 30], 50, 3)) diff --git a/dynamic_programming/integer_partition.py b/dynamic_programming/integer_partition.py index f17561fc135b..ec8c5bf62d7d 100644 --- a/dynamic_programming/integer_partition.py +++ b/dynamic_programming/integer_partition.py @@ -1,33 +1,36 @@ -''' +""" The number of partitions of a number n into at least k parts equals the number of partitions into exactly k parts plus the number of partitions into at least k-1 parts. Subtracting 1 from each part of a partition of n into k parts gives a partition of n-k into k parts. These two facts together are used for this algorithm. -''' +""" + + def partition(m): - memo = [[0 for _ in range(m)] for _ in range(m+1)] - for i in range(m+1): - memo[i][0] = 1 + memo = [[0 for _ in range(m)] for _ in range(m + 1)] + for i in range(m + 1): + memo[i][0] = 1 + + for n in range(m + 1): + for k in range(1, m): + memo[n][k] += memo[n][k - 1] + if n - k > 0: + memo[n][k] += memo[n - k - 1][k] - for n in range(m+1): - for k in range(1, m): - memo[n][k] += memo[n][k-1] - if n-k > 0: - memo[n][k] += memo[n-k-1][k] + return memo[m][m - 1] - return memo[m][m-1] -if __name__ == '__main__': - import sys +if __name__ == "__main__": + import sys - if len(sys.argv) == 1: - try: - n = int(input('Enter a number: ').strip()) - print(partition(n)) - except ValueError: - print('Please enter a number.') - else: - try: - n = int(sys.argv[1]) - print(partition(n)) - except ValueError: - print('Please pass a number.') \ No newline at end of file + if len(sys.argv) == 1: + try: + n = int(input("Enter a number: ").strip()) + print(partition(n)) + except ValueError: + print("Please enter a number.") + else: + try: + n = int(sys.argv[1]) + print(partition(n)) + except ValueError: + print("Please pass a number.") diff --git a/dynamic_programming/k_means_clustering_tensorflow.py b/dynamic_programming/k_means_clustering_tensorflow.py index b6813c6a22b3..6b1eb628e5c3 100644 --- a/dynamic_programming/k_means_clustering_tensorflow.py +++ b/dynamic_programming/k_means_clustering_tensorflow.py @@ -14,24 +14,24 @@ def TFKMeansCluster(vectors, noofclusters): noofclusters = int(noofclusters) assert noofclusters < len(vectors) - #Find out the dimensionality + # Find out the dimensionality dim = len(vectors[0]) - #Will help select random centroids from among the available vectors + # Will help select random centroids from among the available vectors vector_indices = list(range(len(vectors))) shuffle(vector_indices) - #GRAPH OF COMPUTATION - #We initialize a new graph and set it as the default during each run - #of this algorithm. This ensures that as this function is called - #multiple times, the default graph doesn't keep getting crowded with - #unused ops and Variables from previous function calls. + # GRAPH OF COMPUTATION + # We initialize a new graph and set it as the default during each run + # of this algorithm. This ensures that as this function is called + # multiple times, the default graph doesn't keep getting crowded with + # unused ops and Variables from previous function calls. graph = tf.Graph() with graph.as_default(): - #SESSION OF COMPUTATION + # SESSION OF COMPUTATION sess = tf.Session() @@ -39,8 +39,9 @@ def TFKMeansCluster(vectors, noofclusters): ##First lets ensure we have a Variable vector for each centroid, ##initialized to one of the vectors from the available data points - centroids = [tf.Variable((vectors[vector_indices[i]])) - for i in range(noofclusters)] + centroids = [ + tf.Variable((vectors[vector_indices[i]])) for i in range(noofclusters) + ] ##These nodes will assign the centroid Variables the appropriate ##values centroid_value = tf.placeholder("float64", [dim]) @@ -56,26 +57,24 @@ def TFKMeansCluster(vectors, noofclusters): assignment_value = tf.placeholder("int32") cluster_assigns = [] for assignment in assignments: - cluster_assigns.append(tf.assign(assignment, - assignment_value)) + cluster_assigns.append(tf.assign(assignment, assignment_value)) ##Now lets construct the node that will compute the mean - #The placeholder for the input + # The placeholder for the input mean_input = tf.placeholder("float", [None, dim]) - #The Node/op takes the input and computes a mean along the 0th - #dimension, i.e. the list of input vectors + # The Node/op takes the input and computes a mean along the 0th + # dimension, i.e. the list of input vectors mean_op = tf.reduce_mean(mean_input, 0) ##Node for computing Euclidean distances - #Placeholders for input + # Placeholders for input v1 = tf.placeholder("float", [dim]) v2 = tf.placeholder("float", [dim]) - euclid_dist = tf.sqrt(tf.reduce_sum(tf.pow(tf.sub( - v1, v2), 2))) + euclid_dist = tf.sqrt(tf.reduce_sum(tf.pow(tf.sub(v1, v2), 2))) ##This node will figure out which cluster to assign a vector to, ##based on Euclidean distances of the vector from the centroids. - #Placeholder for input + # Placeholder for input centroid_distances = tf.placeholder("float", [noofclusters]) cluster_assignment = tf.argmin(centroid_distances, 0) @@ -87,55 +86,62 @@ def TFKMeansCluster(vectors, noofclusters): ##will be included in the initialization. init_op = tf.initialize_all_variables() - #Initialize all variables + # Initialize all variables sess.run(init_op) ##CLUSTERING ITERATIONS - #Now perform the Expectation-Maximization steps of K-Means clustering - #iterations. To keep things simple, we will only do a set number of - #iterations, instead of using a Stopping Criterion. + # Now perform the Expectation-Maximization steps of K-Means clustering + # iterations. To keep things simple, we will only do a set number of + # iterations, instead of using a Stopping Criterion. noofiterations = 100 for iteration_n in range(noofiterations): ##EXPECTATION STEP ##Based on the centroid locations till last iteration, compute ##the _expected_ centroid assignments. - #Iterate over each vector + # Iterate over each vector for vector_n in range(len(vectors)): vect = vectors[vector_n] - #Compute Euclidean distance between this vector and each - #centroid. Remember that this list cannot be named + # Compute Euclidean distance between this vector and each + # centroid. Remember that this list cannot be named #'centroid_distances', since that is the input to the - #cluster assignment node. - distances = [sess.run(euclid_dist, feed_dict={ - v1: vect, v2: sess.run(centroid)}) - for centroid in centroids] - #Now use the cluster assignment node, with the distances - #as the input - assignment = sess.run(cluster_assignment, feed_dict = { - centroid_distances: distances}) - #Now assign the value to the appropriate state variable - sess.run(cluster_assigns[vector_n], feed_dict={ - assignment_value: assignment}) + # cluster assignment node. + distances = [ + sess.run(euclid_dist, feed_dict={v1: vect, v2: sess.run(centroid)}) + for centroid in centroids + ] + # Now use the cluster assignment node, with the distances + # as the input + assignment = sess.run( + cluster_assignment, feed_dict={centroid_distances: distances} + ) + # Now assign the value to the appropriate state variable + sess.run( + cluster_assigns[vector_n], feed_dict={assignment_value: assignment} + ) ##MAXIMIZATION STEP - #Based on the expected state computed from the Expectation Step, - #compute the locations of the centroids so as to maximize the - #overall objective of minimizing within-cluster Sum-of-Squares + # Based on the expected state computed from the Expectation Step, + # compute the locations of the centroids so as to maximize the + # overall objective of minimizing within-cluster Sum-of-Squares for cluster_n in range(noofclusters): - #Collect all the vectors assigned to this cluster - assigned_vects = [vectors[i] for i in range(len(vectors)) - if sess.run(assignments[i]) == cluster_n] - #Compute new centroid location - new_location = sess.run(mean_op, feed_dict={ - mean_input: array(assigned_vects)}) - #Assign value to appropriate variable - sess.run(cent_assigns[cluster_n], feed_dict={ - centroid_value: new_location}) - - #Return centroids and assignments + # Collect all the vectors assigned to this cluster + assigned_vects = [ + vectors[i] + for i in range(len(vectors)) + if sess.run(assignments[i]) == cluster_n + ] + # Compute new centroid location + new_location = sess.run( + mean_op, feed_dict={mean_input: array(assigned_vects)} + ) + # Assign value to appropriate variable + sess.run( + cent_assigns[cluster_n], feed_dict={centroid_value: new_location} + ) + + # Return centroids and assignments centroids = sess.run(centroids) assignments = sess.run(assignments) return centroids, assignments - diff --git a/dynamic_programming/knapsack.py b/dynamic_programming/knapsack.py index 488059d6244d..e71e3892e8cc 100644 --- a/dynamic_programming/knapsack.py +++ b/dynamic_programming/knapsack.py @@ -8,36 +8,38 @@ def MF_knapsack(i, wt, val, j): - ''' + """ This code involves the concept of memory functions. Here we solve the subproblems which are needed unlike the below example F is a 2D array with -1s filled up - ''' + """ global F # a global dp table for knapsack if F[i][j] < 0: - if j < wt[i-1]: - val = MF_knapsack(i-1, wt, val, j) + if j < wt[i - 1]: + val = MF_knapsack(i - 1, wt, val, j) else: - val = max(MF_knapsack(i-1, wt, val, j), - MF_knapsack(i-1, wt, val, j - wt[i-1]) + val[i-1]) + val = max( + MF_knapsack(i - 1, wt, val, j), + MF_knapsack(i - 1, wt, val, j - wt[i - 1]) + val[i - 1], + ) F[i][j] = val return F[i][j] def knapsack(W, wt, val, n): - dp = [[0 for i in range(W+1)]for j in range(n+1)] + dp = [[0 for i in range(W + 1)] for j in range(n + 1)] - for i in range(1,n+1): - for w in range(1, W+1): - if wt[i-1] <= w: - dp[i][w] = max(val[i-1] + dp[i-1][w-wt[i-1]], dp[i-1][w]) + for i in range(1, n + 1): + for w in range(1, W + 1): + if wt[i - 1] <= w: + dp[i][w] = max(val[i - 1] + dp[i - 1][w - wt[i - 1]], dp[i - 1][w]) else: - dp[i][w] = dp[i-1][w] + dp[i][w] = dp[i - 1][w] return dp[n][W], dp -def knapsack_with_example_solution(W: int, wt: list, val:list): +def knapsack_with_example_solution(W: int, wt: list, val: list): """ Solves the integer weights knapsack problem returns one of the several possible optimal subsets. @@ -70,17 +72,23 @@ def knapsack_with_example_solution(W: int, wt: list, val:list): But got 4 weights and 3 values """ if not (isinstance(wt, (list, tuple)) and isinstance(val, (list, tuple))): - raise ValueError("Both the weights and values vectors must be either lists or tuples") + raise ValueError( + "Both the weights and values vectors must be either lists or tuples" + ) num_items = len(wt) if num_items != len(val): - raise ValueError("The number of weights must be the " - "same as the number of values.\nBut " - "got {} weights and {} values".format(num_items, len(val))) + raise ValueError( + "The number of weights must be the " + "same as the number of values.\nBut " + "got {} weights and {} values".format(num_items, len(val)) + ) for i in range(num_items): if not isinstance(wt[i], int): - raise TypeError("All weights must be integers but " - "got weight of type {} at index {}".format(type(wt[i]), i)) + raise TypeError( + "All weights must be integers but " + "got weight of type {} at index {}".format(type(wt[i]), i) + ) optimal_val, dp_table = knapsack(W, wt, val, num_items) example_optional_set = set() @@ -89,7 +97,7 @@ def knapsack_with_example_solution(W: int, wt: list, val:list): return optimal_val, example_optional_set -def _construct_solution(dp:list, wt:list, i:int, j:int, optimal_set:set): +def _construct_solution(dp: list, wt: list, i: int, j: int, optimal_set: set): """ Recursively reconstructs one of the optimal subsets given a filled DP table and the vector of weights @@ -117,21 +125,21 @@ def _construct_solution(dp:list, wt:list, i:int, j:int, optimal_set:set): _construct_solution(dp, wt, i - 1, j, optimal_set) else: optimal_set.add(i) - _construct_solution(dp, wt, i - 1, j - wt[i-1], optimal_set) + _construct_solution(dp, wt, i - 1, j - wt[i - 1], optimal_set) -if __name__ == '__main__': - ''' +if __name__ == "__main__": + """ Adding test case for knapsack - ''' + """ val = [3, 2, 4, 4] wt = [4, 3, 2, 3] n = 4 w = 6 F = [[0] * (w + 1)] + [[0] + [-1 for i in range(w + 1)] for j in range(n + 1)] - optimal_solution, _ = knapsack(w,wt,val, n) + optimal_solution, _ = knapsack(w, wt, val, n) print(optimal_solution) - print(MF_knapsack(n,wt,val,w)) # switched the n and w + print(MF_knapsack(n, wt, val, w)) # switched the n and w # testing the dynamic programming problem with example # the optimal subset for the above example are items 3 and 4 @@ -140,4 +148,3 @@ def _construct_solution(dp:list, wt:list, i:int, j:int, optimal_set:set): assert optimal_subset == {3, 4} print("optimal_value = ", optimal_solution) print("An optimal subset corresponding to the optimal value", optimal_subset) - diff --git a/dynamic_programming/longest_common_subsequence.py b/dynamic_programming/longest_common_subsequence.py index d39485408988..12fcae684051 100644 --- a/dynamic_programming/longest_common_subsequence.py +++ b/dynamic_programming/longest_common_subsequence.py @@ -41,12 +41,12 @@ def longest_common_subsequence(x: str, y: str): for i in range(1, m + 1): for j in range(1, n + 1): - if x[i-1] == y[j-1]: + if x[i - 1] == y[j - 1]: match = 1 else: match = 0 - L[i][j] = max(L[i-1][j], L[i][j-1], L[i-1][j-1] + match) + L[i][j] = max(L[i - 1][j], L[i][j - 1], L[i - 1][j - 1] + match) seq = "" i, j = m, n @@ -69,9 +69,9 @@ def longest_common_subsequence(x: str, y: str): return L[m][n], seq -if __name__ == '__main__': - a = 'AGGTAB' - b = 'GXTXAYB' +if __name__ == "__main__": + a = "AGGTAB" + b = "GXTXAYB" expected_ln = 4 expected_subseq = "GTAB" diff --git a/dynamic_programming/longest_increasing_subsequence.py b/dynamic_programming/longest_increasing_subsequence.py index 151a5e0b7c80..3cb57806e06f 100644 --- a/dynamic_programming/longest_increasing_subsequence.py +++ b/dynamic_programming/longest_increasing_subsequence.py @@ -1,4 +1,4 @@ -''' +""" Author : Mehdi ALAOUI This is a pure Python implementation of Dynamic Programming solution to the longest increasing subsequence of a given sequence. @@ -6,35 +6,40 @@ The problem is : Given an ARRAY, to find the longest and increasing sub ARRAY in that given ARRAY and return it. Example: [10, 22, 9, 33, 21, 50, 41, 60, 80] as input will return [10, 22, 33, 41, 60, 80] as output -''' -def longestSub(ARRAY): #This function is recursive - - ARRAY_LENGTH = len(ARRAY) - if(ARRAY_LENGTH <= 1): #If the array contains only one element, we return it (it's the stop condition of recursion) - return ARRAY - #Else - PIVOT=ARRAY[0] - isFound=False - i=1 - LONGEST_SUB=[] - while(not isFound and i= ARRAY[i] ] - TEMPORARY_ARRAY = longestSub(TEMPORARY_ARRAY) - if ( len(TEMPORARY_ARRAY) > len(LONGEST_SUB) ): - LONGEST_SUB = TEMPORARY_ARRAY - else: - i+=1 - - TEMPORARY_ARRAY = [ element for element in ARRAY[1:] if element >= PIVOT ] - TEMPORARY_ARRAY = [PIVOT] + longestSub(TEMPORARY_ARRAY) - if ( len(TEMPORARY_ARRAY) > len(LONGEST_SUB) ): - return TEMPORARY_ARRAY - else: - return LONGEST_SUB - -#Some examples - -print(longestSub([4,8,7,5,1,12,2,3,9])) -print(longestSub([9,8,7,6,5,7])) \ No newline at end of file +""" + + +def longestSub(ARRAY): # This function is recursive + + ARRAY_LENGTH = len(ARRAY) + if ( + ARRAY_LENGTH <= 1 + ): # If the array contains only one element, we return it (it's the stop condition of recursion) + return ARRAY + # Else + PIVOT = ARRAY[0] + isFound = False + i = 1 + LONGEST_SUB = [] + while not isFound and i < ARRAY_LENGTH: + if ARRAY[i] < PIVOT: + isFound = True + TEMPORARY_ARRAY = [element for element in ARRAY[i:] if element >= ARRAY[i]] + TEMPORARY_ARRAY = longestSub(TEMPORARY_ARRAY) + if len(TEMPORARY_ARRAY) > len(LONGEST_SUB): + LONGEST_SUB = TEMPORARY_ARRAY + else: + i += 1 + + TEMPORARY_ARRAY = [element for element in ARRAY[1:] if element >= PIVOT] + TEMPORARY_ARRAY = [PIVOT] + longestSub(TEMPORARY_ARRAY) + if len(TEMPORARY_ARRAY) > len(LONGEST_SUB): + return TEMPORARY_ARRAY + else: + return LONGEST_SUB + + +# Some examples + +print(longestSub([4, 8, 7, 5, 1, 12, 2, 3, 9])) +print(longestSub([9, 8, 7, 6, 5, 7])) diff --git a/dynamic_programming/longest_increasing_subsequence_o(nlogn).py b/dynamic_programming/longest_increasing_subsequence_o(nlogn).py index 9b27ed6be303..f7b2c6915bcb 100644 --- a/dynamic_programming/longest_increasing_subsequence_o(nlogn).py +++ b/dynamic_programming/longest_increasing_subsequence_o(nlogn).py @@ -4,38 +4,38 @@ # comments: This programme outputs the Longest Strictly Increasing Subsequence in O(NLogN) # Where N is the Number of elements in the list ############################# -def CeilIndex(v,l,r,key): - while r-l > 1: - m = (l + r)/2 - if v[m] >= key: - r = m - else: - l = m +def CeilIndex(v, l, r, key): + while r - l > 1: + m = (l + r) / 2 + if v[m] >= key: + r = m + else: + l = m - return r + return r def LongestIncreasingSubsequenceLength(v): - if(len(v) == 0): - return 0 + if len(v) == 0: + return 0 - tail = [0]*len(v) - length = 1 + tail = [0] * len(v) + length = 1 - tail[0] = v[0] + tail[0] = v[0] - for i in range(1,len(v)): - if v[i] < tail[0]: - tail[0] = v[i] - elif v[i] > tail[length-1]: - tail[length] = v[i] - length += 1 - else: - tail[CeilIndex(tail,-1,length-1,v[i])] = v[i] + for i in range(1, len(v)): + if v[i] < tail[0]: + tail[0] = v[i] + elif v[i] > tail[length - 1]: + tail[length] = v[i] + length += 1 + else: + tail[CeilIndex(tail, -1, length - 1, v[i])] = v[i] - return length + return length if __name__ == "__main__": - v = [2, 5, 3, 7, 11, 8, 10, 13, 6] - print(LongestIncreasingSubsequenceLength(v)) + v = [2, 5, 3, 7, 11, 8, 10, 13, 6] + print(LongestIncreasingSubsequenceLength(v)) diff --git a/dynamic_programming/longest_sub_array.py b/dynamic_programming/longest_sub_array.py index 856b31f03982..65ce151c33d6 100644 --- a/dynamic_programming/longest_sub_array.py +++ b/dynamic_programming/longest_sub_array.py @@ -1,32 +1,32 @@ -''' +""" Auther : Yvonne This is a pure Python implementation of Dynamic Programming solution to the longest_sub_array problem. The problem is : Given an array, to find the longest and continuous sub array and get the max sum of the sub array in the given array. -''' +""" class SubArray: - def __init__(self, arr): # we need a list not a string, so do something to change the type - self.array = arr.split(',') + self.array = arr.split(",") print(("the input array is:", self.array)) def solve_sub_array(self): - rear = [int(self.array[0])]*len(self.array) - sum_value = [int(self.array[0])]*len(self.array) + rear = [int(self.array[0])] * len(self.array) + sum_value = [int(self.array[0])] * len(self.array) for i in range(1, len(self.array)): - sum_value[i] = max(int(self.array[i]) + sum_value[i-1], int(self.array[i])) - rear[i] = max(sum_value[i], rear[i-1]) - return rear[len(self.array)-1] + sum_value[i] = max( + int(self.array[i]) + sum_value[i - 1], int(self.array[i]) + ) + rear[i] = max(sum_value[i], rear[i - 1]) + return rear[len(self.array) - 1] -if __name__ == '__main__': +if __name__ == "__main__": whole_array = input("please input some numbers:") array = SubArray(whole_array) re = array.solve_sub_array() print(("the results is:", re)) - diff --git a/dynamic_programming/matrix_chain_order.py b/dynamic_programming/matrix_chain_order.py index cb4aec345437..f88a9be8ac95 100644 --- a/dynamic_programming/matrix_chain_order.py +++ b/dynamic_programming/matrix_chain_order.py @@ -1,44 +1,54 @@ import sys -''' + +""" Dynamic Programming Implementation of Matrix Chain Multiplication Time Complexity: O(n^3) Space Complexity: O(n^2) -''' +""" + + def MatrixChainOrder(array): - N=len(array) - Matrix=[[0 for x in range(N)] for x in range(N)] - Sol=[[0 for x in range(N)] for x in range(N)] + N = len(array) + Matrix = [[0 for x in range(N)] for x in range(N)] + Sol = [[0 for x in range(N)] for x in range(N)] - for ChainLength in range(2,N): - for a in range(1,N-ChainLength+1): - b = a+ChainLength-1 + for ChainLength in range(2, N): + for a in range(1, N - ChainLength + 1): + b = a + ChainLength - 1 Matrix[a][b] = sys.maxsize - for c in range(a , b): - cost = Matrix[a][c] + Matrix[c+1][b] + array[a-1]*array[c]*array[b] + for c in range(a, b): + cost = ( + Matrix[a][c] + Matrix[c + 1][b] + array[a - 1] * array[c] * array[b] + ) if cost < Matrix[a][b]: Matrix[a][b] = cost Sol[a][b] = c - return Matrix , Sol -#Print order of matrix with Ai as Matrix -def PrintOptimalSolution(OptimalSolution,i,j): - if i==j: - print("A" + str(i),end = " ") + return Matrix, Sol + + +# Print order of matrix with Ai as Matrix +def PrintOptimalSolution(OptimalSolution, i, j): + if i == j: + print("A" + str(i), end=" ") else: - print("(",end = " ") - PrintOptimalSolution(OptimalSolution,i,OptimalSolution[i][j]) - PrintOptimalSolution(OptimalSolution,OptimalSolution[i][j]+1,j) - print(")",end = " ") + print("(", end=" ") + PrintOptimalSolution(OptimalSolution, i, OptimalSolution[i][j]) + PrintOptimalSolution(OptimalSolution, OptimalSolution[i][j] + 1, j) + print(")", end=" ") + def main(): - array=[30,35,15,5,10,20,25] - n=len(array) - #Size of matrix created from above array will be + array = [30, 35, 15, 5, 10, 20, 25] + n = len(array) + # Size of matrix created from above array will be # 30*35 35*15 15*5 5*10 10*20 20*25 - Matrix , OptimalSolution = MatrixChainOrder(array) + Matrix, OptimalSolution = MatrixChainOrder(array) + + print("No. of Operation required: " + str((Matrix[1][n - 1]))) + PrintOptimalSolution(OptimalSolution, 1, n - 1) + - print("No. of Operation required: "+str((Matrix[1][n-1]))) - PrintOptimalSolution(OptimalSolution,1,n-1) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/dynamic_programming/max_sub_array.py b/dynamic_programming/max_sub_array.py index d6084ecfd6d9..eb6ab41bf52d 100644 --- a/dynamic_programming/max_sub_array.py +++ b/dynamic_programming/max_sub_array.py @@ -5,37 +5,41 @@ import time import matplotlib.pyplot as plt from random import randint -def find_max_sub_array(A,low,high): - if low==high: - return low,high,A[low] - else : - mid=(low+high)//2 - left_low,left_high,left_sum=find_max_sub_array(A,low,mid) - right_low,right_high,right_sum=find_max_sub_array(A,mid+1,high) - cross_left,cross_right,cross_sum=find_max_cross_sum(A,low,mid,high) - if left_sum>=right_sum and left_sum>=cross_sum: - return left_low,left_high,left_sum - elif right_sum>=left_sum and right_sum>=cross_sum : - return right_low,right_high,right_sum + + +def find_max_sub_array(A, low, high): + if low == high: + return low, high, A[low] + else: + mid = (low + high) // 2 + left_low, left_high, left_sum = find_max_sub_array(A, low, mid) + right_low, right_high, right_sum = find_max_sub_array(A, mid + 1, high) + cross_left, cross_right, cross_sum = find_max_cross_sum(A, low, mid, high) + if left_sum >= right_sum and left_sum >= cross_sum: + return left_low, left_high, left_sum + elif right_sum >= left_sum and right_sum >= cross_sum: + return right_low, right_high, right_sum else: - return cross_left,cross_right,cross_sum + return cross_left, cross_right, cross_sum + -def find_max_cross_sum(A,low,mid,high): - left_sum,max_left=-999999999,-1 - right_sum,max_right=-999999999,-1 - summ=0 - for i in range(mid,low-1,-1): - summ+=A[i] +def find_max_cross_sum(A, low, mid, high): + left_sum, max_left = -999999999, -1 + right_sum, max_right = -999999999, -1 + summ = 0 + for i in range(mid, low - 1, -1): + summ += A[i] if summ > left_sum: - left_sum=summ - max_left=i - summ=0 - for i in range(mid+1,high+1): - summ+=A[i] + left_sum = summ + max_left = i + summ = 0 + for i in range(mid + 1, high + 1): + summ += A[i] if summ > right_sum: - right_sum=summ - max_right=i - return max_left,max_right,(left_sum+right_sum) + right_sum = summ + max_right = i + return max_left, max_right, (left_sum + right_sum) + def max_sub_array(nums: List[int]) -> int: """ @@ -58,22 +62,20 @@ def max_sub_array(nums: List[int]) -> int: best = max(best, current) return best -if __name__=='__main__': - inputs=[10,100,1000,10000,50000,100000,200000,300000,400000,500000] - tim=[] + +if __name__ == "__main__": + inputs = [10, 100, 1000, 10000, 50000, 100000, 200000, 300000, 400000, 500000] + tim = [] for i in inputs: - li=[randint(1,i) for j in range(i)] - strt=time.time() - (find_max_sub_array(li,0,len(li)-1)) - end=time.time() - tim.append(end-strt) + li = [randint(1, i) for j in range(i)] + strt = time.time() + (find_max_sub_array(li, 0, len(li) - 1)) + end = time.time() + tim.append(end - strt) print("No of Inputs Time Taken") for i in range(len(inputs)): - print(inputs[i],'\t\t',tim[i]) - plt.plot(inputs,tim) - plt.xlabel("Number of Inputs");plt.ylabel("Time taken in seconds ") + print(inputs[i], "\t\t", tim[i]) + plt.plot(inputs, tim) + plt.xlabel("Number of Inputs") + plt.ylabel("Time taken in seconds ") plt.show() - - - - diff --git a/dynamic_programming/minimum_partition.py b/dynamic_programming/minimum_partition.py index 18aa1faa2fa6..d5750326fea4 100644 --- a/dynamic_programming/minimum_partition.py +++ b/dynamic_programming/minimum_partition.py @@ -1,28 +1,30 @@ """ Partition a set into two subsets such that the difference of subset sums is minimum """ + + def findMin(arr): n = len(arr) s = sum(arr) - dp = [[False for x in range(s+1)]for y in range(n+1)] + dp = [[False for x in range(s + 1)] for y in range(n + 1)] - for i in range(1, n+1): + for i in range(1, n + 1): dp[i][0] = True - for i in range(1, s+1): + for i in range(1, s + 1): dp[0][i] = False - for i in range(1, n+1): - for j in range(1, s+1): - dp[i][j]= dp[i][j-1] + for i in range(1, n + 1): + for j in range(1, s + 1): + dp[i][j] = dp[i][j - 1] - if (arr[i-1] <= j): - dp[i][j] = dp[i][j] or dp[i-1][j-arr[i-1]] + if arr[i - 1] <= j: + dp[i][j] = dp[i][j] or dp[i - 1][j - arr[i - 1]] - for j in range(int(s/2), -1, -1): + for j in range(int(s / 2), -1, -1): if dp[n][j] == True: - diff = s-2*j - break; + diff = s - 2 * j + break return diff diff --git a/dynamic_programming/rod_cutting.py b/dynamic_programming/rod_cutting.py index 5b52eaca7c89..3a1d55320d7b 100644 --- a/dynamic_programming/rod_cutting.py +++ b/dynamic_programming/rod_cutting.py @@ -12,7 +12,7 @@ def naive_cut_rod_recursive(n: int, prices: list): - """ + """ Solves the rod-cutting problem via naively without using the benefit of dynamic programming. The results is the same sub-problems are solved several times leading to an exponential runtime @@ -36,18 +36,20 @@ def naive_cut_rod_recursive(n: int, prices: list): 30 """ - _enforce_args(n, prices) - if n == 0: - return 0 - max_revue = float("-inf") - for i in range(1, n + 1): - max_revue = max(max_revue, prices[i - 1] + naive_cut_rod_recursive(n - i, prices)) + _enforce_args(n, prices) + if n == 0: + return 0 + max_revue = float("-inf") + for i in range(1, n + 1): + max_revue = max( + max_revue, prices[i - 1] + naive_cut_rod_recursive(n - i, prices) + ) - return max_revue + return max_revue def top_down_cut_rod(n: int, prices: list): - """ + """ Constructs a top-down dynamic programming solution for the rod-cutting problem via memoization. This function serves as a wrapper for _top_down_cut_rod_recursive @@ -75,13 +77,13 @@ def top_down_cut_rod(n: int, prices: list): >>> top_down_cut_rod(10, [1, 5, 8, 9, 10, 17, 17, 20, 24, 30]) 30 """ - _enforce_args(n, prices) - max_rev = [float("-inf") for _ in range(n + 1)] - return _top_down_cut_rod_recursive(n, prices, max_rev) + _enforce_args(n, prices) + max_rev = [float("-inf") for _ in range(n + 1)] + return _top_down_cut_rod_recursive(n, prices, max_rev) def _top_down_cut_rod_recursive(n: int, prices: list, max_rev: list): - """ + """ Constructs a top-down dynamic programming solution for the rod-cutting problem via memoization. @@ -99,22 +101,25 @@ def _top_down_cut_rod_recursive(n: int, prices: list, max_rev: list): ------- The maximum revenue obtainable for a rod of length n given the list of prices for each piece. """ - if max_rev[n] >= 0: - return max_rev[n] - elif n == 0: - return 0 - else: - max_revenue = float("-inf") - for i in range(1, n + 1): - max_revenue = max(max_revenue, prices[i - 1] + _top_down_cut_rod_recursive(n - i, prices, max_rev)) + if max_rev[n] >= 0: + return max_rev[n] + elif n == 0: + return 0 + else: + max_revenue = float("-inf") + for i in range(1, n + 1): + max_revenue = max( + max_revenue, + prices[i - 1] + _top_down_cut_rod_recursive(n - i, prices, max_rev), + ) - max_rev[n] = max_revenue + max_rev[n] = max_revenue - return max_rev[n] + return max_rev[n] def bottom_up_cut_rod(n: int, prices: list): - """ + """ Constructs a bottom-up dynamic programming solution for the rod-cutting problem Runtime: O(n^2) @@ -137,24 +142,24 @@ def bottom_up_cut_rod(n: int, prices: list): >>> bottom_up_cut_rod(10, [1, 5, 8, 9, 10, 17, 17, 20, 24, 30]) 30 """ - _enforce_args(n, prices) + _enforce_args(n, prices) - # length(max_rev) = n + 1, to accommodate for the revenue obtainable from a rod of length 0. - max_rev = [float("-inf") for _ in range(n + 1)] - max_rev[0] = 0 + # length(max_rev) = n + 1, to accommodate for the revenue obtainable from a rod of length 0. + max_rev = [float("-inf") for _ in range(n + 1)] + max_rev[0] = 0 - for i in range(1, n + 1): - max_revenue_i = max_rev[i] - for j in range(1, i + 1): - max_revenue_i = max(max_revenue_i, prices[j - 1] + max_rev[i - j]) + for i in range(1, n + 1): + max_revenue_i = max_rev[i] + for j in range(1, i + 1): + max_revenue_i = max(max_revenue_i, prices[j - 1] + max_rev[i - j]) - max_rev[i] = max_revenue_i + max_rev[i] = max_revenue_i - return max_rev[n] + return max_rev[n] def _enforce_args(n: int, prices: list): - """ + """ Basic checks on the arguments to the rod-cutting algorithms n: int, the length of the rod @@ -164,30 +169,32 @@ def _enforce_args(n: int, prices: list): if n is negative or there are fewer items in the price list than the length of the rod """ - if n < 0: - raise ValueError(f"n must be greater than or equal to 0. Got n = {n}") + if n < 0: + raise ValueError(f"n must be greater than or equal to 0. Got n = {n}") - if n > len(prices): - raise ValueError(f"Each integral piece of rod must have a corresponding " - f"price. Got n = {n} but length of prices = {len(prices)}") + if n > len(prices): + raise ValueError( + f"Each integral piece of rod must have a corresponding " + f"price. Got n = {n} but length of prices = {len(prices)}" + ) def main(): - prices = [6, 10, 12, 15, 20, 23] - n = len(prices) + prices = [6, 10, 12, 15, 20, 23] + n = len(prices) - # the best revenue comes from cutting the rod into 6 pieces, each - # of length 1 resulting in a revenue of 6 * 6 = 36. - expected_max_revenue = 36 + # the best revenue comes from cutting the rod into 6 pieces, each + # of length 1 resulting in a revenue of 6 * 6 = 36. + expected_max_revenue = 36 - max_rev_top_down = top_down_cut_rod(n, prices) - max_rev_bottom_up = bottom_up_cut_rod(n, prices) - max_rev_naive = naive_cut_rod_recursive(n, prices) + max_rev_top_down = top_down_cut_rod(n, prices) + max_rev_bottom_up = bottom_up_cut_rod(n, prices) + max_rev_naive = naive_cut_rod_recursive(n, prices) - assert expected_max_revenue == max_rev_top_down - assert max_rev_top_down == max_rev_bottom_up - assert max_rev_bottom_up == max_rev_naive + assert expected_max_revenue == max_rev_top_down + assert max_rev_top_down == max_rev_bottom_up + assert max_rev_bottom_up == max_rev_naive -if __name__ == '__main__': - main() +if __name__ == "__main__": + main() diff --git a/dynamic_programming/subset_generation.py b/dynamic_programming/subset_generation.py index 4b7a2bf87fd5..2cca97fc3cbc 100644 --- a/dynamic_programming/subset_generation.py +++ b/dynamic_programming/subset_generation.py @@ -1,39 +1,43 @@ # python program to print all subset combination of n element in given set of r element . -#arr[] ---> Input Array -#data[] ---> Temporary array to store current combination +# arr[] ---> Input Array +# data[] ---> Temporary array to store current combination # start & end ---> Staring and Ending indexes in arr[] # index ---> Current index in data[] -#r ---> Size of a combination to be printed -def combinationUtil(arr,n,r,index,data,i): -#Current combination is ready to be printed, -# print it - if(index == r): - for j in range(r): - print(data[j],end =" ") - print(" ") - return -# When no more elements are there to put in data[] - if(i >= n): - return -#current is included, put next at next -# location - data[index] = arr[i] - combinationUtil(arr,n,r,index+1,data,i+1) - # current is excluded, replace it with - # next (Note that i+1 is passed, but - # index is not changed) - combinationUtil(arr,n,r,index,data,i+1) - # The main function that prints all combinations - #of size r in arr[] of size n. This function - #mainly uses combinationUtil() -def printcombination(arr,n,r): -# A temporary array to store all combination -# one by one - data = [0]*r -#Print all combination using temprary -#array 'data[]' - combinationUtil(arr,n,r,0,data,0) +# r ---> Size of a combination to be printed +def combinationUtil(arr, n, r, index, data, i): + # Current combination is ready to be printed, + # print it + if index == r: + for j in range(r): + print(data[j], end=" ") + print(" ") + return + # When no more elements are there to put in data[] + if i >= n: + return + # current is included, put next at next + # location + data[index] = arr[i] + combinationUtil(arr, n, r, index + 1, data, i + 1) + # current is excluded, replace it with + # next (Note that i+1 is passed, but + # index is not changed) + combinationUtil(arr, n, r, index, data, i + 1) + # The main function that prints all combinations + # of size r in arr[] of size n. This function + # mainly uses combinationUtil() + + +def printcombination(arr, n, r): + # A temporary array to store all combination + # one by one + data = [0] * r + # Print all combination using temprary + # array 'data[]' + combinationUtil(arr, n, r, 0, data, 0) + + # Driver function to check for above function -arr = [10,20,30,40,50] -printcombination(arr,len(arr),3) -#This code is contributed by Ambuj sahu +arr = [10, 20, 30, 40, 50] +printcombination(arr, len(arr), 3) +# This code is contributed by Ambuj sahu diff --git a/dynamic_programming/sum_of_subset.py b/dynamic_programming/sum_of_subset.py index f6509a259c5d..581039080101 100644 --- a/dynamic_programming/sum_of_subset.py +++ b/dynamic_programming/sum_of_subset.py @@ -1,34 +1,35 @@ def isSumSubset(arr, arrLen, requiredSum): # a subset value says 1 if that subset sum can be formed else 0 - #initially no subsets can be formed hence False/0 - subset = ([[False for i in range(requiredSum + 1)] for i in range(arrLen + 1)]) + # initially no subsets can be formed hence False/0 + subset = [[False for i in range(requiredSum + 1)] for i in range(arrLen + 1)] - #for each arr value, a sum of zero(0) can be formed by not taking any element hence True/1 + # for each arr value, a sum of zero(0) can be formed by not taking any element hence True/1 for i in range(arrLen + 1): subset[i][0] = True - #sum is not zero and set is empty then false + # sum is not zero and set is empty then false for i in range(1, requiredSum + 1): subset[0][i] = False for i in range(1, arrLen + 1): for j in range(1, requiredSum + 1): - if arr[i-1]>j: - subset[i][j] = subset[i-1][j] - if arr[i-1]<=j: - subset[i][j] = (subset[i-1][j] or subset[i-1][j-arr[i-1]]) + if arr[i - 1] > j: + subset[i][j] = subset[i - 1][j] + if arr[i - 1] <= j: + subset[i][j] = subset[i - 1][j] or subset[i - 1][j - arr[i - 1]] - #uncomment to print the subset + # uncomment to print the subset # for i in range(arrLen+1): # print(subset[i]) return subset[arrLen][requiredSum] + arr = [2, 4, 6, 8] -requiredSum = 5 +requiredSum = 5 arrLen = len(arr) if isSumSubset(arr, arrLen, requiredSum): print("Found a subset with required sum") else: - print("No subset with required sum") \ No newline at end of file + print("No subset with required sum") diff --git a/file_transfer/recieve_file.py b/file_transfer/recieve_file.py index f404546d7765..cfba6ed88484 100644 --- a/file_transfer/recieve_file.py +++ b/file_transfer/recieve_file.py @@ -1,4 +1,4 @@ -if __name__ == '__main__': +if __name__ == "__main__": import socket # Import socket module sock = socket.socket() # Create a socket object @@ -6,11 +6,11 @@ port = 12312 sock.connect((host, port)) - sock.send(b'Hello server!') + sock.send(b"Hello server!") - with open('Received_file', 'wb') as out_file: - print('File opened') - print('Receiving data...') + with open("Received_file", "wb") as out_file: + print("File opened") + print("Receiving data...") while True: data = sock.recv(1024) print(f"data={data}") @@ -18,6 +18,6 @@ break out_file.write(data) # Write data to a file - print('Successfully got the file') + print("Successfully got the file") sock.close() - print('Connection closed') + print("Connection closed") diff --git a/file_transfer/send_file.py b/file_transfer/send_file.py index 92fab206c1a1..ebc075a30ad4 100644 --- a/file_transfer/send_file.py +++ b/file_transfer/send_file.py @@ -1,16 +1,18 @@ -if __name__ == '__main__': +if __name__ == "__main__": import socket # Import socket module - ONE_CONNECTION_ONLY = True # Set this to False if you wish to continuously accept connections + ONE_CONNECTION_ONLY = ( + True + ) # Set this to False if you wish to continuously accept connections - filename='mytext.txt' + filename = "mytext.txt" port = 12312 # Reserve a port for your service. sock = socket.socket() # Create a socket object host = socket.gethostname() # Get local machine name sock.bind((host, port)) # Bind to the port sock.listen(5) # Now wait for client connection. - print('Server listening....') + print("Server listening....") while True: conn, addr = sock.accept() # Establish connection with client. @@ -18,16 +20,18 @@ data = conn.recv(1024) print(f"Server received {data}") - with open(filename,'rb') as in_file: + with open(filename, "rb") as in_file: data = in_file.read(1024) - while (data): - conn.send(data) - print(f"Sent {data!r}") - data = in_file.read(1024) + while data: + conn.send(data) + print(f"Sent {data!r}") + data = in_file.read(1024) - print('Done sending') + print("Done sending") conn.close() - if ONE_CONNECTION_ONLY: # This is to make sure that the program doesn't hang while testing + if ( + ONE_CONNECTION_ONLY + ): # This is to make sure that the program doesn't hang while testing break sock.shutdown(1) diff --git a/graphs/a_star.py b/graphs/a_star.py index 09a7a0e579d8..e1d17fc55434 100644 --- a/graphs/a_star.py +++ b/graphs/a_star.py @@ -1,42 +1,45 @@ -grid = [[0, 1, 0, 0, 0, 0], - [0, 1, 0, 0, 0, 0],#0 are free path whereas 1's are obstacles - [0, 1, 0, 0, 0, 0], - [0, 1, 0, 0, 1, 0], - [0, 0, 0, 0, 1, 0]] - -''' +grid = [ + [0, 1, 0, 0, 0, 0], + [0, 1, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles + [0, 1, 0, 0, 0, 0], + [0, 1, 0, 0, 1, 0], + [0, 0, 0, 0, 1, 0], +] + +""" heuristic = [[9, 8, 7, 6, 5, 4], [8, 7, 6, 5, 4, 3], [7, 6, 5, 4, 3, 2], [6, 5, 4, 3, 2, 1], - [5, 4, 3, 2, 1, 0]]''' + [5, 4, 3, 2, 1, 0]]""" init = [0, 0] -goal = [len(grid)-1, len(grid[0])-1] #all coordinates are given in format [y,x] +goal = [len(grid) - 1, len(grid[0]) - 1] # all coordinates are given in format [y,x] cost = 1 -#the cost map which pushes the path closer to the goal +# the cost map which pushes the path closer to the goal heuristic = [[0 for row in range(len(grid[0]))] for col in range(len(grid))] for i in range(len(grid)): for j in range(len(grid[0])): heuristic[i][j] = abs(i - goal[0]) + abs(j - goal[1]) if grid[i][j] == 1: - heuristic[i][j] = 99 #added extra penalty in the heuristic map + heuristic[i][j] = 99 # added extra penalty in the heuristic map -#the actions we can take -delta = [[-1, 0 ], # go up - [ 0, -1], # go left - [ 1, 0 ], # go down - [ 0, 1 ]] # go right +# the actions we can take +delta = [[-1, 0], [0, -1], [1, 0], [0, 1]] # go up # go left # go down # go right -#function to search the path -def search(grid,init,goal,cost,heuristic): +# function to search the path +def search(grid, init, goal, cost, heuristic): - closed = [[0 for col in range(len(grid[0]))] for row in range(len(grid))]# the referrence grid + closed = [ + [0 for col in range(len(grid[0]))] for row in range(len(grid)) + ] # the referrence grid closed[init[0]][init[1]] = 1 - action = [[0 for col in range(len(grid[0]))] for row in range(len(grid))]#the action grid + action = [ + [0 for col in range(len(grid[0]))] for row in range(len(grid)) + ] # the action grid x = init[0] y = init[1] @@ -45,14 +48,14 @@ def search(grid,init,goal,cost,heuristic): cell = [[f, g, x, y]] found = False # flag that is set when search is complete - resign = False # flag set if we can't find expand + resign = False # flag set if we can't find expand while not found and not resign: if len(cell) == 0: resign = True return "FAIL" else: - cell.sort()#to choose the least costliest action so as to move closer to the goal + cell.sort() # to choose the least costliest action so as to move closer to the goal cell.reverse() next = cell.pop() x = next[2] @@ -60,14 +63,13 @@ def search(grid,init,goal,cost,heuristic): g = next[1] f = next[0] - if x == goal[0] and y == goal[1]: found = True else: - for i in range(len(delta)):#to try out different valid actions + for i in range(len(delta)): # to try out different valid actions x2 = x + delta[i][0] y2 = y + delta[i][1] - if x2 >= 0 and x2 < len(grid) and y2 >=0 and y2 < len(grid[0]): + if x2 >= 0 and x2 < len(grid) and y2 >= 0 and y2 < len(grid[0]): if closed[x2][y2] == 0 and grid[x2][y2] == 0: g2 = g + cost f2 = g2 + heuristic[x2][y2] @@ -77,7 +79,7 @@ def search(grid,init,goal,cost,heuristic): invpath = [] x = goal[0] y = goal[1] - invpath.append([x, y])#we get the reverse path from here + invpath.append([x, y]) # we get the reverse path from here while x != init[0] or y != init[1]: x2 = x - delta[action[x][y]][0] y2 = y - delta[action[x][y]][1] @@ -87,14 +89,14 @@ def search(grid,init,goal,cost,heuristic): path = [] for i in range(len(invpath)): - path.append(invpath[len(invpath) - 1 - i]) + path.append(invpath[len(invpath) - 1 - i]) print("ACTION MAP") for i in range(len(action)): print(action[i]) return path -a = search(grid,init,goal,cost,heuristic) -for i in range(len(a)): - print(a[i]) +a = search(grid, init, goal, cost, heuristic) +for i in range(len(a)): + print(a[i]) diff --git a/graphs/articulation_points.py b/graphs/articulation_points.py index 1173c4ea373c..3ecc829946e8 100644 --- a/graphs/articulation_points.py +++ b/graphs/articulation_points.py @@ -33,12 +33,23 @@ def dfs(root, at, parent, outEdgeCount): if not visited[i]: outEdgeCount = 0 outEdgeCount = dfs(i, i, -1, outEdgeCount) - isArt[i] = (outEdgeCount > 1) + isArt[i] = outEdgeCount > 1 for x in range(len(isArt)): if isArt[x] == True: print(x) + # Adjacency list of graph -l = {0:[1,2], 1:[0,2], 2:[0,1,3,5], 3:[2,4], 4:[3], 5:[2,6,8], 6:[5,7], 7:[6,8], 8:[5,7]} +l = { + 0: [1, 2], + 1: [0, 2], + 2: [0, 1, 3, 5], + 3: [2, 4], + 4: [3], + 5: [2, 6, 8], + 6: [5, 7], + 7: [6, 8], + 8: [5, 7], +} computeAP(l) diff --git a/graphs/basic_graphs.py b/graphs/basic_graphs.py index 308abc0839fa..161bc0c09d3b 100644 --- a/graphs/basic_graphs.py +++ b/graphs/basic_graphs.py @@ -237,7 +237,7 @@ def edglist(): n, m = map(int, input().split(" ")) l = [] for i in range(m): - l.append(map(int, input().split(' '))) + l.append(map(int, input().split(" "))) return l, n diff --git a/graphs/bellman_ford.py b/graphs/bellman_ford.py index bebe8f354b26..b782a899fda9 100644 --- a/graphs/bellman_ford.py +++ b/graphs/bellman_ford.py @@ -1,35 +1,35 @@ def printDist(dist, V): - print("\nVertex Distance") - for i in range(V): - if dist[i] != float('inf') : - print(i,"\t",int(dist[i]),end = "\t") - else: - print(i,"\t","INF",end="\t") - print() + print("\nVertex Distance") + for i in range(V): + if dist[i] != float("inf"): + print(i, "\t", int(dist[i]), end="\t") + else: + print(i, "\t", "INF", end="\t") + print() -def BellmanFord(graph, V, E, src): - mdist=[float('inf') for i in range(V)] - mdist[src] = 0.0 - for i in range(V-1): - for j in range(V): - u = graph[j]["src"] - v = graph[j]["dst"] - w = graph[j]["weight"] +def BellmanFord(graph, V, E, src): + mdist = [float("inf") for i in range(V)] + mdist[src] = 0.0 - if mdist[u] != float('inf') and mdist[u] + w < mdist[v]: - mdist[v] = mdist[u] + w - for j in range(V): - u = graph[j]["src"] - v = graph[j]["dst"] - w = graph[j]["weight"] + for i in range(V - 1): + for j in range(V): + u = graph[j]["src"] + v = graph[j]["dst"] + w = graph[j]["weight"] - if mdist[u] != float('inf') and mdist[u] + w < mdist[v]: - print("Negative cycle found. Solution not possible.") - return + if mdist[u] != float("inf") and mdist[u] + w < mdist[v]: + mdist[v] = mdist[u] + w + for j in range(V): + u = graph[j]["src"] + v = graph[j]["dst"] + w = graph[j]["weight"] - printDist(mdist, V) + if mdist[u] != float("inf") and mdist[u] + w < mdist[v]: + print("Negative cycle found. Solution not possible.") + return + printDist(mdist, V) if __name__ == "__main__": @@ -39,14 +39,14 @@ def BellmanFord(graph, V, E, src): graph = [dict() for j in range(E)] for i in range(V): - graph[i][i] = 0.0 + graph[i][i] = 0.0 for i in range(E): - print("\nEdge ",i+1) - src = int(input("Enter source:").strip()) - dst = int(input("Enter destination:").strip()) - weight = float(input("Enter weight:").strip()) - graph[i] = {"src": src,"dst": dst, "weight": weight} + print("\nEdge ", i + 1) + src = int(input("Enter source:").strip()) + dst = int(input("Enter destination:").strip()) + weight = float(input("Enter weight:").strip()) + graph[i] = {"src": src, "dst": dst, "weight": weight} gsrc = int(input("\nEnter shortest path source:").strip()) BellmanFord(graph, V, E, gsrc) diff --git a/graphs/bfs.py b/graphs/bfs.py index ebbde0c82ce6..9d9b1ac037d9 100644 --- a/graphs/bfs.py +++ b/graphs/bfs.py @@ -16,12 +16,14 @@ """ -G = {'A': ['B', 'C'], - 'B': ['A', 'D', 'E'], - 'C': ['A', 'F'], - 'D': ['B'], - 'E': ['B', 'F'], - 'F': ['C', 'E']} +G = { + "A": ["B", "C"], + "B": ["A", "D", "E"], + "C": ["A", "F"], + "D": ["B"], + "E": ["B", "F"], + "F": ["C", "E"], +} def bfs(graph, start): @@ -40,5 +42,5 @@ def bfs(graph, start): return explored -if __name__ == '__main__': - print(bfs(G, 'A')) +if __name__ == "__main__": + print(bfs(G, "A")) diff --git a/graphs/bfs_shortest_path.py b/graphs/bfs_shortest_path.py index 5853351a53a3..ec82c13997e2 100644 --- a/graphs/bfs_shortest_path.py +++ b/graphs/bfs_shortest_path.py @@ -1,21 +1,24 @@ -graph = {'A': ['B', 'C', 'E'], - 'B': ['A','D', 'E'], - 'C': ['A', 'F', 'G'], - 'D': ['B'], - 'E': ['A', 'B','D'], - 'F': ['C'], - 'G': ['C']} +graph = { + "A": ["B", "C", "E"], + "B": ["A", "D", "E"], + "C": ["A", "F", "G"], + "D": ["B"], + "E": ["A", "B", "D"], + "F": ["C"], + "G": ["C"], +} + def bfs_shortest_path(graph, start, goal): # keep track of explored nodes explored = [] # keep track of all the paths to be checked queue = [[start]] - + # return path if start is goal if start == goal: return "That was easy! Start = goal" - + # keeps looping until all possible paths have been checked while queue: # pop the first path from the queue @@ -33,11 +36,12 @@ def bfs_shortest_path(graph, start, goal): # return path if neighbour is goal if neighbour == goal: return new_path - + # mark node as explored explored.append(node) - + # in case there's no path between the 2 nodes return "So sorry, but a connecting path doesn't exist :(" - -bfs_shortest_path(graph, 'G', 'D') # returns ['G', 'C', 'A', 'B', 'D'] + + +bfs_shortest_path(graph, "G", "D") # returns ['G', 'C', 'A', 'B', 'D'] diff --git a/graphs/breadth_first_search.py b/graphs/breadth_first_search.py index 205f49a6172b..8516e60a59c4 100644 --- a/graphs/breadth_first_search.py +++ b/graphs/breadth_first_search.py @@ -4,14 +4,14 @@ """ Author: OMKAR PATHAK """ -class Graph(): +class Graph: def __init__(self): self.vertex = {} # for printing the Graph vertexes def printGraph(self): for i in self.vertex.keys(): - print(i,' -> ', ' -> '.join([str(j) for j in self.vertex[i]])) + print(i, " -> ", " -> ".join([str(j) for j in self.vertex[i]])) # for adding the edge beween two vertexes def addEdge(self, fromVertex, toVertex): @@ -35,7 +35,7 @@ def BFS(self, startVertex): while queue: startVertex = queue.pop(0) - print(startVertex, end = ' ') + print(startVertex, end=" ") # mark all adjacent nodes as visited and print them for i in self.vertex[startVertex]: @@ -43,7 +43,8 @@ def BFS(self, startVertex): queue.append(i) visited[i] = True -if __name__ == '__main__': + +if __name__ == "__main__": g = Graph() g.addEdge(0, 1) g.addEdge(0, 2) @@ -53,7 +54,7 @@ def BFS(self, startVertex): g.addEdge(3, 3) g.printGraph() - print('BFS:') + print("BFS:") g.BFS(2) # OUTPUT: diff --git a/graphs/check_bipartite_graph_bfs.py b/graphs/check_bipartite_graph_bfs.py index 1b9c32c6ccc4..1ec3e3d1d45f 100644 --- a/graphs/check_bipartite_graph_bfs.py +++ b/graphs/check_bipartite_graph_bfs.py @@ -11,7 +11,7 @@ def checkBipartite(l): color = [-1] * len(l) def bfs(): - while(queue): + while queue: u = queue.pop(0) visited[u] = True @@ -38,6 +38,7 @@ def bfs(): return True + # Adjacency List of graph -l = {0:[1,3], 1:[0,2], 2:[1,3], 3:[0,2]} +l = {0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2]} print(checkBipartite(l)) diff --git a/graphs/check_bipartite_graph_dfs.py b/graphs/check_bipartite_graph_dfs.py index eeb3a84b7a15..6fe54a6723c5 100644 --- a/graphs/check_bipartite_graph_dfs.py +++ b/graphs/check_bipartite_graph_dfs.py @@ -26,8 +26,8 @@ def dfs(v, c): return False return True - + # Adjacency list of graph -l = {0:[1,3], 1:[0,2], 2:[1,3], 3:[0,2], 4: []} +l = {0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 4: []} print(check_bipartite_dfs(l)) diff --git a/graphs/depth_first_search.py b/graphs/depth_first_search.py index 2b03683c0047..5347c2fbcfa3 100644 --- a/graphs/depth_first_search.py +++ b/graphs/depth_first_search.py @@ -4,7 +4,7 @@ """ Author: OMKAR PATHAK """ -class Graph(): +class Graph: def __init__(self): self.vertex = {} @@ -12,7 +12,7 @@ def __init__(self): def printGraph(self): print(self.vertex) for i in self.vertex.keys(): - print(i,' -> ', ' -> '.join([str(j) for j in self.vertex[i]])) + print(i, " -> ", " -> ".join([str(j) for j in self.vertex[i]])) # for adding the edge beween two vertexes def addEdge(self, fromVertex, toVertex): @@ -36,14 +36,15 @@ def DFSRec(self, startVertex, visited): # mark start vertex as visited visited[startVertex] = True - print(startVertex, end = ' ') + print(startVertex, end=" ") # Recur for all the vertexes that are adjacent to this node for i in self.vertex.keys(): if visited[i] == False: self.DFSRec(i, visited) -if __name__ == '__main__': + +if __name__ == "__main__": g = Graph() g.addEdge(0, 1) g.addEdge(0, 2) @@ -53,7 +54,7 @@ def DFSRec(self, startVertex, visited): g.addEdge(3, 3) g.printGraph() - print('DFS:') + print("DFS:") g.DFS() # OUTPUT: @@ -62,4 +63,4 @@ def DFSRec(self, startVertex, visited): # 2  ->  0 -> 3 # 3  ->  3 # DFS: - # 0 1 2 3 + #  0 1 2 3 diff --git a/graphs/dfs.py b/graphs/dfs.py index 68bf60e3c298..f183eae73fef 100644 --- a/graphs/dfs.py +++ b/graphs/dfs.py @@ -17,8 +17,10 @@ def dfs(graph, start): it off the stack.""" explored, stack = set(), [start] while stack: - v = stack.pop() # one difference from BFS is to pop last element here instead of first one - + v = ( + stack.pop() + ) # one difference from BFS is to pop last element here instead of first one + if v in explored: continue @@ -30,11 +32,13 @@ def dfs(graph, start): return explored -G = {'A': ['B', 'C'], - 'B': ['A', 'D', 'E'], - 'C': ['A', 'F'], - 'D': ['B'], - 'E': ['B', 'F'], - 'F': ['C', 'E']} +G = { + "A": ["B", "C"], + "B": ["A", "D", "E"], + "C": ["A", "F"], + "D": ["B"], + "E": ["B", "F"], + "F": ["C", "E"], +} -print(dfs(G, 'A')) +print(dfs(G, "A")) diff --git a/graphs/dijkstra.py b/graphs/dijkstra.py index 5f09a45cf2c4..195f4e02d409 100644 --- a/graphs/dijkstra.py +++ b/graphs/dijkstra.py @@ -115,4 +115,5 @@ def dijkstra(graph, start, end): if __name__ == "__main__": import doctest + doctest.testmod() diff --git a/graphs/dijkstra_2.py b/graphs/dijkstra_2.py index f6118830c9c0..762884136e4a 100644 --- a/graphs/dijkstra_2.py +++ b/graphs/dijkstra_2.py @@ -1,55 +1,58 @@ def printDist(dist, V): - print("\nVertex Distance") - for i in range(V): - if dist[i] != float('inf') : - print(i,"\t",int(dist[i]),end = "\t") - else: - print(i,"\t","INF",end="\t") - print() - -def minDist(mdist, vset, V): - minVal = float('inf') - minInd = -1 - for i in range(V): - if (not vset[i]) and mdist[i] < minVal : - minInd = i - minVal = mdist[i] - return minInd + print("\nVertex Distance") + for i in range(V): + if dist[i] != float("inf"): + print(i, "\t", int(dist[i]), end="\t") + else: + print(i, "\t", "INF", end="\t") + print() -def Dijkstra(graph, V, src): - mdist=[float('inf') for i in range(V)] - vset = [False for i in range(V)] - mdist[src] = 0.0 - for i in range(V-1): - u = minDist(mdist, vset, V) - vset[u] = True +def minDist(mdist, vset, V): + minVal = float("inf") + minInd = -1 + for i in range(V): + if (not vset[i]) and mdist[i] < minVal: + minInd = i + minVal = mdist[i] + return minInd - for v in range(V): - if (not vset[v]) and graph[u][v]!=float('inf') and mdist[u] + graph[u][v] < mdist[v]: - mdist[v] = mdist[u] + graph[u][v] +def Dijkstra(graph, V, src): + mdist = [float("inf") for i in range(V)] + vset = [False for i in range(V)] + mdist[src] = 0.0 + for i in range(V - 1): + u = minDist(mdist, vset, V) + vset[u] = True - printDist(mdist, V) + for v in range(V): + if ( + (not vset[v]) + and graph[u][v] != float("inf") + and mdist[u] + graph[u][v] < mdist[v] + ): + mdist[v] = mdist[u] + graph[u][v] + printDist(mdist, V) if __name__ == "__main__": V = int(input("Enter number of vertices: ").strip()) E = int(input("Enter number of edges: ").strip()) - graph = [[float('inf') for i in range(V)] for j in range(V)] + graph = [[float("inf") for i in range(V)] for j in range(V)] for i in range(V): - graph[i][i] = 0.0 + graph[i][i] = 0.0 for i in range(E): - print("\nEdge ",i+1) - src = int(input("Enter source:").strip()) - dst = int(input("Enter destination:").strip()) - weight = float(input("Enter weight:").strip()) - graph[src][dst] = weight + print("\nEdge ", i + 1) + src = int(input("Enter source:").strip()) + dst = int(input("Enter destination:").strip()) + weight = float(input("Enter weight:").strip()) + graph[src][dst] = weight gsrc = int(input("\nEnter shortest path source:").strip()) Dijkstra(graph, V, gsrc) diff --git a/graphs/dijkstra_algorithm.py b/graphs/dijkstra_algorithm.py index c43ff37f5336..9304a83148f3 100644 --- a/graphs/dijkstra_algorithm.py +++ b/graphs/dijkstra_algorithm.py @@ -4,6 +4,7 @@ import math import sys + # For storing the vertex set to retreive node with the lowest distance @@ -12,7 +13,7 @@ class PriorityQueue: def __init__(self): self.cur_size = 0 self.array = [] - self.pos = {} # To store the pos of node in array + self.pos = {} # To store the pos of node in array def isEmpty(self): return self.cur_size == 0 @@ -78,8 +79,8 @@ def decrease_key(self, tup, new_d): class Graph: def __init__(self, num): - self.adjList = {} # To store graph: u -> (v,w) - self.num_nodes = num # Number of nodes in graph + self.adjList = {} # To store graph: u -> (v,w) + self.num_nodes = num # Number of nodes in graph # To store the distance from source vertex self.dist = [0] * self.num_nodes self.par = [-1] * self.num_nodes # To store the path @@ -102,8 +103,11 @@ def add_edge(self, u, v, w): def show_graph(self): # u -> v(w) for u in self.adjList: - print(u, '->', ' -> '.join(str("{}({})".format(v, w)) - for v, w in self.adjList[u])) + print( + u, + "->", + " -> ".join(str("{}({})".format(v, w)) for v, w in self.adjList[u]), + ) def dijkstra(self, src): # Flush old junk values in par[] @@ -137,7 +141,7 @@ def dijkstra(self, src): def show_distances(self, src): print("Distance from node: {}".format(src)) for u in range(self.num_nodes): - print('Node {} has distance: {}'.format(u, self.dist[u])) + print("Node {} has distance: {}".format(u, self.dist[u])) def show_path(self, src, dest): # To show the shortest path from src to dest @@ -157,16 +161,16 @@ def show_path(self, src, dest): path.append(src) path.reverse() - print('----Path to reach {} from {}----'.format(dest, src)) + print("----Path to reach {} from {}----".format(dest, src)) for u in path: - print('{}'.format(u), end=' ') + print("{}".format(u), end=" ") if u != dest: - print('-> ', end='') + print("-> ", end="") - print('\nTotal cost of path: ', cost) + print("\nTotal cost of path: ", cost) -if __name__ == '__main__': +if __name__ == "__main__": graph = Graph(9) graph.add_edge(0, 1, 4) graph.add_edge(0, 7, 8) diff --git a/graphs/directed_and_undirected_(weighted)_graph.py b/graphs/directed_and_undirected_(weighted)_graph.py index a31a4a96d6d0..883a8a00c6b1 100644 --- a/graphs/directed_and_undirected_(weighted)_graph.py +++ b/graphs/directed_and_undirected_(weighted)_graph.py @@ -5,468 +5,493 @@ # the dfault weight is 1 if not assigend but all the implementation is weighted + class DirectedGraph: - def __init__(self): - self.graph = {} - - # adding vertices and edges - # adding the weight is optional - # handels repetition - def add_pair(self, u, v, w = 1): - if self.graph.get(u): - if self.graph[u].count([w,v]) == 0: - self.graph[u].append([w, v]) - else: - self.graph[u] = [[w, v]] - if not self.graph.get(v): - self.graph[v] = [] - - def all_nodes(self): - return list(self.graph) - - # handels if the input does not exist - def remove_pair(self, u, v): - if self.graph.get(u): - for _ in self.graph[u]: - if _[1] == v: - self.graph[u].remove(_) - - # if no destination is meant the defaut value is -1 - def dfs(self, s = -2, d = -1): - if s == d: - return [] - stack = [] - visited = [] - if s == -2: - s = list(self.graph.keys())[0] - stack.append(s) - visited.append(s) - ss = s - - while True: - # check if there is any non isolated nodes - if len(self.graph[s]) != 0: - ss = s - for __ in self.graph[s]: - if visited.count(__[1]) < 1: - if __[1] == d: - visited.append(d) - return visited - else: - stack.append(__[1]) - visited.append(__[1]) - ss =__[1] - break - - # check if all the children are visited - if s == ss : - stack.pop() - if len(stack) != 0: - s = stack[len(stack) - 1] - else: - s = ss - - # check if se have reached the starting point - if len(stack) == 0: - return visited - - # c is the count of nodes you want and if you leave it or pass -1 to the funtion the count - # will be random from 10 to 10000 - def fill_graph_randomly(self, c = -1): - if c == -1: - c = (math.floor(rand.random() * 10000)) + 10 - for _ in range(c): - # every vertex has max 100 edges - e = math.floor(rand.random() * 102) + 1 - for __ in range(e): - n = math.floor(rand.random() * (c)) + 1 - if n == _: - continue - self.add_pair(_, n, 1) - - def bfs(self, s = -2): - d = deque() - visited = [] - if s == -2: - s = list(self.graph.keys())[0] - d.append(s) - visited.append(s) - while d: - s = d.popleft() - if len(self.graph[s]) != 0: - for __ in self.graph[s]: - if visited.count(__[1]) < 1: - d.append(__[1]) - visited.append(__[1]) - return visited - def in_degree(self, u): - count = 0 - for _ in self.graph: - for __ in self.graph[_]: - if __[1] == u: - count += 1 - return count - - def out_degree(self, u): - return len(self.graph[u]) - - def topological_sort(self, s = -2): - stack = [] - visited = [] - if s == -2: - s = list(self.graph.keys())[0] - stack.append(s) - visited.append(s) - ss = s - sorted_nodes = [] - - while True: - # check if there is any non isolated nodes - if len(self.graph[s]) != 0: - ss = s - for __ in self.graph[s]: - if visited.count(__[1]) < 1: - stack.append(__[1]) - visited.append(__[1]) - ss =__[1] - break - - # check if all the children are visited - if s == ss : - sorted_nodes.append(stack.pop()) - if len(stack) != 0: - s = stack[len(stack) - 1] - else: - s = ss - - # check if se have reached the starting point - if len(stack) == 0: - return sorted_nodes - - def cycle_nodes(self): - stack = [] - visited = [] - s = list(self.graph.keys())[0] - stack.append(s) - visited.append(s) - parent = -2 - indirect_parents = [] - ss = s - on_the_way_back = False - anticipating_nodes = set() - - while True: - # check if there is any non isolated nodes - if len(self.graph[s]) != 0: - ss = s - for __ in self.graph[s]: - if visited.count(__[1]) > 0 and __[1] != parent and indirect_parents.count(__[1]) > 0 and not on_the_way_back: - l = len(stack) - 1 - while True and l >= 0: - if stack[l] == __[1]: - anticipating_nodes.add(__[1]) - break - else: - anticipating_nodes.add(stack[l]) - l -= 1 - if visited.count(__[1]) < 1: - stack.append(__[1]) - visited.append(__[1]) - ss =__[1] - break - - # check if all the children are visited - if s == ss : - stack.pop() - on_the_way_back = True - if len(stack) != 0: - s = stack[len(stack) - 1] - else: - on_the_way_back = False - indirect_parents.append(parent) - parent = s - s = ss - - # check if se have reached the starting point - if len(stack) == 0: - return list(anticipating_nodes) - - def has_cycle(self): - stack = [] - visited = [] - s = list(self.graph.keys())[0] - stack.append(s) - visited.append(s) - parent = -2 - indirect_parents = [] - ss = s - on_the_way_back = False - anticipating_nodes = set() - - while True: - # check if there is any non isolated nodes - if len(self.graph[s]) != 0: - ss = s - for __ in self.graph[s]: - if visited.count(__[1]) > 0 and __[1] != parent and indirect_parents.count(__[1]) > 0 and not on_the_way_back: - l = len(stack) - 1 - while True and l >= 0: - if stack[l] == __[1]: - anticipating_nodes.add(__[1]) - break - else: - return True - anticipating_nodes.add(stack[l]) - l -= 1 - if visited.count(__[1]) < 1: - stack.append(__[1]) - visited.append(__[1]) - ss =__[1] - break - - # check if all the children are visited - if s == ss : - stack.pop() - on_the_way_back = True - if len(stack) != 0: - s = stack[len(stack) - 1] - else: - on_the_way_back = False - indirect_parents.append(parent) - parent = s - s = ss - - # check if se have reached the starting point - if len(stack) == 0: - return False - - def dfs_time(self, s = -2, e = -1): - begin = time.time() - self.dfs(s,e) - end = time.time() - return end - begin - - def bfs_time(self, s = -2): - begin = time.time() - self.bfs(s) - end = time.time() - return end - begin + def __init__(self): + self.graph = {} + + # adding vertices and edges + # adding the weight is optional + # handels repetition + def add_pair(self, u, v, w=1): + if self.graph.get(u): + if self.graph[u].count([w, v]) == 0: + self.graph[u].append([w, v]) + else: + self.graph[u] = [[w, v]] + if not self.graph.get(v): + self.graph[v] = [] + + def all_nodes(self): + return list(self.graph) + + # handels if the input does not exist + def remove_pair(self, u, v): + if self.graph.get(u): + for _ in self.graph[u]: + if _[1] == v: + self.graph[u].remove(_) + + # if no destination is meant the defaut value is -1 + def dfs(self, s=-2, d=-1): + if s == d: + return [] + stack = [] + visited = [] + if s == -2: + s = list(self.graph.keys())[0] + stack.append(s) + visited.append(s) + ss = s + + while True: + # check if there is any non isolated nodes + if len(self.graph[s]) != 0: + ss = s + for __ in self.graph[s]: + if visited.count(__[1]) < 1: + if __[1] == d: + visited.append(d) + return visited + else: + stack.append(__[1]) + visited.append(__[1]) + ss = __[1] + break + + # check if all the children are visited + if s == ss: + stack.pop() + if len(stack) != 0: + s = stack[len(stack) - 1] + else: + s = ss + + # check if se have reached the starting point + if len(stack) == 0: + return visited + + # c is the count of nodes you want and if you leave it or pass -1 to the funtion the count + # will be random from 10 to 10000 + def fill_graph_randomly(self, c=-1): + if c == -1: + c = (math.floor(rand.random() * 10000)) + 10 + for _ in range(c): + # every vertex has max 100 edges + e = math.floor(rand.random() * 102) + 1 + for __ in range(e): + n = math.floor(rand.random() * (c)) + 1 + if n == _: + continue + self.add_pair(_, n, 1) + + def bfs(self, s=-2): + d = deque() + visited = [] + if s == -2: + s = list(self.graph.keys())[0] + d.append(s) + visited.append(s) + while d: + s = d.popleft() + if len(self.graph[s]) != 0: + for __ in self.graph[s]: + if visited.count(__[1]) < 1: + d.append(__[1]) + visited.append(__[1]) + return visited + + def in_degree(self, u): + count = 0 + for _ in self.graph: + for __ in self.graph[_]: + if __[1] == u: + count += 1 + return count + + def out_degree(self, u): + return len(self.graph[u]) + + def topological_sort(self, s=-2): + stack = [] + visited = [] + if s == -2: + s = list(self.graph.keys())[0] + stack.append(s) + visited.append(s) + ss = s + sorted_nodes = [] + + while True: + # check if there is any non isolated nodes + if len(self.graph[s]) != 0: + ss = s + for __ in self.graph[s]: + if visited.count(__[1]) < 1: + stack.append(__[1]) + visited.append(__[1]) + ss = __[1] + break + + # check if all the children are visited + if s == ss: + sorted_nodes.append(stack.pop()) + if len(stack) != 0: + s = stack[len(stack) - 1] + else: + s = ss + + # check if se have reached the starting point + if len(stack) == 0: + return sorted_nodes + + def cycle_nodes(self): + stack = [] + visited = [] + s = list(self.graph.keys())[0] + stack.append(s) + visited.append(s) + parent = -2 + indirect_parents = [] + ss = s + on_the_way_back = False + anticipating_nodes = set() + + while True: + # check if there is any non isolated nodes + if len(self.graph[s]) != 0: + ss = s + for __ in self.graph[s]: + if ( + visited.count(__[1]) > 0 + and __[1] != parent + and indirect_parents.count(__[1]) > 0 + and not on_the_way_back + ): + l = len(stack) - 1 + while True and l >= 0: + if stack[l] == __[1]: + anticipating_nodes.add(__[1]) + break + else: + anticipating_nodes.add(stack[l]) + l -= 1 + if visited.count(__[1]) < 1: + stack.append(__[1]) + visited.append(__[1]) + ss = __[1] + break + + # check if all the children are visited + if s == ss: + stack.pop() + on_the_way_back = True + if len(stack) != 0: + s = stack[len(stack) - 1] + else: + on_the_way_back = False + indirect_parents.append(parent) + parent = s + s = ss + + # check if se have reached the starting point + if len(stack) == 0: + return list(anticipating_nodes) + + def has_cycle(self): + stack = [] + visited = [] + s = list(self.graph.keys())[0] + stack.append(s) + visited.append(s) + parent = -2 + indirect_parents = [] + ss = s + on_the_way_back = False + anticipating_nodes = set() + + while True: + # check if there is any non isolated nodes + if len(self.graph[s]) != 0: + ss = s + for __ in self.graph[s]: + if ( + visited.count(__[1]) > 0 + and __[1] != parent + and indirect_parents.count(__[1]) > 0 + and not on_the_way_back + ): + l = len(stack) - 1 + while True and l >= 0: + if stack[l] == __[1]: + anticipating_nodes.add(__[1]) + break + else: + return True + anticipating_nodes.add(stack[l]) + l -= 1 + if visited.count(__[1]) < 1: + stack.append(__[1]) + visited.append(__[1]) + ss = __[1] + break + + # check if all the children are visited + if s == ss: + stack.pop() + on_the_way_back = True + if len(stack) != 0: + s = stack[len(stack) - 1] + else: + on_the_way_back = False + indirect_parents.append(parent) + parent = s + s = ss + + # check if se have reached the starting point + if len(stack) == 0: + return False + + def dfs_time(self, s=-2, e=-1): + begin = time.time() + self.dfs(s, e) + end = time.time() + return end - begin + + def bfs_time(self, s=-2): + begin = time.time() + self.bfs(s) + end = time.time() + return end - begin + class Graph: - def __init__(self): - self.graph = {} - - # adding vertices and edges - # adding the weight is optional - # handels repetition - def add_pair(self, u, v, w = 1): - # check if the u exists - if self.graph.get(u): - # if there already is a edge - if self.graph[u].count([w,v]) == 0: - self.graph[u].append([w, v]) - else: - # if u does not exist - self.graph[u] = [[w, v]] - # add the other way - if self.graph.get(v): - # if there already is a edge - if self.graph[v].count([w,u]) == 0: - self.graph[v].append([w, u]) - else: - # if u does not exist - self.graph[v] = [[w, u]] - - # handels if the input does not exist - def remove_pair(self, u, v): - if self.graph.get(u): - for _ in self.graph[u]: - if _[1] == v: - self.graph[u].remove(_) - # the other way round - if self.graph.get(v): - for _ in self.graph[v]: - if _[1] == u: - self.graph[v].remove(_) - - # if no destination is meant the defaut value is -1 - def dfs(self, s = -2, d = -1): - if s == d: - return [] - stack = [] - visited = [] - if s == -2: - s = list(self.graph.keys())[0] - stack.append(s) - visited.append(s) - ss = s - - while True: - # check if there is any non isolated nodes - if len(self.graph[s]) != 0: - ss = s - for __ in self.graph[s]: - if visited.count(__[1]) < 1: - if __[1] == d: - visited.append(d) - return visited - else: - stack.append(__[1]) - visited.append(__[1]) - ss =__[1] - break - - # check if all the children are visited - if s == ss : - stack.pop() - if len(stack) != 0: - s = stack[len(stack) - 1] - else: - s = ss - - # check if se have reached the starting point - if len(stack) == 0: - return visited - - # c is the count of nodes you want and if you leave it or pass -1 to the funtion the count - # will be random from 10 to 10000 - def fill_graph_randomly(self, c = -1): - if c == -1: - c = (math.floor(rand.random() * 10000)) + 10 - for _ in range(c): - # every vertex has max 100 edges - e = math.floor(rand.random() * 102) + 1 - for __ in range(e): - n = math.floor(rand.random() * (c)) + 1 - if n == _: - continue - self.add_pair(_, n, 1) - - def bfs(self, s = -2): - d = deque() - visited = [] - if s == -2: - s = list(self.graph.keys())[0] - d.append(s) - visited.append(s) - while d: - s = d.popleft() - if len(self.graph[s]) != 0: - for __ in self.graph[s]: - if visited.count(__[1]) < 1: - d.append(__[1]) - visited.append(__[1]) - return visited - def degree(self, u): - return len(self.graph[u]) - - def cycle_nodes(self): - stack = [] - visited = [] - s = list(self.graph.keys())[0] - stack.append(s) - visited.append(s) - parent = -2 - indirect_parents = [] - ss = s - on_the_way_back = False - anticipating_nodes = set() - - while True: - # check if there is any non isolated nodes - if len(self.graph[s]) != 0: - ss = s - for __ in self.graph[s]: - if visited.count(__[1]) > 0 and __[1] != parent and indirect_parents.count(__[1]) > 0 and not on_the_way_back: - l = len(stack) - 1 - while True and l >= 0: - if stack[l] == __[1]: - anticipating_nodes.add(__[1]) - break - else: - anticipating_nodes.add(stack[l]) - l -= 1 - if visited.count(__[1]) < 1: - stack.append(__[1]) - visited.append(__[1]) - ss =__[1] - break - - # check if all the children are visited - if s == ss : - stack.pop() - on_the_way_back = True - if len(stack) != 0: - s = stack[len(stack) - 1] - else: - on_the_way_back = False - indirect_parents.append(parent) - parent = s - s = ss - - # check if se have reached the starting point - if len(stack) == 0: - return list(anticipating_nodes) - - def has_cycle(self): - stack = [] - visited = [] - s = list(self.graph.keys())[0] - stack.append(s) - visited.append(s) - parent = -2 - indirect_parents = [] - ss = s - on_the_way_back = False - anticipating_nodes = set() - - while True: - # check if there is any non isolated nodes - if len(self.graph[s]) != 0: - ss = s - for __ in self.graph[s]: - if visited.count(__[1]) > 0 and __[1] != parent and indirect_parents.count(__[1]) > 0 and not on_the_way_back: - l = len(stack) - 1 - while True and l >= 0: - if stack[l] == __[1]: - anticipating_nodes.add(__[1]) - break - else: - return True - anticipating_nodes.add(stack[l]) - l -= 1 - if visited.count(__[1]) < 1: - stack.append(__[1]) - visited.append(__[1]) - ss =__[1] - break - - # check if all the children are visited - if s == ss : - stack.pop() - on_the_way_back = True - if len(stack) != 0: - s = stack[len(stack) - 1] - else: - on_the_way_back = False - indirect_parents.append(parent) - parent = s - s = ss - - # check if se have reached the starting point - if len(stack) == 0: - return False - def all_nodes(self): - return list(self.graph) - - def dfs_time(self, s = -2, e = -1): - begin = time.time() - self.dfs(s,e) - end = time.time() - return end - begin - - def bfs_time(self, s = -2): - begin = time.time() - self.bfs(s) - end = time.time() - return end - begin + def __init__(self): + self.graph = {} + + # adding vertices and edges + # adding the weight is optional + # handels repetition + def add_pair(self, u, v, w=1): + # check if the u exists + if self.graph.get(u): + # if there already is a edge + if self.graph[u].count([w, v]) == 0: + self.graph[u].append([w, v]) + else: + # if u does not exist + self.graph[u] = [[w, v]] + # add the other way + if self.graph.get(v): + # if there already is a edge + if self.graph[v].count([w, u]) == 0: + self.graph[v].append([w, u]) + else: + # if u does not exist + self.graph[v] = [[w, u]] + + # handels if the input does not exist + def remove_pair(self, u, v): + if self.graph.get(u): + for _ in self.graph[u]: + if _[1] == v: + self.graph[u].remove(_) + # the other way round + if self.graph.get(v): + for _ in self.graph[v]: + if _[1] == u: + self.graph[v].remove(_) + + # if no destination is meant the defaut value is -1 + def dfs(self, s=-2, d=-1): + if s == d: + return [] + stack = [] + visited = [] + if s == -2: + s = list(self.graph.keys())[0] + stack.append(s) + visited.append(s) + ss = s + + while True: + # check if there is any non isolated nodes + if len(self.graph[s]) != 0: + ss = s + for __ in self.graph[s]: + if visited.count(__[1]) < 1: + if __[1] == d: + visited.append(d) + return visited + else: + stack.append(__[1]) + visited.append(__[1]) + ss = __[1] + break + + # check if all the children are visited + if s == ss: + stack.pop() + if len(stack) != 0: + s = stack[len(stack) - 1] + else: + s = ss + + # check if se have reached the starting point + if len(stack) == 0: + return visited + + # c is the count of nodes you want and if you leave it or pass -1 to the funtion the count + # will be random from 10 to 10000 + def fill_graph_randomly(self, c=-1): + if c == -1: + c = (math.floor(rand.random() * 10000)) + 10 + for _ in range(c): + # every vertex has max 100 edges + e = math.floor(rand.random() * 102) + 1 + for __ in range(e): + n = math.floor(rand.random() * (c)) + 1 + if n == _: + continue + self.add_pair(_, n, 1) + + def bfs(self, s=-2): + d = deque() + visited = [] + if s == -2: + s = list(self.graph.keys())[0] + d.append(s) + visited.append(s) + while d: + s = d.popleft() + if len(self.graph[s]) != 0: + for __ in self.graph[s]: + if visited.count(__[1]) < 1: + d.append(__[1]) + visited.append(__[1]) + return visited + + def degree(self, u): + return len(self.graph[u]) + + def cycle_nodes(self): + stack = [] + visited = [] + s = list(self.graph.keys())[0] + stack.append(s) + visited.append(s) + parent = -2 + indirect_parents = [] + ss = s + on_the_way_back = False + anticipating_nodes = set() + + while True: + # check if there is any non isolated nodes + if len(self.graph[s]) != 0: + ss = s + for __ in self.graph[s]: + if ( + visited.count(__[1]) > 0 + and __[1] != parent + and indirect_parents.count(__[1]) > 0 + and not on_the_way_back + ): + l = len(stack) - 1 + while True and l >= 0: + if stack[l] == __[1]: + anticipating_nodes.add(__[1]) + break + else: + anticipating_nodes.add(stack[l]) + l -= 1 + if visited.count(__[1]) < 1: + stack.append(__[1]) + visited.append(__[1]) + ss = __[1] + break + + # check if all the children are visited + if s == ss: + stack.pop() + on_the_way_back = True + if len(stack) != 0: + s = stack[len(stack) - 1] + else: + on_the_way_back = False + indirect_parents.append(parent) + parent = s + s = ss + + # check if se have reached the starting point + if len(stack) == 0: + return list(anticipating_nodes) + + def has_cycle(self): + stack = [] + visited = [] + s = list(self.graph.keys())[0] + stack.append(s) + visited.append(s) + parent = -2 + indirect_parents = [] + ss = s + on_the_way_back = False + anticipating_nodes = set() + + while True: + # check if there is any non isolated nodes + if len(self.graph[s]) != 0: + ss = s + for __ in self.graph[s]: + if ( + visited.count(__[1]) > 0 + and __[1] != parent + and indirect_parents.count(__[1]) > 0 + and not on_the_way_back + ): + l = len(stack) - 1 + while True and l >= 0: + if stack[l] == __[1]: + anticipating_nodes.add(__[1]) + break + else: + return True + anticipating_nodes.add(stack[l]) + l -= 1 + if visited.count(__[1]) < 1: + stack.append(__[1]) + visited.append(__[1]) + ss = __[1] + break + + # check if all the children are visited + if s == ss: + stack.pop() + on_the_way_back = True + if len(stack) != 0: + s = stack[len(stack) - 1] + else: + on_the_way_back = False + indirect_parents.append(parent) + parent = s + s = ss + + # check if se have reached the starting point + if len(stack) == 0: + return False + + def all_nodes(self): + return list(self.graph) + + def dfs_time(self, s=-2, e=-1): + begin = time.time() + self.dfs(s, e) + end = time.time() + return end - begin + + def bfs_time(self, s=-2): + begin = time.time() + self.bfs(s) + end = time.time() + return end - begin diff --git a/graphs/edmonds_karp_multiple_source_and_sink.py b/graphs/edmonds_karp_multiple_source_and_sink.py index d231ac2c4cc3..6334f05c50bd 100644 --- a/graphs/edmonds_karp_multiple_source_and_sink.py +++ b/graphs/edmonds_karp_multiple_source_and_sink.py @@ -28,14 +28,13 @@ def _normalizeGraph(self, sources, sinks): for i in sources: maxInputFlow += sum(self.graph[i]) - size = len(self.graph) + 1 for room in self.graph: room.insert(0, 0) self.graph.insert(0, [0] * size) for i in sources: self.graph[0][i + 1] = maxInputFlow - self.sourceIndex = 0 + self.sourceIndex = 0 size = len(self.graph) + 1 for room in self.graph: @@ -45,7 +44,6 @@ def _normalizeGraph(self, sources, sinks): self.graph[i + 1][size - 1] = maxInputFlow self.sinkIndex = size - 1 - def findMaximumFlow(self): if self.maximumFlowAlgorithm is None: raise Exception("You need to set maximum flow algorithm before.") @@ -80,7 +78,6 @@ def _algorithm(self): pass - class MaximumFlowAlgorithmExecutor(FlowNetworkAlgorithmExecutor): def __init__(self, flowNetwork): super(MaximumFlowAlgorithmExecutor, self).__init__(flowNetwork) @@ -93,6 +90,7 @@ def getMaximumFlow(self): return self.maximumFlow + class PushRelabelExecutor(MaximumFlowAlgorithmExecutor): def __init__(self, flowNetwork): super(PushRelabelExecutor, self).__init__(flowNetwork) @@ -112,8 +110,11 @@ def _algorithm(self): self.excesses[nextVertexIndex] += bandwidth # Relabel-to-front selection rule - verticesList = [i for i in range(self.verticesCount) - if i != self.sourceIndex and i != self.sinkIndex] + verticesList = [ + i + for i in range(self.verticesCount) + if i != self.sourceIndex and i != self.sinkIndex + ] # move through list i = 0 @@ -135,15 +136,21 @@ def processVertex(self, vertexIndex): while self.excesses[vertexIndex] > 0: for neighbourIndex in range(self.verticesCount): # if it's neighbour and current vertex is higher - if self.graph[vertexIndex][neighbourIndex] - self.preflow[vertexIndex][neighbourIndex] > 0\ - and self.heights[vertexIndex] > self.heights[neighbourIndex]: + if ( + self.graph[vertexIndex][neighbourIndex] + - self.preflow[vertexIndex][neighbourIndex] + > 0 + and self.heights[vertexIndex] > self.heights[neighbourIndex] + ): self.push(vertexIndex, neighbourIndex) self.relabel(vertexIndex) def push(self, fromIndex, toIndex): - preflowDelta = min(self.excesses[fromIndex], - self.graph[fromIndex][toIndex] - self.preflow[fromIndex][toIndex]) + preflowDelta = min( + self.excesses[fromIndex], + self.graph[fromIndex][toIndex] - self.preflow[fromIndex][toIndex], + ) self.preflow[fromIndex][toIndex] += preflowDelta self.preflow[toIndex][fromIndex] -= preflowDelta self.excesses[fromIndex] -= preflowDelta @@ -152,14 +159,18 @@ def push(self, fromIndex, toIndex): def relabel(self, vertexIndex): minHeight = None for toIndex in range(self.verticesCount): - if self.graph[vertexIndex][toIndex] - self.preflow[vertexIndex][toIndex] > 0: + if ( + self.graph[vertexIndex][toIndex] - self.preflow[vertexIndex][toIndex] + > 0 + ): if minHeight is None or self.heights[toIndex] < minHeight: minHeight = self.heights[toIndex] if minHeight is not None: self.heights[vertexIndex] = minHeight + 1 -if __name__ == '__main__': + +if __name__ == "__main__": entrances = [0] exits = [3] # graph = [ diff --git a/graphs/eulerian_path_and_circuit_for_undirected_graph.py b/graphs/eulerian_path_and_circuit_for_undirected_graph.py index c6c6a1a25f03..a2e5cf4da26a 100644 --- a/graphs/eulerian_path_and_circuit_for_undirected_graph.py +++ b/graphs/eulerian_path_and_circuit_for_undirected_graph.py @@ -50,32 +50,10 @@ def check_euler(graph, max_node): def main(): - G1 = { - 1: [2, 3, 4], - 2: [1, 3], - 3: [1, 2], - 4: [1, 5], - 5: [4] - } - G2 = { - 1: [2, 3, 4, 5], - 2: [1, 3], - 3: [1, 2], - 4: [1, 5], - 5: [1, 4] - } - G3 = { - 1: [2, 3, 4], - 2: [1, 3, 4], - 3: [1, 2], - 4: [1, 2, 5], - 5: [4] - } - G4 = { - 1: [2, 3], - 2: [1, 3], - 3: [1, 2], - } + G1 = {1: [2, 3, 4], 2: [1, 3], 3: [1, 2], 4: [1, 5], 5: [4]} + G2 = {1: [2, 3, 4, 5], 2: [1, 3], 3: [1, 2], 4: [1, 5], 5: [1, 4]} + G3 = {1: [2, 3, 4], 2: [1, 3, 4], 3: [1, 2], 4: [1, 2, 5], 5: [4]} + G4 = {1: [2, 3], 2: [1, 3], 3: [1, 2]} G5 = { 1: [], 2: [] diff --git a/graphs/even_tree.py b/graphs/even_tree.py index 45d55eecff8a..c9aef6e7861f 100644 --- a/graphs/even_tree.py +++ b/graphs/even_tree.py @@ -45,23 +45,13 @@ def even_tree(): dfs(1) -if __name__ == '__main__': +if __name__ == "__main__": n, m = 10, 9 tree = defaultdict(list) visited = {} cuts = [] count = 0 - edges = [ - (2, 1), - (3, 1), - (4, 3), - (5, 2), - (6, 1), - (7, 2), - (8, 6), - (9, 8), - (10, 8), - ] + edges = [(2, 1), (3, 1), (4, 3), (5, 2), (6, 1), (7, 2), (8, 6), (9, 8), (10, 8)] for u, v in edges: tree[u].append(v) tree[v].append(u) diff --git a/graphs/finding_bridges.py b/graphs/finding_bridges.py index 56533dd48bde..e18a3bafa9c0 100644 --- a/graphs/finding_bridges.py +++ b/graphs/finding_bridges.py @@ -1,7 +1,7 @@ # Finding Bridges in Undirected Graph def computeBridges(l): id = 0 - n = len(l) # No of vertices in graph + n = len(l) # No of vertices in graph low = [0] * n visited = [False] * n @@ -23,9 +23,20 @@ def dfs(at, parent, bridges, id): bridges = [] for i in range(n): - if (not visited[i]): + if not visited[i]: dfs(i, -1, bridges, id) print(bridges) - -l = {0:[1,2], 1:[0,2], 2:[0,1,3,5], 3:[2,4], 4:[3], 5:[2,6,8], 6:[5,7], 7:[6,8], 8:[5,7]} + + +l = { + 0: [1, 2], + 1: [0, 2], + 2: [0, 1, 3, 5], + 3: [2, 4], + 4: [3], + 5: [2, 6, 8], + 6: [5, 7], + 7: [6, 8], + 8: [5, 7], +} computeBridges(l) diff --git a/graphs/graph_list.py b/graphs/graph_list.py index 2ca363b1d746..4f0cbf15c033 100644 --- a/graphs/graph_list.py +++ b/graphs/graph_list.py @@ -5,6 +5,7 @@ # We can use Python's dictionary for constructing the graph. + class AdjacencyList(object): def __init__(self): self.List = {} @@ -17,10 +18,11 @@ def addEdge(self, fromVertex, toVertex): self.List[fromVertex] = [toVertex] def printList(self): - for i in self.List: - print((i,'->',' -> '.join([str(j) for j in self.List[i]]))) + for i in self.List: + print((i, "->", " -> ".join([str(j) for j in self.List[i]]))) + -if __name__ == '__main__': +if __name__ == "__main__": al = AdjacencyList() al.addEdge(0, 1) al.addEdge(0, 4) diff --git a/graphs/graph_matrix.py b/graphs/graph_matrix.py index 1998fec8d6fe..987168426ba5 100644 --- a/graphs/graph_matrix.py +++ b/graphs/graph_matrix.py @@ -1,8 +1,7 @@ class Graph: - def __init__(self, vertex): self.vertex = vertex - self.graph = [[0] * vertex for i in range(vertex) ] + self.graph = [[0] * vertex for i in range(vertex)] def add_edge(self, u, v): self.graph[u - 1][v - 1] = 1 @@ -12,18 +11,15 @@ def show(self): for i in self.graph: for j in i: - print(j, end=' ') - print(' ') - - + print(j, end=" ") + print(" ") g = Graph(100) -g.add_edge(1,4) -g.add_edge(4,2) -g.add_edge(4,5) -g.add_edge(2,5) -g.add_edge(5,3) +g.add_edge(1, 4) +g.add_edge(4, 2) +g.add_edge(4, 5) +g.add_edge(2, 5) +g.add_edge(5, 3) g.show() - diff --git a/graphs/graphs_floyd_warshall.py b/graphs/graphs_floyd_warshall.py index 5f159683733f..5727a2f21d89 100644 --- a/graphs/graphs_floyd_warshall.py +++ b/graphs/graphs_floyd_warshall.py @@ -6,19 +6,18 @@ def _print_dist(dist, v): - print("\nThe shortest path matrix using Floyd Warshall algorithm\n") - for i in range(v): - for j in range(v): - if dist[i][j] != float('inf') : - print(int(dist[i][j]),end = "\t") - else: - print("INF",end="\t") - print() - + print("\nThe shortest path matrix using Floyd Warshall algorithm\n") + for i in range(v): + for j in range(v): + if dist[i][j] != float("inf"): + print(int(dist[i][j]), end="\t") + else: + print("INF", end="\t") + print() def floyd_warshall(graph, v): - """ + """ :param graph: 2D array calculated from weight[edge[i, j]] :type graph: List[List[float]] :param v: number of vertices @@ -33,68 +32,70 @@ def floyd_warshall(graph, v): 5. Whenever distance[i][j] is given a new minimum value, next vertex[i][j] is updated to the next vertex[i][k]. """ - dist=[[float('inf') for _ in range(v)] for _ in range(v)] - - for i in range(v): - for j in range(v): - dist[i][j] = graph[i][j] - - # check vertex k against all other vertices (i, j) - for k in range(v): - # looping through rows of graph array - for i in range(v): - # looping through columns of graph array - for j in range(v): - if dist[i][k]!=float('inf') and dist[k][j]!=float('inf') and dist[i][k]+dist[k][j] < dist[i][j]: - dist[i][j] = dist[i][k] + dist[k][j] - - _print_dist(dist, v) - return dist, v - - - -if __name__== '__main__': - v = int(input("Enter number of vertices: ")) - e = int(input("Enter number of edges: ")) - - graph = [[float('inf') for i in range(v)] for j in range(v)] - - for i in range(v): - graph[i][i] = 0.0 - - # src and dst are indices that must be within the array size graph[e][v] - # failure to follow this will result in an error - for i in range(e): - print("\nEdge ",i+1) - src = int(input("Enter source:")) - dst = int(input("Enter destination:")) - weight = float(input("Enter weight:")) - graph[src][dst] = weight - - floyd_warshall(graph, v) - - - # Example Input - # Enter number of vertices: 3 - # Enter number of edges: 2 - - # # generated graph from vertex and edge inputs - # [[inf, inf, inf], [inf, inf, inf], [inf, inf, inf]] - # [[0.0, inf, inf], [inf, 0.0, inf], [inf, inf, 0.0]] - - # specify source, destination and weight for edge #1 - # Edge 1 - # Enter source:1 - # Enter destination:2 - # Enter weight:2 - - # specify source, destination and weight for edge #2 - # Edge 2 - # Enter source:2 - # Enter destination:1 - # Enter weight:1 - - # # Expected Output from the vertice, edge and src, dst, weight inputs!! - # 0 INF INF - # INF 0 2 - # INF 1 0 + dist = [[float("inf") for _ in range(v)] for _ in range(v)] + + for i in range(v): + for j in range(v): + dist[i][j] = graph[i][j] + + # check vertex k against all other vertices (i, j) + for k in range(v): + # looping through rows of graph array + for i in range(v): + # looping through columns of graph array + for j in range(v): + if ( + dist[i][k] != float("inf") + and dist[k][j] != float("inf") + and dist[i][k] + dist[k][j] < dist[i][j] + ): + dist[i][j] = dist[i][k] + dist[k][j] + + _print_dist(dist, v) + return dist, v + + +if __name__ == "__main__": + v = int(input("Enter number of vertices: ")) + e = int(input("Enter number of edges: ")) + + graph = [[float("inf") for i in range(v)] for j in range(v)] + + for i in range(v): + graph[i][i] = 0.0 + + # src and dst are indices that must be within the array size graph[e][v] + # failure to follow this will result in an error + for i in range(e): + print("\nEdge ", i + 1) + src = int(input("Enter source:")) + dst = int(input("Enter destination:")) + weight = float(input("Enter weight:")) + graph[src][dst] = weight + + floyd_warshall(graph, v) + + # Example Input + # Enter number of vertices: 3 + # Enter number of edges: 2 + + # # generated graph from vertex and edge inputs + # [[inf, inf, inf], [inf, inf, inf], [inf, inf, inf]] + # [[0.0, inf, inf], [inf, 0.0, inf], [inf, inf, 0.0]] + + # specify source, destination and weight for edge #1 + # Edge 1 + # Enter source:1 + # Enter destination:2 + # Enter weight:2 + + # specify source, destination and weight for edge #2 + # Edge 2 + # Enter source:2 + # Enter destination:1 + # Enter weight:1 + + # # Expected Output from the vertice, edge and src, dst, weight inputs!! + # 0 INF INF + # INF 0 2 + # INF 1 0 diff --git a/graphs/kahns_algorithm_long.py b/graphs/kahns_algorithm_long.py index 453b5706f6da..0651040365d0 100644 --- a/graphs/kahns_algorithm_long.py +++ b/graphs/kahns_algorithm_long.py @@ -12,19 +12,20 @@ def longestDistance(l): if indegree[i] == 0: queue.append(i) - while(queue): + while queue: vertex = queue.pop(0) for x in l[vertex]: indegree[x] -= 1 if longDist[vertex] + 1 > longDist[x]: - longDist[x] = longDist[vertex] + 1 + longDist[x] = longDist[vertex] + 1 if indegree[x] == 0: queue.append(x) print(max(longDist)) + # Adjacency list of Graph -l = {0:[2,3,4], 1:[2,7], 2:[5], 3:[5,7], 4:[7], 5:[6], 6:[7], 7:[]} +l = {0: [2, 3, 4], 1: [2, 7], 2: [5], 3: [5, 7], 4: [7], 5: [6], 6: [7], 7: []} longestDistance(l) diff --git a/graphs/kahns_algorithm_topo.py b/graphs/kahns_algorithm_topo.py index 8c182c4e902c..d50bc9a43d19 100644 --- a/graphs/kahns_algorithm_topo.py +++ b/graphs/kahns_algorithm_topo.py @@ -13,7 +13,7 @@ def topologicalSort(l): if indegree[i] == 0: queue.append(i) - while(queue): + while queue: vertex = queue.pop(0) cnt += 1 topo.append(vertex) @@ -27,6 +27,7 @@ def topologicalSort(l): else: print(topo) + # Adjacency List of Graph -l = {0:[1,2], 1:[3], 2:[3], 3:[4,5], 4:[], 5:[]} +l = {0: [1, 2], 1: [3], 2: [3], 3: [4, 5], 4: [], 5: []} topologicalSort(l) diff --git a/graphs/minimum_spanning_tree_kruskal.py b/graphs/minimum_spanning_tree_kruskal.py index a2211582ec40..91b44f6508e7 100644 --- a/graphs/minimum_spanning_tree_kruskal.py +++ b/graphs/minimum_spanning_tree_kruskal.py @@ -4,29 +4,29 @@ edges = [] for i in range(num_edges): - node1, node2, cost = list(map(int, input().strip().split())) - edges.append((i,node1,node2,cost)) + node1, node2, cost = list(map(int, input().strip().split())) + edges.append((i, node1, node2, cost)) edges = sorted(edges, key=lambda edge: edge[3]) parent = list(range(num_nodes)) def find_parent(i): - if i != parent[i]: - parent[i] = find_parent(parent[i]) - return parent[i] + if i != parent[i]: + parent[i] = find_parent(parent[i]) + return parent[i] minimum_spanning_tree_cost = 0 minimum_spanning_tree = [] for edge in edges: - parent_a = find_parent(edge[1]) - parent_b = find_parent(edge[2]) - if parent_a != parent_b: - minimum_spanning_tree_cost += edge[3] - minimum_spanning_tree.append(edge) - parent[parent_a] = parent_b + parent_a = find_parent(edge[1]) + parent_b = find_parent(edge[2]) + if parent_a != parent_b: + minimum_spanning_tree_cost += edge[3] + minimum_spanning_tree.append(edge) + parent[parent_a] = parent_b print(minimum_spanning_tree_cost) for edge in minimum_spanning_tree: - print(edge) + print(edge) diff --git a/graphs/minimum_spanning_tree_prims.py b/graphs/minimum_spanning_tree_prims.py index 0f21b8f494e4..216d6a3f56de 100644 --- a/graphs/minimum_spanning_tree_prims.py +++ b/graphs/minimum_spanning_tree_prims.py @@ -1,9 +1,11 @@ import sys from collections import defaultdict + def PrimsAlgorithm(l): nodePosition = [] + def getPosition(vertex): return nodePosition[vertex] @@ -36,11 +38,11 @@ def topToBottom(heap, start, size, positions): def bottomToTop(val, index, heap, position): temp = position[index] - while(index != 0): + while index != 0: if index % 2 == 0: - parent = int( (index-2) / 2 ) + parent = int((index - 2) / 2) else: - parent = int( (index-1) / 2 ) + parent = int((index - 1) / 2) if val < heap[parent]: heap[index] = heap[parent] @@ -69,9 +71,9 @@ def deleteMinimum(heap, positions): return temp visited = [0 for i in range(len(l))] - Nbr_TV = [-1 for i in range(len(l))] # Neighboring Tree Vertex of selected vertex + Nbr_TV = [-1 for i in range(len(l))] # Neighboring Tree Vertex of selected vertex # Minimum Distance of explored vertex with neighboring vertex of partial tree formed in graph - Distance_TV = [] # Heap of Distance of vertices from their neighboring vertex + Distance_TV = [] # Heap of Distance of vertices from their neighboring vertex Positions = [] for x in range(len(l)): @@ -84,8 +86,8 @@ def deleteMinimum(heap, positions): visited[0] = 1 Distance_TV[0] = sys.maxsize for x in l[0]: - Nbr_TV[ x[0] ] = 0 - Distance_TV[ x[0] ] = x[1] + Nbr_TV[x[0]] = 0 + Distance_TV[x[0]] = x[1] heapify(Distance_TV, Positions) for i in range(1, len(l)): @@ -94,12 +96,13 @@ def deleteMinimum(heap, positions): TreeEdges.append((Nbr_TV[vertex], vertex)) visited[vertex] = 1 for v in l[vertex]: - if visited[v[0]] == 0 and v[1] < Distance_TV[ getPosition(v[0]) ]: - Distance_TV[ getPosition(v[0]) ] = v[1] + if visited[v[0]] == 0 and v[1] < Distance_TV[getPosition(v[0])]: + Distance_TV[getPosition(v[0])] = v[1] bottomToTop(v[1], getPosition(v[0]), Distance_TV, Positions) - Nbr_TV[ v[0] ] = vertex + Nbr_TV[v[0]] = vertex return TreeEdges + if __name__ == "__main__": # < --------- Prims Algorithm --------- > n = int(input("Enter number of vertices: ").strip()) @@ -107,6 +110,6 @@ def deleteMinimum(heap, positions): adjlist = defaultdict(list) for x in range(e): l = [int(x) for x in input().strip().split()] - adjlist[l[0]].append([ l[1], l[2] ]) - adjlist[l[1]].append([ l[0], l[2] ]) + adjlist[l[0]].append([l[1], l[2]]) + adjlist[l[1]].append([l[0], l[2]]) print(PrimsAlgorithm(adjlist)) diff --git a/graphs/multi_hueristic_astar.py b/graphs/multi_hueristic_astar.py index 3021c4162b8e..56cfc727d338 100644 --- a/graphs/multi_hueristic_astar.py +++ b/graphs/multi_hueristic_astar.py @@ -3,260 +3,319 @@ class PriorityQueue: - def __init__(self): - self.elements = [] - self.set = set() - - def minkey(self): - if not self.empty(): - return self.elements[0][0] - else: - return float('inf') - - def empty(self): - return len(self.elements) == 0 - - def put(self, item, priority): - if item not in self.set: - heapq.heappush(self.elements, (priority, item)) - self.set.add(item) - else: - # update - # print("update", item) - temp = [] - (pri, x) = heapq.heappop(self.elements) - while x != item: - temp.append((pri, x)) - (pri, x) = heapq.heappop(self.elements) - temp.append((priority, item)) - for (pro, xxx) in temp: - heapq.heappush(self.elements, (pro, xxx)) - - def remove_element(self, item): - if item in self.set: - self.set.remove(item) - temp = [] - (pro, x) = heapq.heappop(self.elements) - while x != item: - temp.append((pro, x)) - (pro, x) = heapq.heappop(self.elements) - for (prito, yyy) in temp: - heapq.heappush(self.elements, (prito, yyy)) - - def top_show(self): - return self.elements[0][1] - - def get(self): - (priority, item) = heapq.heappop(self.elements) - self.set.remove(item) - return (priority, item) + def __init__(self): + self.elements = [] + self.set = set() + + def minkey(self): + if not self.empty(): + return self.elements[0][0] + else: + return float("inf") + + def empty(self): + return len(self.elements) == 0 + + def put(self, item, priority): + if item not in self.set: + heapq.heappush(self.elements, (priority, item)) + self.set.add(item) + else: + # update + # print("update", item) + temp = [] + (pri, x) = heapq.heappop(self.elements) + while x != item: + temp.append((pri, x)) + (pri, x) = heapq.heappop(self.elements) + temp.append((priority, item)) + for (pro, xxx) in temp: + heapq.heappush(self.elements, (pro, xxx)) + + def remove_element(self, item): + if item in self.set: + self.set.remove(item) + temp = [] + (pro, x) = heapq.heappop(self.elements) + while x != item: + temp.append((pro, x)) + (pro, x) = heapq.heappop(self.elements) + for (prito, yyy) in temp: + heapq.heappush(self.elements, (prito, yyy)) + + def top_show(self): + return self.elements[0][1] + + def get(self): + (priority, item) = heapq.heappop(self.elements) + self.set.remove(item) + return (priority, item) + def consistent_hueristic(P, goal): - # euclidean distance - a = np.array(P) - b = np.array(goal) - return np.linalg.norm(a - b) + # euclidean distance + a = np.array(P) + b = np.array(goal) + return np.linalg.norm(a - b) + def hueristic_2(P, goal): - # integer division by time variable - return consistent_hueristic(P, goal) // t + # integer division by time variable + return consistent_hueristic(P, goal) // t + def hueristic_1(P, goal): - # manhattan distance - return abs(P[0] - goal[0]) + abs(P[1] - goal[1]) + # manhattan distance + return abs(P[0] - goal[0]) + abs(P[1] - goal[1]) + def key(start, i, goal, g_function): - ans = g_function[start] + W1 * hueristics[i](start, goal) - return ans + ans = g_function[start] + W1 * hueristics[i](start, goal) + return ans + def do_something(back_pointer, goal, start): - grid = np.chararray((n, n)) - for i in range(n): - for j in range(n): - grid[i][j] = '*' - - for i in range(n): - for j in range(n): - if (j, (n-1)-i) in blocks: - grid[i][j] = "#" - - grid[0][(n-1)] = "-" - x = back_pointer[goal] - while x != start: - (x_c, y_c) = x - # print(x) - grid[(n-1)-y_c][x_c] = "-" - x = back_pointer[x] - grid[(n-1)][0] = "-" - - - for i in range(n): - for j in range(n): - if (i, j) == (0, n-1): - print(grid[i][j], end=' ') - print("<-- End position", end=' ') - else: - print(grid[i][j], end=' ') - print() - print("^") - print("Start position") - print() - print("# is an obstacle") - print("- is the path taken by algorithm") - print("PATH TAKEN BY THE ALGORITHM IS:-") - x = back_pointer[goal] - while x != start: - print(x, end=' ') - x = back_pointer[x] - print(x) - quit() + grid = np.chararray((n, n)) + for i in range(n): + for j in range(n): + grid[i][j] = "*" + + for i in range(n): + for j in range(n): + if (j, (n - 1) - i) in blocks: + grid[i][j] = "#" + + grid[0][(n - 1)] = "-" + x = back_pointer[goal] + while x != start: + (x_c, y_c) = x + # print(x) + grid[(n - 1) - y_c][x_c] = "-" + x = back_pointer[x] + grid[(n - 1)][0] = "-" + + for i in range(n): + for j in range(n): + if (i, j) == (0, n - 1): + print(grid[i][j], end=" ") + print("<-- End position", end=" ") + else: + print(grid[i][j], end=" ") + print() + print("^") + print("Start position") + print() + print("# is an obstacle") + print("- is the path taken by algorithm") + print("PATH TAKEN BY THE ALGORITHM IS:-") + x = back_pointer[goal] + while x != start: + print(x, end=" ") + x = back_pointer[x] + print(x) + quit() + def valid(p): - if p[0] < 0 or p[0] > n-1: - return False - if p[1] < 0 or p[1] > n-1: - return False - return True - -def expand_state(s, j, visited, g_function, close_list_anchor, close_list_inad, open_list, back_pointer): - for itera in range(n_hueristic): - open_list[itera].remove_element(s) - # print("s", s) - # print("j", j) - (x, y) = s - left = (x-1, y) - right = (x+1, y) - up = (x, y+1) - down = (x, y-1) - - for neighbours in [left, right, up, down]: - if neighbours not in blocks: - if valid(neighbours) and neighbours not in visited: - # print("neighbour", neighbours) - visited.add(neighbours) - back_pointer[neighbours] = -1 - g_function[neighbours] = float('inf') - - if valid(neighbours) and g_function[neighbours] > g_function[s] + 1: - g_function[neighbours] = g_function[s] + 1 - back_pointer[neighbours] = s - if neighbours not in close_list_anchor: - open_list[0].put(neighbours, key(neighbours, 0, goal, g_function)) - if neighbours not in close_list_inad: - for var in range(1,n_hueristic): - if key(neighbours, var, goal, g_function) <= W2 * key(neighbours, 0, goal, g_function): - # print("why not plssssssssss") - open_list[j].put(neighbours, key(neighbours, var, goal, g_function)) - - - # print + if p[0] < 0 or p[0] > n - 1: + return False + if p[1] < 0 or p[1] > n - 1: + return False + return True + + +def expand_state( + s, + j, + visited, + g_function, + close_list_anchor, + close_list_inad, + open_list, + back_pointer, +): + for itera in range(n_hueristic): + open_list[itera].remove_element(s) + # print("s", s) + # print("j", j) + (x, y) = s + left = (x - 1, y) + right = (x + 1, y) + up = (x, y + 1) + down = (x, y - 1) + + for neighbours in [left, right, up, down]: + if neighbours not in blocks: + if valid(neighbours) and neighbours not in visited: + # print("neighbour", neighbours) + visited.add(neighbours) + back_pointer[neighbours] = -1 + g_function[neighbours] = float("inf") + + if valid(neighbours) and g_function[neighbours] > g_function[s] + 1: + g_function[neighbours] = g_function[s] + 1 + back_pointer[neighbours] = s + if neighbours not in close_list_anchor: + open_list[0].put(neighbours, key(neighbours, 0, goal, g_function)) + if neighbours not in close_list_inad: + for var in range(1, n_hueristic): + if key(neighbours, var, goal, g_function) <= W2 * key( + neighbours, 0, goal, g_function + ): + # print("why not plssssssssss") + open_list[j].put( + neighbours, key(neighbours, var, goal, g_function) + ) + + # print + def make_common_ground(): - some_list = [] - # block 1 - for x in range(1, 5): - for y in range(1, 6): - some_list.append((x, y)) - - # line - for x in range(15, 20): - some_list.append((x, 17)) - - # block 2 big - for x in range(10, 19): - for y in range(1, 15): - some_list.append((x, y)) - - # L block - for x in range(1, 4): - for y in range(12, 19): - some_list.append((x, y)) - for x in range(3, 13): - for y in range(16, 19): - some_list.append((x, y)) - return some_list + some_list = [] + # block 1 + for x in range(1, 5): + for y in range(1, 6): + some_list.append((x, y)) + + # line + for x in range(15, 20): + some_list.append((x, 17)) + + # block 2 big + for x in range(10, 19): + for y in range(1, 15): + some_list.append((x, y)) + + # L block + for x in range(1, 4): + for y in range(12, 19): + some_list.append((x, y)) + for x in range(3, 13): + for y in range(16, 19): + some_list.append((x, y)) + return some_list + hueristics = {0: consistent_hueristic, 1: hueristic_1, 2: hueristic_2} -blocks_blk = [(0, 1),(1, 1),(2, 1),(3, 1),(4, 1),(5, 1),(6, 1),(7, 1),(8, 1),(9, 1),(10, 1),(11, 1),(12, 1),(13, 1),(14, 1),(15, 1),(16, 1),(17, 1),(18, 1), (19, 1)] +blocks_blk = [ + (0, 1), + (1, 1), + (2, 1), + (3, 1), + (4, 1), + (5, 1), + (6, 1), + (7, 1), + (8, 1), + (9, 1), + (10, 1), + (11, 1), + (12, 1), + (13, 1), + (14, 1), + (15, 1), + (16, 1), + (17, 1), + (18, 1), + (19, 1), +] blocks_no = [] blocks_all = make_common_ground() - - blocks = blocks_blk # hyper parameters W1 = 1 W2 = 1 n = 20 -n_hueristic = 3 # one consistent and two other inconsistent +n_hueristic = 3 # one consistent and two other inconsistent # start and end destination start = (0, 0) -goal = (n-1, n-1) +goal = (n - 1, n - 1) t = 1 + + def multi_a_star(start, goal, n_hueristic): - g_function = {start: 0, goal: float('inf')} - back_pointer = {start:-1, goal:-1} - open_list = [] - visited = set() - - for i in range(n_hueristic): - open_list.append(PriorityQueue()) - open_list[i].put(start, key(start, i, goal, g_function)) - - close_list_anchor = [] - close_list_inad = [] - while open_list[0].minkey() < float('inf'): - for i in range(1, n_hueristic): - # print("i", i) - # print(open_list[0].minkey(), open_list[i].minkey()) - if open_list[i].minkey() <= W2 * open_list[0].minkey(): - global t - t += 1 - # print("less prio") - if g_function[goal] <= open_list[i].minkey(): - if g_function[goal] < float('inf'): - do_something(back_pointer, goal, start) - else: - _, get_s = open_list[i].top_show() - visited.add(get_s) - expand_state(get_s, i, visited, g_function, close_list_anchor, close_list_inad, open_list, back_pointer) - close_list_inad.append(get_s) - else: - # print("more prio") - if g_function[goal] <= open_list[0].minkey(): - if g_function[goal] < float('inf'): - do_something(back_pointer, goal, start) - else: - # print("hoolla") - get_s = open_list[0].top_show() - visited.add(get_s) - expand_state(get_s, 0, visited, g_function, close_list_anchor, close_list_inad, open_list, back_pointer) - close_list_anchor.append(get_s) - print("No path found to goal") - print() - for i in range(n-1,-1, -1): - for j in range(n): - if (j, i) in blocks: - print('#', end=' ') - elif (j, i) in back_pointer: - if (j, i) == (n-1, n-1): - print('*', end=' ') - else: - print('-', end=' ') - else: - print('*', end=' ') - if (j, i) == (n-1, n-1): - print('<-- End position', end=' ') - print() - print("^") - print("Start position") - print() - print("# is an obstacle") - print("- is the path taken by algorithm") + g_function = {start: 0, goal: float("inf")} + back_pointer = {start: -1, goal: -1} + open_list = [] + visited = set() + + for i in range(n_hueristic): + open_list.append(PriorityQueue()) + open_list[i].put(start, key(start, i, goal, g_function)) + + close_list_anchor = [] + close_list_inad = [] + while open_list[0].minkey() < float("inf"): + for i in range(1, n_hueristic): + # print("i", i) + # print(open_list[0].minkey(), open_list[i].minkey()) + if open_list[i].minkey() <= W2 * open_list[0].minkey(): + global t + t += 1 + # print("less prio") + if g_function[goal] <= open_list[i].minkey(): + if g_function[goal] < float("inf"): + do_something(back_pointer, goal, start) + else: + _, get_s = open_list[i].top_show() + visited.add(get_s) + expand_state( + get_s, + i, + visited, + g_function, + close_list_anchor, + close_list_inad, + open_list, + back_pointer, + ) + close_list_inad.append(get_s) + else: + # print("more prio") + if g_function[goal] <= open_list[0].minkey(): + if g_function[goal] < float("inf"): + do_something(back_pointer, goal, start) + else: + # print("hoolla") + get_s = open_list[0].top_show() + visited.add(get_s) + expand_state( + get_s, + 0, + visited, + g_function, + close_list_anchor, + close_list_inad, + open_list, + back_pointer, + ) + close_list_anchor.append(get_s) + print("No path found to goal") + print() + for i in range(n - 1, -1, -1): + for j in range(n): + if (j, i) in blocks: + print("#", end=" ") + elif (j, i) in back_pointer: + if (j, i) == (n - 1, n - 1): + print("*", end=" ") + else: + print("-", end=" ") + else: + print("*", end=" ") + if (j, i) == (n - 1, n - 1): + print("<-- End position", end=" ") + print() + print("^") + print("Start position") + print() + print("# is an obstacle") + print("- is the path taken by algorithm") if __name__ == "__main__": diff --git a/graphs/page_rank.py b/graphs/page_rank.py index 59f15a99e6b2..1e2c7d9aeb48 100644 --- a/graphs/page_rank.py +++ b/graphs/page_rank.py @@ -1,7 +1,7 @@ -''' +""" Author: https://github.com/bhushan-borole -''' -''' +""" +""" The input graph for the algorithm is: A B C @@ -9,11 +9,9 @@ B 0 0 1 C 1 0 0 -''' +""" -graph = [[0, 1, 1], - [0, 0, 1], - [1, 0, 0]] +graph = [[0, 1, 1], [0, 0, 1], [1, 0, 0]] class Node: @@ -21,17 +19,17 @@ def __init__(self, name): self.name = name self.inbound = [] self.outbound = [] - + def add_inbound(self, node): self.inbound.append(node) - + def add_outbound(self, node): self.outbound.append(node) - + def __repr__(self): - return 'Node {}: Inbound: {} ; Outbound: {}'.format(self.name, - self.inbound, - self.outbound) + return "Node {}: Inbound: {} ; Outbound: {}".format( + self.name, self.inbound, self.outbound + ) def page_rank(nodes, limit=3, d=0.85): @@ -44,17 +42,19 @@ def page_rank(nodes, limit=3, d=0.85): outbounds[node.name] = len(node.outbound) for i in range(limit): - print("======= Iteration {} =======".format(i+1)) + print("======= Iteration {} =======".format(i + 1)) for j, node in enumerate(nodes): - ranks[node.name] = (1 - d) + d * sum([ ranks[ib]/outbounds[ib] for ib in node.inbound ]) + ranks[node.name] = (1 - d) + d * sum( + [ranks[ib] / outbounds[ib] for ib in node.inbound] + ) print(ranks) def main(): - names = list(input('Enter Names of the Nodes: ').split()) + names = list(input("Enter Names of the Nodes: ").split()) nodes = [Node(name) for name in names] - + for ri, row in enumerate(graph): for ci, col in enumerate(row): if col == 1: @@ -68,5 +68,5 @@ def main(): page_rank(nodes) -if __name__ == '__main__': - main() \ No newline at end of file +if __name__ == "__main__": + main() diff --git a/graphs/prim.py b/graphs/prim.py index f7e08278966d..336424d2c3c1 100644 --- a/graphs/prim.py +++ b/graphs/prim.py @@ -21,7 +21,7 @@ import math -class vertex(): +class vertex: """Class Vertex.""" def __init__(self, id): @@ -40,7 +40,7 @@ def __init__(self, id): def __lt__(self, other): """Comparison rule to < operator.""" - return (self.key < other.key) + return self.key < other.key def __repr__(self): """Return the vertex id.""" @@ -76,4 +76,4 @@ def prim(graph, root): v.key = u.edges[v.id] for i in range(1, len(graph)): A.append([graph[i].id, graph[i].pi.id]) - return(A) + return A diff --git a/graphs/scc_kosaraju.py b/graphs/scc_kosaraju.py index 99564a7cfa35..573c1bf5e363 100644 --- a/graphs/scc_kosaraju.py +++ b/graphs/scc_kosaraju.py @@ -1,26 +1,31 @@ def dfs(u): global g, r, scc, component, visit, stack - if visit[u]: return + if visit[u]: + return visit[u] = True for v in g[u]: dfs(v) stack.append(u) + def dfs2(u): global g, r, scc, component, visit, stack - if visit[u]: return + if visit[u]: + return visit[u] = True component.append(u) for v in r[u]: dfs2(v) + def kosaraju(): global g, r, scc, component, visit, stack for i in range(n): dfs(i) - visit = [False]*n + visit = [False] * n for i in stack[::-1]: - if visit[i]: continue + if visit[i]: + continue component = [] dfs2(i) scc.append(component) @@ -29,18 +34,18 @@ def kosaraju(): if __name__ == "__main__": # n - no of nodes, m - no of edges - n, m = list(map(int,input().strip().split())) + n, m = list(map(int, input().strip().split())) - g = [[] for i in range(n)] #graph - r = [[] for i in range(n)] #reversed graph + g = [[] for i in range(n)] # graph + r = [[] for i in range(n)] # reversed graph # input graph data (edges) for i in range(m): - u, v = list(map(int,input().strip().split())) + u, v = list(map(int, input().strip().split())) g[u].append(v) r[v].append(u) stack = [] - visit = [False]*n + visit = [False] * n scc = [] component = [] print(kosaraju()) diff --git a/graphs/tarjans_scc.py b/graphs/tarjans_scc.py index 89754e593508..4b0a689ea3c0 100644 --- a/graphs/tarjans_scc.py +++ b/graphs/tarjans_scc.py @@ -36,9 +36,13 @@ def strong_connect(v, index, components): for w in g[v]: if index_of[w] == -1: index = strong_connect(w, index, components) - lowlink_of[v] = lowlink_of[w] if lowlink_of[w] < lowlink_of[v] else lowlink_of[v] + lowlink_of[v] = ( + lowlink_of[w] if lowlink_of[w] < lowlink_of[v] else lowlink_of[v] + ) elif on_stack[w]: - lowlink_of[v] = lowlink_of[w] if lowlink_of[w] < lowlink_of[v] else lowlink_of[v] + lowlink_of[v] = ( + lowlink_of[w] if lowlink_of[w] < lowlink_of[v] else lowlink_of[v] + ) if lowlink_of[v] == index_of[v]: component = [] @@ -67,7 +71,7 @@ def create_graph(n, edges): return g -if __name__ == '__main__': +if __name__ == "__main__": # Test n_vertices = 7 source = [0, 0, 1, 2, 3, 3, 4, 4, 6] diff --git a/hashes/chaos_machine.py b/hashes/chaos_machine.py index 3a7c3950bb29..8d3bbd4c0251 100644 --- a/hashes/chaos_machine.py +++ b/hashes/chaos_machine.py @@ -1,7 +1,9 @@ """example of simple chaos machine""" # Chaos Machine (K, t, m) -K = [0.33, 0.44, 0.55, 0.44, 0.33]; t = 3; m = 5 +K = [0.33, 0.44, 0.55, 0.44, 0.33] +t = 3 +m = 5 # Buffer Space (with Parameters Space) buffer_space, params_space = [], [] @@ -9,75 +11,73 @@ # Machine Time machine_time = 0 + def push(seed): - global buffer_space, params_space, machine_time, \ - K, m, t + global buffer_space, params_space, machine_time, K, m, t + + # Choosing Dynamical Systems (All) + for key, value in enumerate(buffer_space): + # Evolution Parameter + e = float(seed / value) - # Choosing Dynamical Systems (All) - for key, value in enumerate(buffer_space): - # Evolution Parameter - e = float(seed / value) + # Control Theory: Orbit Change + value = (buffer_space[(key + 1) % m] + e) % 1 - # Control Theory: Orbit Change - value = (buffer_space[(key + 1) % m] + e) % 1 + # Control Theory: Trajectory Change + r = (params_space[key] + e) % 1 + 3 - # Control Theory: Trajectory Change - r = (params_space[key] + e) % 1 + 3 + # Modification (Transition Function) - Jumps + buffer_space[key] = round(float(r * value * (1 - value)), 10) + params_space[key] = r # Saving to Parameters Space - # Modification (Transition Function) - Jumps - buffer_space[key] = \ - round(float(r * value * (1 - value)), 10) - params_space[key] = \ - r # Saving to Parameters Space + # Logistic Map + assert max(buffer_space) < 1 + assert max(params_space) < 4 - # Logistic Map - assert max(buffer_space) < 1 - assert max(params_space) < 4 + # Machine Time + machine_time += 1 - # Machine Time - machine_time += 1 def pull(): - global buffer_space, params_space, machine_time, \ - K, m, t + global buffer_space, params_space, machine_time, K, m, t + + # PRNG (Xorshift by George Marsaglia) + def xorshift(X, Y): + X ^= Y >> 13 + Y ^= X << 17 + X ^= Y >> 5 + return X - # PRNG (Xorshift by George Marsaglia) - def xorshift(X, Y): - X ^= Y >> 13 - Y ^= X << 17 - X ^= Y >> 5 - return X + # Choosing Dynamical Systems (Increment) + key = machine_time % m - # Choosing Dynamical Systems (Increment) - key = machine_time % m + # Evolution (Time Length) + for i in range(0, t): + # Variables (Position + Parameters) + r = params_space[key] + value = buffer_space[key] - # Evolution (Time Length) - for i in range(0, t): - # Variables (Position + Parameters) - r = params_space[key] - value = buffer_space[key] + # Modification (Transition Function) - Flow + buffer_space[key] = round(float(r * value * (1 - value)), 10) + params_space[key] = (machine_time * 0.01 + r * 1.01) % 1 + 3 - # Modification (Transition Function) - Flow - buffer_space[key] = \ - round(float(r * value * (1 - value)), 10) - params_space[key] = \ - (machine_time * 0.01 + r * 1.01) % 1 + 3 + # Choosing Chaotic Data + X = int(buffer_space[(key + 2) % m] * (10 ** 10)) + Y = int(buffer_space[(key - 2) % m] * (10 ** 10)) - # Choosing Chaotic Data - X = int(buffer_space[(key + 2) % m] * (10 ** 10)) - Y = int(buffer_space[(key - 2) % m] * (10 ** 10)) + # Machine Time + machine_time += 1 - # Machine Time - machine_time += 1 + return xorshift(X, Y) % 0xFFFFFFFF - return xorshift(X, Y) % 0xFFFFFFFF def reset(): - global buffer_space, params_space, machine_time, \ - K, m, t + global buffer_space, params_space, machine_time, K, m, t + + buffer_space = K + params_space = [0] * m + machine_time = 0 - buffer_space = K; params_space = [0] * m - machine_time = 0 ####################################### @@ -86,15 +86,17 @@ def reset(): # Pushing Data (Input) import random + message = random.sample(range(0xFFFFFFFF), 100) for chunk in message: - push(chunk) + push(chunk) # for controlling inp = "" # Pulling Data (Output) while inp in ("e", "E"): - print("%s" % format(pull(), '#04x')) - print(buffer_space); print(params_space) - inp = input("(e)exit? ").strip() + print("%s" % format(pull(), "#04x")) + print(buffer_space) + print(params_space) + inp = input("(e)exit? ").strip() diff --git a/hashes/enigma_machine.py b/hashes/enigma_machine.py index 06215785765f..5420bacc1409 100644 --- a/hashes/enigma_machine.py +++ b/hashes/enigma_machine.py @@ -40,7 +40,7 @@ def engine(input_character): rotator() -if __name__ == '__main__': +if __name__ == "__main__": decode = input("Type your message:\n") decode = list(decode) while True: @@ -56,4 +56,5 @@ def engine(input_character): print("\n" + "".join(code)) print( f"\nYour Token is {token} please write it down.\nIf you want to decode " - f"this message again you should input same digits as token!") + f"this message again you should input same digits as token!" + ) diff --git a/hashes/md5.py b/hashes/md5.py index 1ad43013363f..85565533d175 100644 --- a/hashes/md5.py +++ b/hashes/md5.py @@ -20,8 +20,8 @@ def rearrange(bitString32): if len(bitString32) != 32: raise ValueError("Need length 32") newString = "" - for i in [3, 2,1,0]: - newString += bitString32[8*i:8*i+8] + for i in [3, 2, 1, 0]: + newString += bitString32[8 * i : 8 * i + 8] return newString @@ -35,10 +35,10 @@ def reformatHex(i): '9a020000' """ - hexrep = format(i, '08x') + hexrep = format(i, "08x") thing = "" - for i in [3, 2,1,0]: - thing += hexrep[2*i:2*i+2] + for i in [3, 2, 1, 0]: + thing += hexrep[2 * i : 2 * i + 2] return thing @@ -53,10 +53,10 @@ def pad(bitString): [string] -- [binary string] """ startLength = len(bitString) - bitString += '1' + bitString += "1" while len(bitString) % 512 != 448: - bitString += '0' - lastPart = format(startLength, '064b') + bitString += "0" + lastPart = format(startLength, "064b") bitString += rearrange(lastPart[32:]) + rearrange(lastPart[:32]) return bitString @@ -73,33 +73,35 @@ def getBlock(bitString): currPos = 0 while currPos < len(bitString): - currPart = bitString[currPos:currPos+512] + currPart = bitString[currPos : currPos + 512] mySplits = [] for i in range(16): - mySplits.append(int(rearrange(currPart[32*i:32*i+32]), 2)) + mySplits.append(int(rearrange(currPart[32 * i : 32 * i + 32]), 2)) yield mySplits currPos += 512 def not32(i): - ''' + """ >>> not32(34) 4294967261 - ''' - i_str = format(i, '032b') - new_str = '' + """ + i_str = format(i, "032b") + new_str = "" for c in i_str: - new_str += '1' if c == '0' else '0' + new_str += "1" if c == "0" else "0" return int(new_str, 2) + def sum32(a, b): - ''' + """ + + """ + return (a + b) % 2 ** 32 - ''' - return (a + b) % 2**32 def leftrot32(i, s): - return (i << s) ^ (i >> (32-s)) + return (i << s) ^ (i >> (32 - s)) def md5me(testString): @@ -110,22 +112,84 @@ def md5me(testString): testString {[string]} -- [message] """ - bs = '' + bs = "" for i in testString: - bs += format(ord(i), '08b') + bs += format(ord(i), "08b") bs = pad(bs) - tvals = [int(2**32 * abs(math.sin(i+1))) for i in range(64)] + tvals = [int(2 ** 32 * abs(math.sin(i + 1))) for i in range(64)] a0 = 0x67452301 - b0 = 0xefcdab89 - c0 = 0x98badcfe + b0 = 0xEFCDAB89 + c0 = 0x98BADCFE d0 = 0x10325476 - s = [7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, - 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, \ - 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, \ - 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21 ] + s = [ + 7, + 12, + 17, + 22, + 7, + 12, + 17, + 22, + 7, + 12, + 17, + 22, + 7, + 12, + 17, + 22, + 5, + 9, + 14, + 20, + 5, + 9, + 14, + 20, + 5, + 9, + 14, + 20, + 5, + 9, + 14, + 20, + 4, + 11, + 16, + 23, + 4, + 11, + 16, + 23, + 4, + 11, + 16, + 23, + 4, + 11, + 16, + 23, + 6, + 10, + 15, + 21, + 6, + 10, + 15, + 21, + 6, + 10, + 15, + 21, + 6, + 10, + 15, + 21, + ] for m in getBlock(bs): A = a0 @@ -134,42 +198,44 @@ def md5me(testString): D = d0 for i in range(64): if i <= 15: - #f = (B & C) | (not32(B) & D) + # f = (B & C) | (not32(B) & D) f = D ^ (B & (C ^ D)) g = i elif i <= 31: - #f = (D & B) | (not32(D) & C) + # f = (D & B) | (not32(D) & C) f = C ^ (D & (B ^ C)) - g = (5*i+1) % 16 + g = (5 * i + 1) % 16 elif i <= 47: f = B ^ C ^ D - g = (3*i+5) % 16 + g = (3 * i + 5) % 16 else: f = C ^ (B | not32(D)) - g = (7*i) % 16 + g = (7 * i) % 16 dtemp = D D = C C = B - B = sum32(B, leftrot32((A + f + tvals[i] + m[g]) % 2**32, s[i])) + B = sum32(B, leftrot32((A + f + tvals[i] + m[g]) % 2 ** 32, s[i])) A = dtemp a0 = sum32(a0, A) b0 = sum32(b0, B) c0 = sum32(c0, C) d0 = sum32(d0, D) - digest = reformatHex(a0) + reformatHex(b0) + \ - reformatHex(c0) + reformatHex(d0) + digest = reformatHex(a0) + reformatHex(b0) + reformatHex(c0) + reformatHex(d0) return digest def test(): assert md5me("") == "d41d8cd98f00b204e9800998ecf8427e" - assert md5me( - "The quick brown fox jumps over the lazy dog") == "9e107d9d372bb6826bd81d3542a419d6" + assert ( + md5me("The quick brown fox jumps over the lazy dog") + == "9e107d9d372bb6826bd81d3542a419d6" + ) print("Success.") if __name__ == "__main__": test() import doctest + doctest.testmod() diff --git a/hashes/sha1.py b/hashes/sha1.py index 511ea6363733..3bf27af27582 100644 --- a/hashes/sha1.py +++ b/hashes/sha1.py @@ -25,7 +25,7 @@ import argparse import struct -import hashlib #hashlib is only used inside the Test class +import hashlib # hashlib is only used inside the Test class import unittest @@ -35,6 +35,7 @@ class SHA1Hash: >>> SHA1Hash(bytes('Allan', 'utf-8')).final_hash() '872af2d8ac3d8695387e7c804bf0e02c18df9e6e' """ + def __init__(self, data): """ Inititates the variables data and h. h is a list of 5 8-digit Hexadecimal @@ -52,21 +53,23 @@ def rotate(n, b): >>> SHA1Hash('').rotate(12,2) 48 """ - return ((n << b) | (n >> (32 - b))) & 0xffffffff + return ((n << b) | (n >> (32 - b))) & 0xFFFFFFFF def padding(self): """ Pads the input message with zeros so that padded_data has 64 bytes or 512 bits """ - padding = b'\x80' + b'\x00'*(63 - (len(self.data) + 8) % 64) - padded_data = self.data + padding + struct.pack('>Q', 8 * len(self.data)) + padding = b"\x80" + b"\x00" * (63 - (len(self.data) + 8) % 64) + padded_data = self.data + padding + struct.pack(">Q", 8 * len(self.data)) return padded_data def split_blocks(self): """ Returns a list of bytestrings each of length 64 """ - return [self.padded_data[i:i+64] for i in range(0, len(self.padded_data), 64)] + return [ + self.padded_data[i : i + 64] for i in range(0, len(self.padded_data), 64) + ] # @staticmethod def expand_block(self, block): @@ -74,9 +77,9 @@ def expand_block(self, block): Takes a bytestring-block of length 64, unpacks it to a list of integers and returns a list of 80 integers after some bit operations """ - w = list(struct.unpack('>16L', block)) + [0] * 64 + w = list(struct.unpack(">16L", block)) + [0] * 64 for i in range(16, 80): - w[i] = self.rotate((w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16]), 1) + w[i] = self.rotate((w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16]), 1) return w def final_hash(self): @@ -106,22 +109,30 @@ def final_hash(self): elif 60 <= i < 80: f = b ^ c ^ d k = 0xCA62C1D6 - a, b, c, d, e = self.rotate(a, 5) + f + e + k + expanded_block[i] & 0xffffffff,\ - a, self.rotate(b, 30), c, d - self.h = self.h[0] + a & 0xffffffff,\ - self.h[1] + b & 0xffffffff,\ - self.h[2] + c & 0xffffffff,\ - self.h[3] + d & 0xffffffff,\ - self.h[4] + e & 0xffffffff - return '%08x%08x%08x%08x%08x' %tuple(self.h) + a, b, c, d, e = ( + self.rotate(a, 5) + f + e + k + expanded_block[i] & 0xFFFFFFFF, + a, + self.rotate(b, 30), + c, + d, + ) + self.h = ( + self.h[0] + a & 0xFFFFFFFF, + self.h[1] + b & 0xFFFFFFFF, + self.h[2] + c & 0xFFFFFFFF, + self.h[3] + d & 0xFFFFFFFF, + self.h[4] + e & 0xFFFFFFFF, + ) + return "%08x%08x%08x%08x%08x" % tuple(self.h) class SHA1HashTest(unittest.TestCase): """ Test class for the SHA1Hash class. Inherits the TestCase class from unittest """ + def testMatchHashes(self): - msg = bytes('Test String', 'utf-8') + msg = bytes("Test String", "utf-8") self.assertEqual(SHA1Hash(msg).final_hash(), hashlib.sha1(msg).hexdigest()) @@ -132,23 +143,27 @@ def main(): the test each time. """ # unittest.main() - parser = argparse.ArgumentParser(description='Process some strings or files') - parser.add_argument('--string', dest='input_string', - default='Hello World!! Welcome to Cryptography', - help='Hash the string') - parser.add_argument('--file', dest='input_file', help='Hash contents of a file') + parser = argparse.ArgumentParser(description="Process some strings or files") + parser.add_argument( + "--string", + dest="input_string", + default="Hello World!! Welcome to Cryptography", + help="Hash the string", + ) + parser.add_argument("--file", dest="input_file", help="Hash contents of a file") args = parser.parse_args() input_string = args.input_string - #In any case hash input should be a bytestring + # In any case hash input should be a bytestring if args.input_file: - with open(args.input_file, 'rb') as f: + with open(args.input_file, "rb") as f: hash_input = f.read() else: - hash_input = bytes(input_string, 'utf-8') + hash_input = bytes(input_string, "utf-8") print(SHA1Hash(hash_input).final_hash()) -if __name__ == '__main__': +if __name__ == "__main__": main() import doctest - doctest.testmod() \ No newline at end of file + + doctest.testmod() diff --git a/linear_algebra/src/lib.py b/linear_algebra/src/lib.py index 281991a93b2d..5ce0f696ad71 100644 --- a/linear_algebra/src/lib.py +++ b/linear_algebra/src/lib.py @@ -45,13 +45,15 @@ class Vector(object): changeComponent(pos,value) : changes the specified component. TODO: compare-operator """ - def __init__(self,components=[]): + + def __init__(self, components=[]): """ input: components or nothing simple constructor for init the vector """ self.__components = list(components) - def set(self,components): + + def set(self, components): """ input: new components changes the components of the vector. @@ -61,34 +63,39 @@ def set(self,components): self.__components = list(components) else: raise Exception("please give any vector") + def __str__(self): """ returns a string representation of the vector """ return "(" + ",".join(map(str, self.__components)) + ")" - def component(self,i): + + def component(self, i): """ input: index (start at 0) output: the i-th component of the vector. """ - if type(i) is int and -len(self.__components) <= i < len(self.__components) : + if type(i) is int and -len(self.__components) <= i < len(self.__components): return self.__components[i] else: raise Exception("index out of range") + def __len__(self): """ returns the size of the vector """ return len(self.__components) + def eulidLength(self): """ returns the eulidean length of the vector """ summe = 0 for c in self.__components: - summe += c**2 + summe += c ** 2 return math.sqrt(summe) - def __add__(self,other): + + def __add__(self, other): """ input: other vector assumes: other vector has the same size @@ -100,7 +107,8 @@ def __add__(self,other): return Vector(result) else: raise Exception("must have the same size") - def __sub__(self,other): + + def __sub__(self, other): """ input: other vector assumes: other vector has the same size @@ -110,73 +118,80 @@ def __sub__(self,other): if size == len(other): result = [self.__components[i] - other.component(i) for i in range(size)] return result - else: # error case + else: # error case raise Exception("must have the same size") - def __mul__(self,other): + + def __mul__(self, other): """ mul implements the scalar multiplication and the dot-product """ - if isinstance(other,float) or isinstance(other,int): - ans = [c*other for c in self.__components] + if isinstance(other, float) or isinstance(other, int): + ans = [c * other for c in self.__components] return ans - elif (isinstance(other,Vector) and (len(self) == len(other))): + elif isinstance(other, Vector) and (len(self) == len(other)): size = len(self) summe = 0 for i in range(size): summe += self.__components[i] * other.component(i) return summe - else: # error case + else: # error case raise Exception("invalide operand!") + def copy(self): """ copies this vector and returns it. """ return Vector(self.__components) - def changeComponent(self,pos,value): + + def changeComponent(self, pos, value): """ input: an index (pos) and a value changes the specified component (pos) with the 'value' """ - #precondition - assert (-len(self.__components) <= pos < len(self.__components)) + # precondition + assert -len(self.__components) <= pos < len(self.__components) self.__components[pos] = value - + + def zeroVector(dimension): """ returns a zero-vector of size 'dimension' - """ - #precondition - assert(isinstance(dimension,int)) - return Vector([0]*dimension) + """ + # precondition + assert isinstance(dimension, int) + return Vector([0] * dimension) -def unitBasisVector(dimension,pos): +def unitBasisVector(dimension, pos): """ returns a unit basis vector with a One at index 'pos' (indexing at 0) """ - #precondition - assert(isinstance(dimension,int) and (isinstance(pos,int))) - ans = [0]*dimension + # precondition + assert isinstance(dimension, int) and (isinstance(pos, int)) + ans = [0] * dimension ans[pos] = 1 return Vector(ans) - -def axpy(scalar,x,y): + +def axpy(scalar, x, y): """ input: a 'scalar' and two vectors 'x' and 'y' output: a vector computes the axpy operation """ # precondition - assert(isinstance(x,Vector) and (isinstance(y,Vector)) \ - and (isinstance(scalar,int) or isinstance(scalar,float))) - return (x*scalar + y) - + assert ( + isinstance(x, Vector) + and (isinstance(y, Vector)) + and (isinstance(scalar, int) or isinstance(scalar, float)) + ) + return x * scalar + y + -def randomVector(N,a,b): +def randomVector(N, a, b): """ input: size (N) of the vector. random range (a,b) @@ -184,7 +199,7 @@ def randomVector(N,a,b): random integer components between 'a' and 'b'. """ random.seed(None) - ans = [random.randint(a,b) for i in range(N)] + ans = [random.randint(a, b) for i in range(N)] return Vector(ans) @@ -205,7 +220,8 @@ class Matrix(object): operator + : implements the matrix-addition. operator - _ implements the matrix-subtraction """ - def __init__(self,matrix,w,h): + + def __init__(self, matrix, w, h): """ simple constructor for initialzes the matrix with components. @@ -213,6 +229,7 @@ def __init__(self,matrix,w,h): self.__matrix = matrix self.__width = w self.__height = h + def __str__(self): """ returns a string representation of this @@ -222,102 +239,113 @@ def __str__(self): for i in range(self.__height): ans += "|" for j in range(self.__width): - if j < self.__width -1: + if j < self.__width - 1: ans += str(self.__matrix[i][j]) + "," else: ans += str(self.__matrix[i][j]) + "|\n" return ans - def changeComponent(self,x,y, value): + + def changeComponent(self, x, y, value): """ changes the x-y component of this matrix """ if x >= 0 and x < self.__height and y >= 0 and y < self.__width: self.__matrix[x][y] = value else: - raise Exception ("changeComponent: indices out of bounds") - def component(self,x,y): + raise Exception("changeComponent: indices out of bounds") + + def component(self, x, y): """ returns the specified (x,y) component """ if x >= 0 and x < self.__height and y >= 0 and y < self.__width: return self.__matrix[x][y] else: - raise Exception ("changeComponent: indices out of bounds") + raise Exception("changeComponent: indices out of bounds") + def width(self): """ getter for the width """ return self.__width + def height(self): """ getter for the height """ return self.__height - def __mul__(self,other): + + def __mul__(self, other): """ implements the matrix-vector multiplication. implements the matrix-scalar multiplication """ - if isinstance(other, Vector): # vector-matrix - if (len(other) == self.__width): + if isinstance(other, Vector): # vector-matrix + if len(other) == self.__width: ans = zeroVector(self.__height) for i in range(self.__height): summe = 0 for j in range(self.__width): summe += other.component(j) * self.__matrix[i][j] - ans.changeComponent(i,summe) + ans.changeComponent(i, summe) summe = 0 return ans else: - raise Exception("vector must have the same size as the " + "number of columns of the matrix!") - elif isinstance(other,int) or isinstance(other,float): # matrix-scalar - matrix = [[self.__matrix[i][j] * other for j in range(self.__width)] for i in range(self.__height)] - return Matrix(matrix,self.__width,self.__height) - def __add__(self,other): + raise Exception( + "vector must have the same size as the " + + "number of columns of the matrix!" + ) + elif isinstance(other, int) or isinstance(other, float): # matrix-scalar + matrix = [ + [self.__matrix[i][j] * other for j in range(self.__width)] + for i in range(self.__height) + ] + return Matrix(matrix, self.__width, self.__height) + + def __add__(self, other): """ implements the matrix-addition. """ - if (self.__width == other.width() and self.__height == other.height()): + if self.__width == other.width() and self.__height == other.height(): matrix = [] for i in range(self.__height): row = [] for j in range(self.__width): - row.append(self.__matrix[i][j] + other.component(i,j)) + row.append(self.__matrix[i][j] + other.component(i, j)) matrix.append(row) - return Matrix(matrix,self.__width,self.__height) + return Matrix(matrix, self.__width, self.__height) else: raise Exception("matrix must have the same dimension!") - def __sub__(self,other): + + def __sub__(self, other): """ implements the matrix-subtraction. """ - if (self.__width == other.width() and self.__height == other.height()): + if self.__width == other.width() and self.__height == other.height(): matrix = [] for i in range(self.__height): row = [] for j in range(self.__width): - row.append(self.__matrix[i][j] - other.component(i,j)) + row.append(self.__matrix[i][j] - other.component(i, j)) matrix.append(row) - return Matrix(matrix,self.__width,self.__height) + return Matrix(matrix, self.__width, self.__height) else: raise Exception("matrix must have the same dimension!") - + def squareZeroMatrix(N): """ returns a square zero-matrix of dimension NxN """ - ans = [[0]*N for i in range(N)] - return Matrix(ans,N,N) - - -def randomMatrix(W,H,a,b): + ans = [[0] * N for i in range(N)] + return Matrix(ans, N, N) + + +def randomMatrix(W, H, a, b): """ returns a random matrix WxH with integer components between 'a' and 'b' """ random.seed(None) - matrix = [[random.randint(a,b) for j in range(W)] for i in range(H)] - return Matrix(matrix,W,H) - - + matrix = [[random.randint(a, b) for j in range(W)] for i in range(H)] + return Matrix(matrix, W, H) diff --git a/linear_algebra/src/tests.py b/linear_algebra/src/tests.py index afca4ce87117..b63f2ae8c2db 100644 --- a/linear_algebra/src/tests.py +++ b/linear_algebra/src/tests.py @@ -11,123 +11,144 @@ import unittest from lib import Matrix, Vector, axpy, squareZeroMatrix, unitBasisVector, zeroVector + class Test(unittest.TestCase): def test_component(self): """ test for method component """ - x = Vector([1,2,3]) - self.assertEqual(x.component(0),1) - self.assertEqual(x.component(2),3) + x = Vector([1, 2, 3]) + self.assertEqual(x.component(0), 1) + self.assertEqual(x.component(2), 3) try: y = Vector() self.assertTrue(False) except: self.assertTrue(True) + def test_str(self): """ test for toString() method """ - x = Vector([0,0,0,0,0,1]) - self.assertEqual(str(x),"(0,0,0,0,0,1)") + x = Vector([0, 0, 0, 0, 0, 1]) + self.assertEqual(str(x), "(0,0,0,0,0,1)") + def test_size(self): """ test for size()-method """ - x = Vector([1,2,3,4]) - self.assertEqual(len(x),4) + x = Vector([1, 2, 3, 4]) + self.assertEqual(len(x), 4) + def test_euclidLength(self): """ test for the eulidean length """ - x = Vector([1,2]) - self.assertAlmostEqual(x.eulidLength(),2.236,3) + x = Vector([1, 2]) + self.assertAlmostEqual(x.eulidLength(), 2.236, 3) + def test_add(self): """ test for + operator """ - x = Vector([1,2,3]) - y = Vector([1,1,1]) - self.assertEqual((x+y).component(0),2) - self.assertEqual((x+y).component(1),3) - self.assertEqual((x+y).component(2),4) + x = Vector([1, 2, 3]) + y = Vector([1, 1, 1]) + self.assertEqual((x + y).component(0), 2) + self.assertEqual((x + y).component(1), 3) + self.assertEqual((x + y).component(2), 4) + def test_sub(self): """ test for - operator """ - x = Vector([1,2,3]) - y = Vector([1,1,1]) - self.assertEqual((x-y).component(0),0) - self.assertEqual((x-y).component(1),1) - self.assertEqual((x-y).component(2),2) + x = Vector([1, 2, 3]) + y = Vector([1, 1, 1]) + self.assertEqual((x - y).component(0), 0) + self.assertEqual((x - y).component(1), 1) + self.assertEqual((x - y).component(2), 2) + def test_mul(self): """ test for * operator """ - x = Vector([1,2,3]) - a = Vector([2,-1,4]) # for test of dot-product - b = Vector([1,-2,-1]) - self.assertEqual(str(x*3.0),"(3.0,6.0,9.0)") - self.assertEqual((a*b),0) + x = Vector([1, 2, 3]) + a = Vector([2, -1, 4]) # for test of dot-product + b = Vector([1, -2, -1]) + self.assertEqual(str(x * 3.0), "(3.0,6.0,9.0)") + self.assertEqual((a * b), 0) + def test_zeroVector(self): """ test for the global function zeroVector(...) """ self.assertTrue(str(zeroVector(10)).count("0") == 10) + def test_unitBasisVector(self): """ test for the global function unitBasisVector(...) """ - self.assertEqual(str(unitBasisVector(3,1)),"(0,1,0)") + self.assertEqual(str(unitBasisVector(3, 1)), "(0,1,0)") + def test_axpy(self): """ test for the global function axpy(...) (operation) """ - x = Vector([1,2,3]) - y = Vector([1,0,1]) - self.assertEqual(str(axpy(2,x,y)),"(3,4,7)") + x = Vector([1, 2, 3]) + y = Vector([1, 0, 1]) + self.assertEqual(str(axpy(2, x, y)), "(3,4,7)") + def test_copy(self): """ test for the copy()-method """ - x = Vector([1,0,0,0,0,0]) + x = Vector([1, 0, 0, 0, 0, 0]) y = x.copy() - self.assertEqual(str(x),str(y)) + self.assertEqual(str(x), str(y)) + def test_changeComponent(self): """ test for the changeComponent(...)-method """ - x = Vector([1,0,0]) - x.changeComponent(0,0) - x.changeComponent(1,1) - self.assertEqual(str(x),"(0,1,0)") + x = Vector([1, 0, 0]) + x.changeComponent(0, 0) + x.changeComponent(1, 1) + self.assertEqual(str(x), "(0,1,0)") + def test_str_matrix(self): - A = Matrix([[1,2,3],[2,4,5],[6,7,8]],3,3) - self.assertEqual("|1,2,3|\n|2,4,5|\n|6,7,8|\n",str(A)) + A = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]], 3, 3) + self.assertEqual("|1,2,3|\n|2,4,5|\n|6,7,8|\n", str(A)) + def test__mul__matrix(self): - A = Matrix([[1,2,3],[4,5,6],[7,8,9]],3,3) - x = Vector([1,2,3]) - self.assertEqual("(14,32,50)",str(A*x)) - self.assertEqual("|2,4,6|\n|8,10,12|\n|14,16,18|\n",str(A*2)) + A = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]], 3, 3) + x = Vector([1, 2, 3]) + self.assertEqual("(14,32,50)", str(A * x)) + self.assertEqual("|2,4,6|\n|8,10,12|\n|14,16,18|\n", str(A * 2)) + def test_changeComponent_matrix(self): - A = Matrix([[1,2,3],[2,4,5],[6,7,8]],3,3) - A.changeComponent(0,2,5) - self.assertEqual("|1,2,5|\n|2,4,5|\n|6,7,8|\n",str(A)) + A = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]], 3, 3) + A.changeComponent(0, 2, 5) + self.assertEqual("|1,2,5|\n|2,4,5|\n|6,7,8|\n", str(A)) + def test_component_matrix(self): - A = Matrix([[1,2,3],[2,4,5],[6,7,8]],3,3) - self.assertEqual(7,A.component(2,1),0.01) + A = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]], 3, 3) + self.assertEqual(7, A.component(2, 1), 0.01) + def test__add__matrix(self): - A = Matrix([[1,2,3],[2,4,5],[6,7,8]],3,3) - B = Matrix([[1,2,7],[2,4,5],[6,7,10]],3,3) - self.assertEqual("|2,4,10|\n|4,8,10|\n|12,14,18|\n",str(A+B)) + A = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]], 3, 3) + B = Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 10]], 3, 3) + self.assertEqual("|2,4,10|\n|4,8,10|\n|12,14,18|\n", str(A + B)) + def test__sub__matrix(self): - A = Matrix([[1,2,3],[2,4,5],[6,7,8]],3,3) - B = Matrix([[1,2,7],[2,4,5],[6,7,10]],3,3) - self.assertEqual("|0,0,-4|\n|0,0,0|\n|0,0,-2|\n",str(A-B)) + A = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]], 3, 3) + B = Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 10]], 3, 3) + self.assertEqual("|0,0,-4|\n|0,0,0|\n|0,0,-2|\n", str(A - B)) + def test_squareZeroMatrix(self): - self.assertEqual('|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|' - +'\n|0,0,0,0,0|\n',str(squareZeroMatrix(5))) - + self.assertEqual( + "|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|" + "\n|0,0,0,0,0|\n", + str(squareZeroMatrix(5)), + ) + if __name__ == "__main__": unittest.main() diff --git a/machine_learning/decision_tree.py b/machine_learning/decision_tree.py index acdf646875ac..4f7a4d12966e 100644 --- a/machine_learning/decision_tree.py +++ b/machine_learning/decision_tree.py @@ -5,8 +5,9 @@ """ import numpy as np + class Decision_Tree: - def __init__(self, depth = 5, min_leaf_size = 5): + def __init__(self, depth=5, min_leaf_size=5): self.depth = depth self.decision_boundary = 0 self.left = None @@ -58,8 +59,7 @@ def train(self, X, y): return best_split = 0 - min_error = self.mean_squared_error(X,np.mean(y)) * 2 - + min_error = self.mean_squared_error(X, np.mean(y)) * 2 """ loop over all possible splits for the decision tree. find the best split. @@ -86,8 +86,12 @@ def train(self, X, y): right_y = y[best_split:] self.decision_boundary = X[best_split] - self.left = Decision_Tree(depth = self.depth - 1, min_leaf_size = self.min_leaf_size) - self.right = Decision_Tree(depth = self.depth - 1, min_leaf_size = self.min_leaf_size) + self.left = Decision_Tree( + depth=self.depth - 1, min_leaf_size=self.min_leaf_size + ) + self.right = Decision_Tree( + depth=self.depth - 1, min_leaf_size=self.min_leaf_size + ) self.left.train(left_X, left_y) self.right.train(right_X, right_y) else: @@ -113,17 +117,18 @@ def predict(self, x): print("Error: Decision tree not yet trained") return None + def main(): """ In this demonstration we're generating a sample data set from the sin function in numpy. We then train a decision tree on the data set and use the decision tree to predict the label of 10 different test values. Then the mean squared error over this test is displayed. """ - X = np.arange(-1., 1., 0.005) + X = np.arange(-1.0, 1.0, 0.005) y = np.sin(X) - tree = Decision_Tree(depth = 10, min_leaf_size = 10) - tree.train(X,y) + tree = Decision_Tree(depth=10, min_leaf_size=10) + tree.train(X, y) test_cases = (np.random.rand(10) * 2) - 1 predictions = np.array([tree.predict(x) for x in test_cases]) @@ -134,5 +139,5 @@ def main(): print("Average error: " + str(avg_error)) -if __name__ == '__main__': - main() \ No newline at end of file +if __name__ == "__main__": + main() diff --git a/machine_learning/gradient_descent.py b/machine_learning/gradient_descent.py index 9a17113b7ddb..811cc68467f9 100644 --- a/machine_learning/gradient_descent.py +++ b/machine_learning/gradient_descent.py @@ -4,21 +4,28 @@ import numpy # List of input, output pairs -train_data = (((5, 2, 3), 15), ((6, 5, 9), 25), - ((11, 12, 13), 41), ((1, 1, 1), 8), ((11, 12, 13), 41)) +train_data = ( + ((5, 2, 3), 15), + ((6, 5, 9), 25), + ((11, 12, 13), 41), + ((1, 1, 1), 8), + ((11, 12, 13), 41), +) test_data = (((515, 22, 13), 555), ((61, 35, 49), 150)) parameter_vector = [2, 4, 1, 5] m = len(train_data) LEARNING_RATE = 0.009 -def _error(example_no, data_set='train'): +def _error(example_no, data_set="train"): """ :param data_set: train data or test data :param example_no: example number whose error has to be checked :return: error in example pointed by example number. """ - return calculate_hypothesis_value(example_no, data_set) - output(example_no, data_set) + return calculate_hypothesis_value(example_no, data_set) - output( + example_no, data_set + ) def _hypothesis_value(data_input_tuple): @@ -32,7 +39,7 @@ def _hypothesis_value(data_input_tuple): """ hyp_val = 0 for i in range(len(parameter_vector) - 1): - hyp_val += data_input_tuple[i]*parameter_vector[i+1] + hyp_val += data_input_tuple[i] * parameter_vector[i + 1] hyp_val += parameter_vector[0] return hyp_val @@ -43,9 +50,9 @@ def output(example_no, data_set): :param example_no: example whose output is to be fetched :return: output for that example """ - if data_set == 'train': + if data_set == "train": return train_data[example_no][1] - elif data_set == 'test': + elif data_set == "test": return test_data[example_no][1] @@ -75,7 +82,7 @@ def summation_of_cost_derivative(index, end=m): if index == -1: summation_value += _error(i) else: - summation_value += _error(i)*train_data[i][0][index] + summation_value += _error(i) * train_data[i][0][index] return summation_value @@ -85,7 +92,7 @@ def get_cost_derivative(index): :return: derivative wrt to that index Note: If index is -1, this means we are calculating summation wrt to biased parameter. """ - cost_derivative_value = summation_of_cost_derivative(index, m)/m + cost_derivative_value = summation_of_cost_derivative(index, m) / m return cost_derivative_value @@ -99,11 +106,16 @@ def run_gradient_descent(): j += 1 temp_parameter_vector = [0, 0, 0, 0] for i in range(0, len(parameter_vector)): - cost_derivative = get_cost_derivative(i-1) - temp_parameter_vector[i] = parameter_vector[i] - \ - LEARNING_RATE*cost_derivative - if numpy.allclose(parameter_vector, temp_parameter_vector, - atol=absolute_error_limit, rtol=relative_error_limit): + cost_derivative = get_cost_derivative(i - 1) + temp_parameter_vector[i] = ( + parameter_vector[i] - LEARNING_RATE * cost_derivative + ) + if numpy.allclose( + parameter_vector, + temp_parameter_vector, + atol=absolute_error_limit, + rtol=relative_error_limit, + ): break parameter_vector = temp_parameter_vector print(("Number of iterations:", j)) @@ -111,11 +123,11 @@ def run_gradient_descent(): def test_gradient_descent(): for i in range(len(test_data)): - print(("Actual output value:", output(i, 'test'))) - print(("Hypothesis output:", calculate_hypothesis_value(i, 'test'))) + print(("Actual output value:", output(i, "test"))) + print(("Hypothesis output:", calculate_hypothesis_value(i, "test"))) -if __name__ == '__main__': +if __name__ == "__main__": run_gradient_descent() print("\nTesting gradient descent for a linear hypothesis function.\n") test_gradient_descent() diff --git a/machine_learning/k_means_clust.py b/machine_learning/k_means_clust.py index d0ce0f2599e0..4c643226b213 100644 --- a/machine_learning/k_means_clust.py +++ b/machine_learning/k_means_clust.py @@ -1,4 +1,4 @@ -'''README, Author - Anurag Kumar(mailto:anuragkumarak95@gmail.com) +"""README, Author - Anurag Kumar(mailto:anuragkumarak95@gmail.com) Requirements: - sklearn @@ -45,17 +45,18 @@ 5. Have fun.. -''' +""" from sklearn.metrics import pairwise_distances import numpy as np -TAG = 'K-MEANS-CLUST/ ' +TAG = "K-MEANS-CLUST/ " + def get_initial_centroids(data, k, seed=None): - '''Randomly choose k data points as initial centroids''' - if seed is not None: # useful for obtaining consistent results + """Randomly choose k data points as initial centroids""" + if seed is not None: # useful for obtaining consistent results np.random.seed(seed) - n = data.shape[0] # number of data points + n = data.shape[0] # number of data points # Pick K indices from range [0, N). rand_indices = np.random.randint(0, n, k) @@ -63,30 +64,33 @@ def get_initial_centroids(data, k, seed=None): # Keep centroids as dense format, as many entries will be nonzero due to averaging. # As long as at least one document in a cluster contains a word, # it will carry a nonzero weight in the TF-IDF vector of the centroid. - centroids = data[rand_indices,:] + centroids = data[rand_indices, :] return centroids -def centroid_pairwise_dist(X,centroids): - return pairwise_distances(X,centroids,metric='euclidean') + +def centroid_pairwise_dist(X, centroids): + return pairwise_distances(X, centroids, metric="euclidean") + def assign_clusters(data, centroids): # Compute distances between each data point and the set of centroids: # Fill in the blank (RHS only) - distances_from_centroids = centroid_pairwise_dist(data,centroids) + distances_from_centroids = centroid_pairwise_dist(data, centroids) # Compute cluster assignments for each data point: # Fill in the blank (RHS only) - cluster_assignment = np.argmin(distances_from_centroids,axis=1) + cluster_assignment = np.argmin(distances_from_centroids, axis=1) return cluster_assignment + def revise_centroids(data, k, cluster_assignment): new_centroids = [] for i in range(k): # Select all data points that belong to cluster i. Fill in the blank (RHS only) - member_data_points = data[cluster_assignment==i] + member_data_points = data[cluster_assignment == i] # Compute the mean of the data points. Fill in the blank (RHS only) centroid = member_data_points.mean(axis=0) new_centroids.append(centroid) @@ -94,79 +98,102 @@ def revise_centroids(data, k, cluster_assignment): return new_centroids + def compute_heterogeneity(data, k, centroids, cluster_assignment): heterogeneity = 0.0 for i in range(k): # Select all data points that belong to cluster i. Fill in the blank (RHS only) - member_data_points = data[cluster_assignment==i, :] + member_data_points = data[cluster_assignment == i, :] - if member_data_points.shape[0] > 0: # check if i-th cluster is non-empty + if member_data_points.shape[0] > 0: # check if i-th cluster is non-empty # Compute distances from centroid to data points (RHS only) - distances = pairwise_distances(member_data_points, [centroids[i]], metric='euclidean') - squared_distances = distances**2 + distances = pairwise_distances( + member_data_points, [centroids[i]], metric="euclidean" + ) + squared_distances = distances ** 2 heterogeneity += np.sum(squared_distances) return heterogeneity + from matplotlib import pyplot as plt + + def plot_heterogeneity(heterogeneity, k): - plt.figure(figsize=(7,4)) + plt.figure(figsize=(7, 4)) plt.plot(heterogeneity, linewidth=4) - plt.xlabel('# Iterations') - plt.ylabel('Heterogeneity') - plt.title('Heterogeneity of clustering over time, K={0:d}'.format(k)) - plt.rcParams.update({'font.size': 16}) + plt.xlabel("# Iterations") + plt.ylabel("Heterogeneity") + plt.title("Heterogeneity of clustering over time, K={0:d}".format(k)) + plt.rcParams.update({"font.size": 16}) plt.show() -def kmeans(data, k, initial_centroids, maxiter=500, record_heterogeneity=None, verbose=False): - '''This function runs k-means on given data and initial set of centroids. + +def kmeans( + data, k, initial_centroids, maxiter=500, record_heterogeneity=None, verbose=False +): + """This function runs k-means on given data and initial set of centroids. maxiter: maximum number of iterations to run.(default=500) record_heterogeneity: (optional) a list, to store the history of heterogeneity as function of iterations if None, do not store the history. - verbose: if True, print how many data points changed their cluster labels in each iteration''' + verbose: if True, print how many data points changed their cluster labels in each iteration""" centroids = initial_centroids[:] prev_cluster_assignment = None for itr in range(maxiter): if verbose: - print(itr, end='') + print(itr, end="") # 1. Make cluster assignments using nearest centroids - cluster_assignment = assign_clusters(data,centroids) + cluster_assignment = assign_clusters(data, centroids) # 2. Compute a new centroid for each of the k clusters, averaging all data points assigned to that cluster. - centroids = revise_centroids(data,k, cluster_assignment) + centroids = revise_centroids(data, k, cluster_assignment) # Check for convergence: if none of the assignments changed, stop - if prev_cluster_assignment is not None and \ - (prev_cluster_assignment==cluster_assignment).all(): + if ( + prev_cluster_assignment is not None + and (prev_cluster_assignment == cluster_assignment).all() + ): break # Print number of new assignments if prev_cluster_assignment is not None: - num_changed = np.sum(prev_cluster_assignment!=cluster_assignment) + num_changed = np.sum(prev_cluster_assignment != cluster_assignment) if verbose: - print(' {0:5d} elements changed their cluster assignment.'.format(num_changed)) + print( + " {0:5d} elements changed their cluster assignment.".format( + num_changed + ) + ) # Record heterogeneity convergence metric if record_heterogeneity is not None: # YOUR CODE HERE - score = compute_heterogeneity(data,k,centroids,cluster_assignment) + score = compute_heterogeneity(data, k, centroids, cluster_assignment) record_heterogeneity.append(score) prev_cluster_assignment = cluster_assignment[:] return centroids, cluster_assignment + # Mock test below -if False: # change to true to run this test case. +if False: # change to true to run this test case. import sklearn.datasets as ds + dataset = ds.load_iris() k = 3 heterogeneity = [] - initial_centroids = get_initial_centroids(dataset['data'], k, seed=0) - centroids, cluster_assignment = kmeans(dataset['data'], k, initial_centroids, maxiter=400, - record_heterogeneity=heterogeneity, verbose=True) + initial_centroids = get_initial_centroids(dataset["data"], k, seed=0) + centroids, cluster_assignment = kmeans( + dataset["data"], + k, + initial_centroids, + maxiter=400, + record_heterogeneity=heterogeneity, + verbose=True, + ) plot_heterogeneity(heterogeneity, k) diff --git a/machine_learning/knn_sklearn.py b/machine_learning/knn_sklearn.py index 64582564304f..a371e30f5403 100644 --- a/machine_learning/knn_sklearn.py +++ b/machine_learning/knn_sklearn.py @@ -2,27 +2,30 @@ from sklearn.datasets import load_iris from sklearn.neighbors import KNeighborsClassifier -#Load iris file +# Load iris file iris = load_iris() iris.keys() -print('Target names: \n {} '.format(iris.target_names)) -print('\n Features: \n {}'.format(iris.feature_names)) +print("Target names: \n {} ".format(iris.target_names)) +print("\n Features: \n {}".format(iris.feature_names)) -#Train set e Test set -X_train, X_test, y_train, y_test = train_test_split(iris['data'],iris['target'], random_state=4) +# Train set e Test set +X_train, X_test, y_train, y_test = train_test_split( + iris["data"], iris["target"], random_state=4 +) -#KNN +# KNN -knn = KNeighborsClassifier (n_neighbors = 1) +knn = KNeighborsClassifier(n_neighbors=1) knn.fit(X_train, y_train) -#new array to test -X_new = [[1,2,1,4], - [2,3,4,5]] +# new array to test +X_new = [[1, 2, 1, 4], [2, 3, 4, 5]] prediction = knn.predict(X_new) -print('\nNew array: \n {}' - '\n\nTarget Names Prediction: \n {}'.format(X_new, iris['target_names'][prediction])) +print( + "\nNew array: \n {}" + "\n\nTarget Names Prediction: \n {}".format(X_new, iris["target_names"][prediction]) +) diff --git a/machine_learning/linear_regression.py b/machine_learning/linear_regression.py index 9d9738fced8d..b666feddccc7 100644 --- a/machine_learning/linear_regression.py +++ b/machine_learning/linear_regression.py @@ -16,21 +16,22 @@ def collect_dataset(): The dataset contains ADR vs Rating of a Player :return : dataset obtained from the link, as matrix """ - response = requests.get('https://raw.githubusercontent.com/yashLadha/' + - 'The_Math_of_Intelligence/master/Week1/ADRvs' + - 'Rating.csv') + response = requests.get( + "https://raw.githubusercontent.com/yashLadha/" + + "The_Math_of_Intelligence/master/Week1/ADRvs" + + "Rating.csv" + ) lines = response.text.splitlines() data = [] for item in lines: - item = item.split(',') + item = item.split(",") data.append(item) data.pop(0) # This is for removing the labels from the list dataset = np.matrix(data) return dataset -def run_steep_gradient_descent(data_x, data_y, - len_data, alpha, theta): +def run_steep_gradient_descent(data_x, data_y, len_data, alpha, theta): """ Run steep gradient descent and updates the Feature vector accordingly_ :param data_x : contains the dataset :param data_y : contains the output associated with each data-entry @@ -79,10 +80,9 @@ def run_linear_regression(data_x, data_y): theta = np.zeros((1, no_features)) for i in range(0, iterations): - theta = run_steep_gradient_descent(data_x, data_y, - len_data, alpha, theta) + theta = run_steep_gradient_descent(data_x, data_y, len_data, alpha, theta) error = sum_of_square_error(data_x, data_y, len_data, theta) - print('At Iteration %d - Error is %.5f ' % (i + 1, error)) + print("At Iteration %d - Error is %.5f " % (i + 1, error)) return theta @@ -97,10 +97,10 @@ def main(): theta = run_linear_regression(data_x, data_y) len_result = theta.shape[1] - print('Resultant Feature vector : ') + print("Resultant Feature vector : ") for i in range(0, len_result): - print('%.5f' % (theta[0, i])) + print("%.5f" % (theta[0, i])) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/machine_learning/logistic_regression.py b/machine_learning/logistic_regression.py index b2749f1be260..f23d400ced55 100644 --- a/machine_learning/logistic_regression.py +++ b/machine_learning/logistic_regression.py @@ -9,8 +9,8 @@ # importing all the required libraries -''' Implementing logistic regression for classification problem - Helpful resources : 1.Coursera ML course 2.https://medium.com/@martinpella/logistic-regression-from-scratch-in-python-124c5636b8ac''' +""" Implementing logistic regression for classification problem + Helpful resources : 1.Coursera ML course 2.https://medium.com/@martinpella/logistic-regression-from-scratch-in-python-124c5636b8ac""" import numpy as np import matplotlib.pyplot as plt @@ -24,6 +24,7 @@ # sigmoid function or logistic function is used as a hypothesis function in classification problems + def sigmoid_function(z): return 1 / (1 + np.exp(-z)) @@ -31,17 +32,14 @@ def sigmoid_function(z): def cost_function(h, y): return (-y * np.log(h) - (1 - y) * np.log(1 - h)).mean() + def log_likelihood(X, Y, weights): scores = np.dot(X, weights) - return np.sum(Y*scores - np.log(1 + np.exp(scores)) ) + return np.sum(Y * scores - np.log(1 + np.exp(scores))) + # here alpha is the learning rate, X is the feature matrix,y is the target matrix -def logistic_reg( - alpha, - X, - y, - max_iterations=70000, - ): +def logistic_reg(alpha, X, y, max_iterations=70000): theta = np.zeros(X.shape[1]) for iterations in range(max_iterations): @@ -53,42 +51,35 @@ def logistic_reg( h = sigmoid_function(z) J = cost_function(h, y) if iterations % 100 == 0: - print(f'loss: {J} \t') # printing the loss after every 100 iterations + print(f"loss: {J} \t") # printing the loss after every 100 iterations return theta + # In[68]: -if __name__ == '__main__': +if __name__ == "__main__": iris = datasets.load_iris() X = iris.data[:, :2] y = (iris.target != 0) * 1 alpha = 0.1 - theta = logistic_reg(alpha,X,y,max_iterations=70000) - print("theta: ",theta) # printing the theta i.e our weights vector - + theta = logistic_reg(alpha, X, y, max_iterations=70000) + print("theta: ", theta) # printing the theta i.e our weights vector def predict_prob(X): - return sigmoid_function(np.dot(X, theta)) # predicting the value of probability from the logistic regression algorithm - + return sigmoid_function( + np.dot(X, theta) + ) # predicting the value of probability from the logistic regression algorithm plt.figure(figsize=(10, 6)) - plt.scatter(X[y == 0][:, 0], X[y == 0][:, 1], color='b', label='0') - plt.scatter(X[y == 1][:, 0], X[y == 1][:, 1], color='r', label='1') + plt.scatter(X[y == 0][:, 0], X[y == 0][:, 1], color="b", label="0") + plt.scatter(X[y == 1][:, 0], X[y == 1][:, 1], color="r", label="1") (x1_min, x1_max) = (X[:, 0].min(), X[:, 0].max()) (x2_min, x2_max) = (X[:, 1].min(), X[:, 1].max()) - (xx1, xx2) = np.meshgrid(np.linspace(x1_min, x1_max), - np.linspace(x2_min, x2_max)) + (xx1, xx2) = np.meshgrid(np.linspace(x1_min, x1_max), np.linspace(x2_min, x2_max)) grid = np.c_[xx1.ravel(), xx2.ravel()] probs = predict_prob(grid).reshape(xx1.shape) - plt.contour( - xx1, - xx2, - probs, - [0.5], - linewidths=1, - colors='black', - ) + plt.contour(xx1, xx2, probs, [0.5], linewidths=1, colors="black") plt.legend() plt.show() diff --git a/machine_learning/random_forest_classification/random_forest_classification.py b/machine_learning/random_forest_classification/random_forest_classification.py index 81016387ecc7..6aed4e6e66de 100644 --- a/machine_learning/random_forest_classification/random_forest_classification.py +++ b/machine_learning/random_forest_classification/random_forest_classification.py @@ -8,23 +8,30 @@ # Importing the dataset script_dir = os.path.dirname(os.path.realpath(__file__)) -dataset = pd.read_csv(os.path.join(script_dir, 'Social_Network_Ads.csv')) +dataset = pd.read_csv(os.path.join(script_dir, "Social_Network_Ads.csv")) X = dataset.iloc[:, [2, 3]].values y = dataset.iloc[:, 4].values # Splitting the dataset into the Training set and Test set from sklearn.model_selection import train_test_split -X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0) + +X_train, X_test, y_train, y_test = train_test_split( + X, y, test_size=0.25, random_state=0 +) # Feature Scaling from sklearn.preprocessing import StandardScaler + sc = StandardScaler() X_train = sc.fit_transform(X_train) X_test = sc.transform(X_test) # Fitting Random Forest Classification to the Training set from sklearn.ensemble import RandomForestClassifier -classifier = RandomForestClassifier(n_estimators = 10, criterion = 'entropy', random_state = 0) + +classifier = RandomForestClassifier( + n_estimators=10, criterion="entropy", random_state=0 +) classifier.fit(X_train, y_train) # Predicting the Test set results @@ -32,40 +39,65 @@ # Making the Confusion Matrix from sklearn.metrics import confusion_matrix + cm = confusion_matrix(y_test, y_pred) # Visualising the Training set results from matplotlib.colors import ListedColormap + X_set, y_set = X_train, y_train -X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01), - np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01)) -plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape), - alpha = 0.75, cmap = ListedColormap(('red', 'green'))) +X1, X2 = np.meshgrid( + np.arange(start=X_set[:, 0].min() - 1, stop=X_set[:, 0].max() + 1, step=0.01), + np.arange(start=X_set[:, 1].min() - 1, stop=X_set[:, 1].max() + 1, step=0.01), +) +plt.contourf( + X1, + X2, + classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape), + alpha=0.75, + cmap=ListedColormap(("red", "green")), +) plt.xlim(X1.min(), X1.max()) plt.ylim(X2.min(), X2.max()) for i, j in enumerate(np.unique(y_set)): - plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1], - c = ListedColormap(('red', 'green'))(i), label = j) -plt.title('Random Forest Classification (Training set)') -plt.xlabel('Age') -plt.ylabel('Estimated Salary') + plt.scatter( + X_set[y_set == j, 0], + X_set[y_set == j, 1], + c=ListedColormap(("red", "green"))(i), + label=j, + ) +plt.title("Random Forest Classification (Training set)") +plt.xlabel("Age") +plt.ylabel("Estimated Salary") plt.legend() plt.show() # Visualising the Test set results from matplotlib.colors import ListedColormap + X_set, y_set = X_test, y_test -X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01), - np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01)) -plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape), - alpha = 0.75, cmap = ListedColormap(('red', 'green'))) +X1, X2 = np.meshgrid( + np.arange(start=X_set[:, 0].min() - 1, stop=X_set[:, 0].max() + 1, step=0.01), + np.arange(start=X_set[:, 1].min() - 1, stop=X_set[:, 1].max() + 1, step=0.01), +) +plt.contourf( + X1, + X2, + classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape), + alpha=0.75, + cmap=ListedColormap(("red", "green")), +) plt.xlim(X1.min(), X1.max()) plt.ylim(X2.min(), X2.max()) for i, j in enumerate(np.unique(y_set)): - plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1], - c = ListedColormap(('red', 'green'))(i), label = j) -plt.title('Random Forest Classification (Test set)') -plt.xlabel('Age') -plt.ylabel('Estimated Salary') + plt.scatter( + X_set[y_set == j, 0], + X_set[y_set == j, 1], + c=ListedColormap(("red", "green"))(i), + label=j, + ) +plt.title("Random Forest Classification (Test set)") +plt.xlabel("Age") +plt.ylabel("Estimated Salary") plt.legend() plt.show() diff --git a/machine_learning/random_forest_regression/random_forest_regression.py b/machine_learning/random_forest_regression/random_forest_regression.py index 85ce0676b598..2599e97e957e 100644 --- a/machine_learning/random_forest_regression/random_forest_regression.py +++ b/machine_learning/random_forest_regression/random_forest_regression.py @@ -8,7 +8,7 @@ # Importing the dataset script_dir = os.path.dirname(os.path.realpath(__file__)) -dataset = pd.read_csv(os.path.join(script_dir, 'Position_Salaries.csv')) +dataset = pd.read_csv(os.path.join(script_dir, "Position_Salaries.csv")) X = dataset.iloc[:, 1:2].values y = dataset.iloc[:, 2].values @@ -26,7 +26,8 @@ # Fitting Random Forest Regression to the dataset from sklearn.ensemble import RandomForestRegressor -regressor = RandomForestRegressor(n_estimators = 10, random_state = 0) + +regressor = RandomForestRegressor(n_estimators=10, random_state=0) regressor.fit(X, y) # Predicting a new result @@ -35,9 +36,9 @@ # Visualising the Random Forest Regression results (higher resolution) X_grid = np.arange(min(X), max(X), 0.01) X_grid = X_grid.reshape((len(X_grid), 1)) -plt.scatter(X, y, color = 'red') -plt.plot(X_grid, regressor.predict(X_grid), color = 'blue') -plt.title('Truth or Bluff (Random Forest Regression)') -plt.xlabel('Position level') -plt.ylabel('Salary') +plt.scatter(X, y, color="red") +plt.plot(X_grid, regressor.predict(X_grid), color="blue") +plt.title("Truth or Bluff (Random Forest Regression)") +plt.xlabel("Position level") +plt.ylabel("Salary") plt.show() diff --git a/machine_learning/scoring_functions.py b/machine_learning/scoring_functions.py index a2d97b09ded2..2b24287b3726 100755 --- a/machine_learning/scoring_functions.py +++ b/machine_learning/scoring_functions.py @@ -14,7 +14,7 @@ and types of data """ -#Mean Absolute Error +# Mean Absolute Error def mae(predict, actual): predict = np.array(predict) actual = np.array(actual) @@ -24,7 +24,8 @@ def mae(predict, actual): return score -#Mean Squared Error + +# Mean Squared Error def mse(predict, actual): predict = np.array(predict) actual = np.array(actual) @@ -35,7 +36,8 @@ def mse(predict, actual): score = square_diff.mean() return score -#Root Mean Squared Error + +# Root Mean Squared Error def rmse(predict, actual): predict = np.array(predict) actual = np.array(actual) @@ -46,13 +48,14 @@ def rmse(predict, actual): score = np.sqrt(mean_square_diff) return score -#Root Mean Square Logarithmic Error + +# Root Mean Square Logarithmic Error def rmsle(predict, actual): predict = np.array(predict) actual = np.array(actual) - log_predict = np.log(predict+1) - log_actual = np.log(actual+1) + log_predict = np.log(predict + 1) + log_actual = np.log(actual + 1) difference = log_predict - log_actual square_diff = np.square(difference) @@ -62,14 +65,15 @@ def rmsle(predict, actual): return score -#Mean Bias Deviation + +# Mean Bias Deviation def mbd(predict, actual): predict = np.array(predict) actual = np.array(actual) difference = predict - actual - numerator = np.sum(difference) / len(predict) - denumerator = np.sum(actual) / len(predict) + numerator = np.sum(difference) / len(predict) + denumerator = np.sum(actual) / len(predict) print(numerator) print(denumerator) diff --git a/machine_learning/sequential_minimum_optimization.py b/machine_learning/sequential_minimum_optimization.py index 0b5d788e92e1..1d4e4a276bc1 100644 --- a/machine_learning/sequential_minimum_optimization.py +++ b/machine_learning/sequential_minimum_optimization.py @@ -41,11 +41,20 @@ from sklearn.datasets import make_blobs, make_circles from sklearn.preprocessing import StandardScaler -CANCER_DATASET_URL = 'http://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/wdbc.data' +CANCER_DATASET_URL = "http://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/wdbc.data" class SmoSVM(object): - def __init__(self, train, kernel_func, alpha_list=None, cost=0.4, b=0.0, tolerance=0.001, auto_norm=True): + def __init__( + self, + train, + kernel_func, + alpha_list=None, + cost=0.4, + b=0.0, + tolerance=0.001, + auto_norm=True, + ): self._init = True self._auto_norm = auto_norm self._c = np.float64(cost) @@ -91,13 +100,25 @@ def fit(self): self.alphas[i1], self.alphas[i2] = a1_new, a2_new # 3: update threshold(b) - b1_new = np.float64(-e1 - y1 * K(i1, i1) * (a1_new - a1) - y2 * K(i2, i1) * (a2_new - a2) + self._b) - b2_new = np.float64(-e2 - y2 * K(i2, i2) * (a2_new - a2) - y1 * K(i1, i2) * (a1_new - a1) + self._b) + b1_new = np.float64( + -e1 + - y1 * K(i1, i1) * (a1_new - a1) + - y2 * K(i2, i1) * (a2_new - a2) + + self._b + ) + b2_new = np.float64( + -e2 + - y2 * K(i2, i2) * (a2_new - a2) + - y1 * K(i1, i2) * (a1_new - a1) + + self._b + ) if 0.0 < a1_new < self._c: b = b1_new if 0.0 < a2_new < self._c: b = b2_new - if not (np.float64(0) < a2_new < self._c) and not (np.float64(0) < a1_new < self._c): + if not (np.float64(0) < a2_new < self._c) and not ( + np.float64(0) < a1_new < self._c + ): b = (b1_new + b2_new) / 2.0 b_old = self._b self._b = b @@ -107,7 +128,11 @@ def fit(self): for s in self.unbound: if s == i1 or s == i2: continue - self._error[s] += y1 * (a1_new - a1) * K(i1, s) + y2 * (a2_new - a2) * K(i2, s) + (self._b - b_old) + self._error[s] += ( + y1 * (a1_new - a1) * K(i1, s) + + y2 * (a2_new - a2) * K(i2, s) + + (self._b - b_old) + ) # if i1 or i2 is non-bound,update there error value to zero if self._is_unbound(i1): @@ -119,7 +144,9 @@ def fit(self): def predict(self, test_samples, classify=True): if test_samples.shape[1] > self.samples.shape[1]: - raise ValueError("Test samples' feature length does not equal to that of train samples") + raise ValueError( + "Test samples' feature length does not equal to that of train samples" + ) if self._auto_norm: test_samples = self._norm(test_samples) @@ -173,14 +200,23 @@ def _calculate_k_matrix(self): k_matrix = np.zeros([self.length, self.length]) for i in self._all_samples: for j in self._all_samples: - k_matrix[i, j] = np.float64(self.Kernel(self.samples[i, :], self.samples[j, :])) + k_matrix[i, j] = np.float64( + self.Kernel(self.samples[i, :], self.samples[j, :]) + ) return k_matrix # Predict test sample's tag def _predict(self, sample): k = self._k - predicted_value = np.sum( - [self.alphas[i1] * self.tags[i1] * k(i1, sample) for i1 in self._all_samples]) + self._b + predicted_value = ( + np.sum( + [ + self.alphas[i1] * self.tags[i1] * k(i1, sample) + for i1 in self._all_samples + ] + ) + + self._b + ) return predicted_value # Choose alpha1 and alpha2 @@ -200,23 +236,27 @@ def _choose_a1(self): while True: all_not_obey = True # all sample - print('scanning all sample!') + print("scanning all sample!") for i1 in [i for i in self._all_samples if self._check_obey_kkt(i)]: all_not_obey = False yield from self._choose_a2(i1) # non-bound sample - print('scanning non-bound sample!') + print("scanning non-bound sample!") while True: not_obey = True - for i1 in [i for i in self._all_samples if self._check_obey_kkt(i) and self._is_unbound(i)]: + for i1 in [ + i + for i in self._all_samples + if self._check_obey_kkt(i) and self._is_unbound(i) + ]: not_obey = False yield from self._choose_a2(i1) if not_obey: - print('all non-bound samples fit the KKT condition!') + print("all non-bound samples fit the KKT condition!") break if all_not_obey: - print('all samples fit the KKT condition! Optimization done!') + print("all samples fit the KKT condition! Optimization done!") break return False @@ -231,7 +271,11 @@ def _choose_a2(self, i1): if len(self.unbound) > 0: tmp_error = self._error.copy().tolist() - tmp_error_dict = {index: value for index, value in enumerate(tmp_error) if self._is_unbound(index)} + tmp_error_dict = { + index: value + for index, value in enumerate(tmp_error) + if self._is_unbound(index) + } if self._e(i1) >= 0: i2 = min(tmp_error_dict, key=lambda index: tmp_error_dict[index]) else: @@ -289,8 +333,20 @@ def _get_new_alpha(self, i1, i2, a1, a2, e1, e2, y1, y2): # way 1 f1 = y1 * (e1 + b) - a1 * K(i1, i1) - s * a2 * K(i1, i2) f2 = y2 * (e2 + b) - a2 * K(i2, i2) - s * a1 * K(i1, i2) - ol = l1 * f1 + L * f2 + 1 / 2 * l1 ** 2 * K(i1, i1) + 1 / 2 * L ** 2 * K(i2, i2) + s * L * l1 * K(i1, i2) - oh = h1 * f1 + H * f2 + 1 / 2 * h1 ** 2 * K(i1, i1) + 1 / 2 * H ** 2 * K(i2, i2) + s * H * h1 * K(i1, i2) + ol = ( + l1 * f1 + + L * f2 + + 1 / 2 * l1 ** 2 * K(i1, i1) + + 1 / 2 * L ** 2 * K(i2, i2) + + s * L * l1 * K(i1, i2) + ) + oh = ( + h1 * f1 + + H * f2 + + 1 / 2 * h1 ** 2 * K(i1, i1) + + 1 / 2 * H ** 2 * K(i2, i2) + + s * H * h1 * K(i1, i2) + ) """ # way 2 Use objective function check which alpha2 new could get the minimal objectives @@ -370,14 +426,10 @@ def _rbf(self, v1, v2): def _check(self): if self._kernel == self._rbf: if self.gamma < 0: - raise ValueError('gamma value must greater than 0') + raise ValueError("gamma value must greater than 0") def _get_kernel(self, kernel_name): - maps = { - 'linear': self._linear, - 'poly': self._polynomial, - 'rbf': self._rbf - } + maps = {"linear": self._linear, "poly": self._polynomial, "rbf": self._rbf} return maps[kernel_name] def __call__(self, v1, v2): @@ -390,34 +442,35 @@ def __repr__(self): def count_time(func): def call_func(*args, **kwargs): import time + start_time = time.time() func(*args, **kwargs) end_time = time.time() - print('smo algorithm cost {} seconds'.format(end_time - start_time)) + print("smo algorithm cost {} seconds".format(end_time - start_time)) return call_func @count_time def test_cancel_data(): - print('Hello!\r\nStart test svm by smo algorithm!') + print("Hello!\r\nStart test svm by smo algorithm!") # 0: download dataset and load into pandas' dataframe - if not os.path.exists(r'cancel_data.csv'): + if not os.path.exists(r"cancel_data.csv"): request = urllib.request.Request( CANCER_DATASET_URL, - headers={'User-Agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'} + headers={"User-Agent": "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)"}, ) response = urllib.request.urlopen(request) - content = response.read().decode('utf-8') - with open(r'cancel_data.csv', 'w') as f: + content = response.read().decode("utf-8") + with open(r"cancel_data.csv", "w") as f: f.write(content) - data = pd.read_csv(r'cancel_data.csv', header=None) + data = pd.read_csv(r"cancel_data.csv", header=None) # 1: pre-processing data del data[data.columns.tolist()[0]] data = data.dropna(axis=0) - data = data.replace({'M': np.float64(1), 'B': np.float64(-1)}) + data = data.replace({"M": np.float64(1), "B": np.float64(-1)}) samples = np.array(data)[:, :] # 2: deviding data into train_data data and test_data data @@ -425,11 +478,18 @@ def test_cancel_data(): test_tags, test_samples = test_data[:, 0], test_data[:, 1:] # 3: choose kernel function,and set initial alphas to zero(optional) - mykernel = Kernel(kernel='rbf', degree=5, coef0=1, gamma=0.5) + mykernel = Kernel(kernel="rbf", degree=5, coef0=1, gamma=0.5) al = np.zeros(train_data.shape[0]) # 4: calculating best alphas using SMO algorithm and predict test_data samples - mysvm = SmoSVM(train=train_data, kernel_func=mykernel, alpha_list=al, cost=0.4, b=0.0, tolerance=0.001) + mysvm = SmoSVM( + train=train_data, + kernel_func=mykernel, + alpha_list=al, + cost=0.4, + b=0.0, + tolerance=0.001, + ) mysvm.fit() predict = mysvm.predict(test_samples) @@ -439,14 +499,18 @@ def test_cancel_data(): for i in range(test_tags.shape[0]): if test_tags[i] == predict[i]: score += 1 - print('\r\nall: {}\r\nright: {}\r\nfalse: {}'.format(test_num, score, test_num - score)) + print( + "\r\nall: {}\r\nright: {}\r\nfalse: {}".format( + test_num, score, test_num - score + ) + ) print("Rough Accuracy: {}".format(score / test_tags.shape[0])) def test_demonstration(): # change stdout - print('\r\nStart plot,please wait!!!') - sys.stdout = open(os.devnull, 'w') + print("\r\nStart plot,please wait!!!") + sys.stdout = open(os.devnull, "w") ax1 = plt.subplot2grid((2, 2), (0, 0)) ax2 = plt.subplot2grid((2, 2), (0, 1)) @@ -464,32 +528,50 @@ def test_demonstration(): sys.stdout = sys.__stdout__ print("Plot done!!!") + def test_linear_kernel(ax, cost): - train_x, train_y = make_blobs(n_samples=500, centers=2, - n_features=2, random_state=1) + train_x, train_y = make_blobs( + n_samples=500, centers=2, n_features=2, random_state=1 + ) train_y[train_y == 0] = -1 scaler = StandardScaler() train_x_scaled = scaler.fit_transform(train_x, train_y) train_data = np.hstack((train_y.reshape(500, 1), train_x_scaled)) - mykernel = Kernel(kernel='linear', degree=5, coef0=1, gamma=0.5) - mysvm = SmoSVM(train=train_data, kernel_func=mykernel, cost=cost, tolerance=0.001, auto_norm=False) + mykernel = Kernel(kernel="linear", degree=5, coef0=1, gamma=0.5) + mysvm = SmoSVM( + train=train_data, + kernel_func=mykernel, + cost=cost, + tolerance=0.001, + auto_norm=False, + ) mysvm.fit() plot_partition_boundary(mysvm, train_data, ax=ax) def test_rbf_kernel(ax, cost): - train_x, train_y = make_circles(n_samples=500, noise=0.1, factor=0.1, random_state=1) + train_x, train_y = make_circles( + n_samples=500, noise=0.1, factor=0.1, random_state=1 + ) train_y[train_y == 0] = -1 scaler = StandardScaler() train_x_scaled = scaler.fit_transform(train_x, train_y) train_data = np.hstack((train_y.reshape(500, 1), train_x_scaled)) - mykernel = Kernel(kernel='rbf', degree=5, coef0=1, gamma=0.5) - mysvm = SmoSVM(train=train_data, kernel_func=mykernel, cost=cost, tolerance=0.001, auto_norm=False) + mykernel = Kernel(kernel="rbf", degree=5, coef0=1, gamma=0.5) + mysvm = SmoSVM( + train=train_data, + kernel_func=mykernel, + cost=cost, + tolerance=0.001, + auto_norm=False, + ) mysvm.fit() plot_partition_boundary(mysvm, train_data, ax=ax) -def plot_partition_boundary(model, train_data, ax, resolution=100, colors=('b', 'k', 'r')): +def plot_partition_boundary( + model, train_data, ax, resolution=100, colors=("b", "k", "r") +): """ We can not get the optimum w of our kernel svm model which is different from linear svm. For this reason, we generate randomly destributed points with high desity and prediced values of these points are @@ -502,25 +584,44 @@ def plot_partition_boundary(model, train_data, ax, resolution=100, colors=('b', train_data_tags = train_data[:, 0] xrange = np.linspace(train_data_x.min(), train_data_x.max(), resolution) yrange = np.linspace(train_data_y.min(), train_data_y.max(), resolution) - test_samples = np.array([(x, y) for x in xrange for y in yrange]).reshape(resolution * resolution, 2) + test_samples = np.array([(x, y) for x in xrange for y in yrange]).reshape( + resolution * resolution, 2 + ) test_tags = model.predict(test_samples, classify=False) grid = test_tags.reshape((len(xrange), len(yrange))) # Plot contour map which represents the partition boundary - ax.contour(xrange, yrange, np.mat(grid).T, levels=(-1, 0, 1), linestyles=('--', '-', '--'), - linewidths=(1, 1, 1), - colors=colors) + ax.contour( + xrange, + yrange, + np.mat(grid).T, + levels=(-1, 0, 1), + linestyles=("--", "-", "--"), + linewidths=(1, 1, 1), + colors=colors, + ) # Plot all train samples - ax.scatter(train_data_x, train_data_y, c=train_data_tags, cmap=plt.cm.Dark2, lw=0, alpha=0.5) + ax.scatter( + train_data_x, + train_data_y, + c=train_data_tags, + cmap=plt.cm.Dark2, + lw=0, + alpha=0.5, + ) # Plot support vectors support = model.support - ax.scatter(train_data_x[support], train_data_y[support], c=train_data_tags[support], cmap=plt.cm.Dark2) + ax.scatter( + train_data_x[support], + train_data_y[support], + c=train_data_tags[support], + cmap=plt.cm.Dark2, + ) -if __name__ == '__main__': +if __name__ == "__main__": test_cancel_data() test_demonstration() plt.show() - diff --git a/maths/3n+1.py b/maths/3n+1.py index d6c14ff0f47d..6b2dfc785794 100644 --- a/maths/3n+1.py +++ b/maths/3n+1.py @@ -1,6 +1,7 @@ from typing import Tuple, List -def n31(a: int) -> Tuple[List[int], int]: + +def n31(a: int) -> Tuple[List[int], int]: """ Returns the Collatz sequence and its length of any postiver integer. >>> n31(4) @@ -8,23 +9,29 @@ def n31(a: int) -> Tuple[List[int], int]: """ if not isinstance(a, int): - raise TypeError('Must be int, not {0}'.format(type(a).__name__)) - if a < 1: - raise ValueError('Given integer must be greater than 1, not {0}'.format(a)) - + raise TypeError("Must be int, not {0}".format(type(a).__name__)) + if a < 1: + raise ValueError("Given integer must be greater than 1, not {0}".format(a)) + path = [a] while a != 1: if a % 2 == 0: a = a // 2 else: - a = 3*a +1 + a = 3 * a + 1 path += [a] return path, len(path) + def main(): num = 4 - path , length = n31(num) - print("The Collatz sequence of {0} took {1} steps. \nPath: {2}".format(num,length, path)) + path, length = n31(num) + print( + "The Collatz sequence of {0} took {1} steps. \nPath: {2}".format( + num, length, path + ) + ) + -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/maths/abs.py b/maths/abs.py index 2734e58ceee6..4d15ee6e82a8 100644 --- a/maths/abs.py +++ b/maths/abs.py @@ -22,5 +22,5 @@ def main(): print(abs_val(-34)) # = 34 -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/maths/abs_max.py b/maths/abs_max.py index 28f631f0100e..554e27f6ee66 100644 --- a/maths/abs_max.py +++ b/maths/abs_max.py @@ -1,4 +1,5 @@ -from typing import List +from typing import List + def abs_max(x: List[int]) -> int: """ @@ -7,12 +8,13 @@ def abs_max(x: List[int]) -> int: >>> abs_max([3,-10,-2]) -10 """ - j =x[0] + j = x[0] for i in x: if abs(i) > abs(j): j = i return j + def abs_max_sort(x): """ >>> abs_max_sort([0,5,1,11]) @@ -20,13 +22,14 @@ def abs_max_sort(x): >>> abs_max_sort([3,-10,-2]) -10 """ - return sorted(x,key=abs)[-1] + return sorted(x, key=abs)[-1] + def main(): - a = [1,2,-11] + a = [1, 2, -11] assert abs_max(a) == -11 assert abs_max_sort(a) == -11 -if __name__ == '__main__': - main() +if __name__ == "__main__": + main() diff --git a/maths/abs_min.py b/maths/abs_min.py index abb0c9051b7d..eb84de37ce23 100644 --- a/maths/abs_min.py +++ b/maths/abs_min.py @@ -16,9 +16,9 @@ def absMin(x): def main(): - a = [-3,-1,2,-11] + a = [-3, -1, 2, -11] print(absMin(a)) # = -1 -if __name__ == '__main__': - main() \ No newline at end of file +if __name__ == "__main__": + main() diff --git a/maths/average_mean.py b/maths/average_mean.py index 78387111022d..e04b63be0e19 100644 --- a/maths/average_mean.py +++ b/maths/average_mean.py @@ -16,5 +16,5 @@ def main(): average([2, 4, 6, 8, 20, 50, 70]) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/maths/average_median.py b/maths/average_median.py index eab0107d8da8..ccb250d7718c 100644 --- a/maths/average_median.py +++ b/maths/average_median.py @@ -24,11 +24,13 @@ def median(nums): med = sorted_list[mid_index] return med + def main(): print("Odd number of numbers:") print(median([2, 4, 6, 8, 20, 50, 70])) print("Even number of numbers:") print(median([2, 4, 6, 8, 20, 50])) -if __name__ == '__main__': + +if __name__ == "__main__": main() diff --git a/maths/basic_maths.py b/maths/basic_maths.py index cd7bac0113b8..34ffd1031527 100644 --- a/maths/basic_maths.py +++ b/maths/basic_maths.py @@ -49,7 +49,7 @@ def sum_of_divisors(n): temp += 1 n = int(n / 2) if temp > 1: - s *= (2**temp - 1) / (2 - 1) + s *= (2 ** temp - 1) / (2 - 1) for i in range(3, int(math.sqrt(n)) + 1, 2): temp = 1 @@ -57,7 +57,7 @@ def sum_of_divisors(n): temp += 1 n = int(n / i) if temp > 1: - s *= (i**temp - 1) / (i - 1) + s *= (i ** temp - 1) / (i - 1) return s @@ -80,5 +80,5 @@ def main(): print(euler_phi(100)) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/maths/binary_exponentiation.py b/maths/binary_exponentiation.py index a8d736adfea0..57c4b8686f5c 100644 --- a/maths/binary_exponentiation.py +++ b/maths/binary_exponentiation.py @@ -6,10 +6,10 @@ def binary_exponentiation(a, n): - if (n == 0): + if n == 0: return 1 - elif (n % 2 == 1): + elif n % 2 == 1: return binary_exponentiation(a, n - 1) * a else: diff --git a/maths/collatz_sequence.py b/maths/collatz_sequence.py index 9f88453d518b..c83da3f0f0e8 100644 --- a/maths/collatz_sequence.py +++ b/maths/collatz_sequence.py @@ -10,10 +10,10 @@ def collatz_sequence(n): """ sequence = [n] while n != 1: - if n % 2 == 0:# even - n //= 2 + if n % 2 == 0: # even + n //= 2 else: - n = 3*n +1 + n = 3 * n + 1 sequence.append(n) return sequence @@ -22,7 +22,8 @@ def main(): n = 43 sequence = collatz_sequence(n) print(sequence) - print("collatz sequence from %d took %d steps."%(n,len(sequence))) + print("collatz sequence from %d took %d steps." % (n, len(sequence))) -if __name__ == '__main__': + +if __name__ == "__main__": main() diff --git a/maths/extended_euclidean_algorithm.py b/maths/extended_euclidean_algorithm.py index fc3798e7e432..fe81bcfaf71d 100644 --- a/maths/extended_euclidean_algorithm.py +++ b/maths/extended_euclidean_algorithm.py @@ -61,12 +61,12 @@ def extended_euclidean_algorithm(m, n): def main(): """Call Extended Euclidean Algorithm.""" if len(sys.argv) < 3: - print('2 integer arguments required') + print("2 integer arguments required") exit(1) m = int(sys.argv[1]) n = int(sys.argv[2]) print(extended_euclidean_algorithm(m, n)) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/maths/factorial_recursive.py b/maths/factorial_recursive.py index 06173dcbcd7d..f346c65f1962 100644 --- a/maths/factorial_recursive.py +++ b/maths/factorial_recursive.py @@ -11,4 +11,4 @@ def fact(n): where i ranges from 1 to 20. """ for i in range(1, 21): - print(i, ": ", fact(i), sep='') + print(i, ": ", fact(i), sep="") diff --git a/maths/fermat_little_theorem.py b/maths/fermat_little_theorem.py index 8cf60dafe3ca..24d558115795 100644 --- a/maths/fermat_little_theorem.py +++ b/maths/fermat_little_theorem.py @@ -6,10 +6,10 @@ def binary_exponentiation(a, n, mod): - if (n == 0): + if n == 0: return 1 - elif (n % 2 == 1): + elif n % 2 == 1: return (binary_exponentiation(a, n - 1, mod) * a) % mod else: diff --git a/maths/fibonacci.py b/maths/fibonacci.py index 0a0611f21379..5ba9f6636364 100644 --- a/maths/fibonacci.py +++ b/maths/fibonacci.py @@ -21,10 +21,11 @@ def timer_wrapper(*args, **kwargs): func(*args, **kwargs) end = time.time() if int(end - start) > 0: - print(f'Run time for {func.__name__}: {(end - start):0.2f}s') + print(f"Run time for {func.__name__}: {(end - start):0.2f}s") else: - print(f'Run time for {func.__name__}: {(end - start)*1000:0.2f}ms') + print(f"Run time for {func.__name__}: {(end - start)*1000:0.2f}ms") return func(*args, **kwargs) + return timer_wrapper @@ -69,9 +70,13 @@ def _check_number_input(n, min_thresh, max_thresh=None): except ValueLessThanZero: print("Incorrect Input: number must not be less than 0") except ValueTooSmallError: - print(f'Incorrect Input: input number must be > {min_thresh} for the recursive calculation') + print( + f"Incorrect Input: input number must be > {min_thresh} for the recursive calculation" + ) except ValueTooLargeError: - print(f'Incorrect Input: input number must be < {max_thresh} for the recursive calculation') + print( + f"Incorrect Input: input number must be < {max_thresh} for the recursive calculation" + ) return False @@ -86,8 +91,8 @@ def fib_iterative(n): if _check_number_input(n, 2): seq_out = [0, 1] a, b = 0, 1 - for _ in range(n-len(seq_out)): - a, b = b, a+b + for _ in range(n - len(seq_out)): + a, b = b, a + b seq_out.append(b) return seq_out @@ -106,12 +111,14 @@ def fib_formula(n): phi_1 = Decimal(1 + sqrt) / Decimal(2) phi_2 = Decimal(1 - sqrt) / Decimal(2) for i in range(2, n): - temp_out = ((phi_1**Decimal(i)) - (phi_2**Decimal(i))) * (Decimal(sqrt) ** Decimal(-1)) + temp_out = ((phi_1 ** Decimal(i)) - (phi_2 ** Decimal(i))) * ( + Decimal(sqrt) ** Decimal(-1) + ) seq_out.append(int(temp_out)) return seq_out -if __name__ == '__main__': +if __name__ == "__main__": num = 20 # print(f'{fib_recursive(num)}\n') # print(f'{fib_iterative(num)}\n') diff --git a/maths/fibonacci_sequence_recursion.py b/maths/fibonacci_sequence_recursion.py index 9190e7fc7a40..3a565a458631 100644 --- a/maths/fibonacci_sequence_recursion.py +++ b/maths/fibonacci_sequence_recursion.py @@ -1,14 +1,17 @@ # Fibonacci Sequence Using Recursion + def recur_fibo(n): if n <= 1: return n else: - (recur_fibo(n-1) + recur_fibo(n-2)) + (recur_fibo(n - 1) + recur_fibo(n - 2)) + def isPositiveInteger(limit): return limit >= 0 + def main(): limit = int(input("How many terms to include in fibonacci series: ")) if isPositiveInteger(limit): @@ -17,5 +20,6 @@ def main(): else: print("Please enter a positive integer: ") -if __name__ == '__main__': + +if __name__ == "__main__": main() diff --git a/maths/find_lcm.py b/maths/find_lcm.py index f7ac958070b5..dffadd1f3c5b 100644 --- a/maths/find_lcm.py +++ b/maths/find_lcm.py @@ -10,14 +10,14 @@ def find_lcm(num_1, num_2): >>> find_lcm(12,76) 228 """ - if num_1>=num_2: - max_num=num_1 + if num_1 >= num_2: + max_num = num_1 else: - max_num=num_2 - + max_num = num_2 + lcm = max_num while True: - if ((lcm % num_1 == 0) and (lcm % num_2 == 0)): + if (lcm % num_1 == 0) and (lcm % num_2 == 0): break lcm += max_num return lcm @@ -25,10 +25,10 @@ def find_lcm(num_1, num_2): def main(): """Use test numbers to run the find_lcm algorithm.""" - num_1 = int(input().strip()) - num_2 = int(input().strip()) + num_1 = int(input().strip()) + num_2 = int(input().strip()) print(find_lcm(num_1, num_2)) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/maths/find_max.py b/maths/find_max.py index 0ce49a68c348..7cc82aacfb09 100644 --- a/maths/find_max.py +++ b/maths/find_max.py @@ -1,14 +1,17 @@ # NguyenU + def find_max(nums): max = nums[0] for x in nums: - if x > max: - max = x + if x > max: + max = x print(max) + def main(): - find_max([2, 4, 9, 7, 19, 94, 5]) + find_max([2, 4, 9, 7, 19, 94, 5]) + -if __name__ == '__main__': - main() +if __name__ == "__main__": + main() diff --git a/maths/find_min.py b/maths/find_min.py index c720da268a25..e24982a9369b 100644 --- a/maths/find_min.py +++ b/maths/find_min.py @@ -3,6 +3,7 @@ def main(): """Find Minimum Number in a List.""" + def find_min(x): min_num = x[0] for i in x: @@ -13,5 +14,5 @@ def find_min(x): print(find_min([0, 1, 2, 3, 4, 5, -3, 24, -56])) # = -56 -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/maths/gaussian.py b/maths/gaussian.py index f3a47a3f6a1b..e5f55dfaffd1 100644 --- a/maths/gaussian.py +++ b/maths/gaussian.py @@ -1,4 +1,3 @@ - """ Reference: https://en.wikipedia.org/wiki/Gaussian_function @@ -9,7 +8,6 @@ from numpy import pi, sqrt, exp - def gaussian(x, mu: float = 0.0, sigma: float = 1.0) -> int: """ >>> gaussian(1) diff --git a/maths/greater_common_divisor.py b/maths/greater_common_divisor.py index adc7811e8317..ec608488a61f 100644 --- a/maths/greater_common_divisor.py +++ b/maths/greater_common_divisor.py @@ -13,7 +13,7 @@ def gcd(a, b): def main(): """Call GCD Function.""" try: - nums = input("Enter two Integers separated by comma (,): ").split(',') + nums = input("Enter two Integers separated by comma (,): ").split(",") num_1 = int(nums[0]) num_2 = int(nums[1]) except (IndexError, UnboundLocalError, ValueError): @@ -21,5 +21,5 @@ def main(): print(f"gcd({num_1}, {num_2}) = {gcd(num_1, num_2)}") -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/maths/lucas_series.py b/maths/lucas_series.py index 9ae437dc9f54..22ad893a6567 100644 --- a/maths/lucas_series.py +++ b/maths/lucas_series.py @@ -1,5 +1,6 @@ # Lucas Sequence Using Recursion + def recur_luc(n): """ >>> recur_luc(1) diff --git a/maths/matrix_exponentiation.py b/maths/matrix_exponentiation.py index f80f6c3cad5e..c20292735a92 100644 --- a/maths/matrix_exponentiation.py +++ b/maths/matrix_exponentiation.py @@ -85,8 +85,9 @@ def simple_fibonacci_time(): """ code = "simple_fibonacci(randint(1,70000), 1, 1)" exec_time = timeit.timeit(setup=setup, stmt=code, number=100) - print("Without matrix exponentiation the average execution time is ", - exec_time / 100) + print( + "Without matrix exponentiation the average execution time is ", exec_time / 100 + ) return exec_time diff --git a/maths/modular_exponential.py b/maths/modular_exponential.py index 750de7cba99e..8715e17147ff 100644 --- a/maths/modular_exponential.py +++ b/maths/modular_exponential.py @@ -21,5 +21,5 @@ def main(): print(modular_exponential(3, 200, 13)) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/maths/newton_raphson.py b/maths/newton_raphson.py index d89f264acdd8..093cc4438416 100644 --- a/maths/newton_raphson.py +++ b/maths/newton_raphson.py @@ -1,4 +1,4 @@ -''' +""" Author: P Shreyas Shetty Implementation of Newton-Raphson method for solving equations of kind f(x) = 0. It is an iterative method where solution is found by the expression @@ -6,27 +6,29 @@ If no solution exists, then either the solution will not be found when iteration limit is reached or the gradient f'(x[n]) approaches zero. In both cases, exception is raised. If iteration limit is reached, try increasing maxiter. - ''' + """ import math as m + def calc_derivative(f, a, h=0.001): - ''' + """ Calculates derivative at point a for function f using finite difference method - ''' - return (f(a+h)-f(a-h))/(2*h) + """ + return (f(a + h) - f(a - h)) / (2 * h) + + +def newton_raphson(f, x0=0, maxiter=100, step=0.0001, maxerror=1e-6, logsteps=False): -def newton_raphson(f, x0=0, maxiter=100, step=0.0001, maxerror=1e-6,logsteps=False): - - a = x0 #set the initial guess + a = x0 # set the initial guess steps = [a] error = abs(f(a)) - f1 = lambda x:calc_derivative(f, x, h=step) #Derivative of f(x) + f1 = lambda x: calc_derivative(f, x, h=step) # Derivative of f(x) for _ in range(maxiter): if f1(a) == 0: raise ValueError("No converging solution found") - a = a - f(a)/f1(a) #Calculate the next estimate + a = a - f(a) / f1(a) # Calculate the next estimate if logsteps: steps.append(a) if error < maxerror: @@ -34,14 +36,18 @@ def newton_raphson(f, x0=0, maxiter=100, step=0.0001, maxerror=1e-6,logsteps=Fal else: raise ValueError("Iteration limit reached, no converging solution found") if logsteps: - #If logstep is true, then log intermediate steps + # If logstep is true, then log intermediate steps return a, error, steps return a, error - -if __name__ == '__main__': + + +if __name__ == "__main__": import matplotlib.pyplot as plt - f = lambda x:m.tanh(x)**2-m.exp(3*x) - solution, error, steps = newton_raphson(f, x0=10, maxiter=1000, step=1e-6, logsteps=True) + + f = lambda x: m.tanh(x) ** 2 - m.exp(3 * x) + solution, error, steps = newton_raphson( + f, x0=10, maxiter=1000, step=1e-6, logsteps=True + ) plt.plot([abs(f(x)) for x in steps]) plt.xlabel("step") plt.ylabel("error") diff --git a/maths/polynomial_evaluation.py b/maths/polynomial_evaluation.py index b4f18b9fa106..3c91ecd93031 100644 --- a/maths/polynomial_evaluation.py +++ b/maths/polynomial_evaluation.py @@ -11,7 +11,7 @@ def evaluate_poly(poly, x): 79800.0 """ - return sum(c*(x**i) for i, c in enumerate(poly)) + return sum(c * (x ** i) for i, c in enumerate(poly)) if __name__ == "__main__": diff --git a/maths/prime_check.py b/maths/prime_check.py index 9249834dc069..e60281228fda 100644 --- a/maths/prime_check.py +++ b/maths/prime_check.py @@ -40,12 +40,13 @@ def test_primes(self): self.assertTrue(prime_check(29)) def test_not_primes(self): - self.assertFalse(prime_check(-19), - "Negative numbers are not prime.") - self.assertFalse(prime_check(0), - "Zero doesn't have any divider, primes must have two") - self.assertFalse(prime_check(1), - "One just have 1 divider, primes must have two.") + self.assertFalse(prime_check(-19), "Negative numbers are not prime.") + self.assertFalse( + prime_check(0), "Zero doesn't have any divider, primes must have two" + ) + self.assertFalse( + prime_check(1), "One just have 1 divider, primes must have two." + ) self.assertFalse(prime_check(2 * 2)) self.assertFalse(prime_check(2 * 3)) self.assertFalse(prime_check(3 * 3)) @@ -53,5 +54,5 @@ def test_not_primes(self): self.assertFalse(prime_check(3 * 5 * 7)) -if __name__ == '__main__': +if __name__ == "__main__": unittest.main() diff --git a/maths/radix2_fft.py b/maths/radix2_fft.py index c7ffe96528b4..3911fea1d04d 100644 --- a/maths/radix2_fft.py +++ b/maths/radix2_fft.py @@ -65,12 +65,7 @@ def __init__(self, polyA=[0], polyB=[0]): # Add 0 to make lengths equal a power of 2 self.C_max_length = int( - 2 - ** np.ceil( - np.log2( - len(self.polyA) + len(self.polyB) - 1 - ) - ) + 2 ** np.ceil(np.log2(len(self.polyA) + len(self.polyB) - 1)) ) while len(self.polyA) < self.C_max_length: @@ -78,9 +73,7 @@ def __init__(self, polyA=[0], polyB=[0]): while len(self.polyB) < self.C_max_length: self.polyB.append(0) # A complex root used for the fourier transform - self.root = complex( - mpmath.root(x=1, n=self.C_max_length, k=1) - ) + self.root = complex(mpmath.root(x=1, n=self.C_max_length, k=1)) # The product self.product = self.__multiply() @@ -102,27 +95,15 @@ def __DFT(self, which): # First half of next step current_root = 1 - for j in range( - self.C_max_length // (next_ncol * 2) - ): + for j in range(self.C_max_length // (next_ncol * 2)): for i in range(next_ncol): - new_dft[i].append( - dft[i][j] - + current_root - * dft[i + next_ncol][j] - ) + new_dft[i].append(dft[i][j] + current_root * dft[i + next_ncol][j]) current_root *= root # Second half of next step current_root = 1 - for j in range( - self.C_max_length // (next_ncol * 2) - ): + for j in range(self.C_max_length // (next_ncol * 2)): for i in range(next_ncol): - new_dft[i].append( - dft[i][j] - - current_root - * dft[i + next_ncol][j] - ) + new_dft[i].append(dft[i][j] - current_root * dft[i + next_ncol][j]) current_root *= root # Update dft = new_dft @@ -133,12 +114,7 @@ def __DFT(self, which): def __multiply(self): dftA = self.__DFT("A") dftB = self.__DFT("B") - inverseC = [ - [ - dftA[i] * dftB[i] - for i in range(self.C_max_length) - ] - ] + inverseC = [[dftA[i] * dftB[i] for i in range(self.C_max_length)]] del dftA del dftB @@ -158,11 +134,7 @@ def __multiply(self): new_inverseC[i].append( ( inverseC[i][j] - + inverseC[i][ - j - + self.C_max_length - // next_ncol - ] + + inverseC[i][j + self.C_max_length // next_ncol] ) / 2 ) @@ -170,11 +142,7 @@ def __multiply(self): new_inverseC[i + next_ncol // 2].append( ( inverseC[i][j] - - inverseC[i][ - j - + self.C_max_length - // next_ncol - ] + - inverseC[i][j + self.C_max_length // next_ncol] ) / (2 * current_root) ) @@ -183,10 +151,7 @@ def __multiply(self): inverseC = new_inverseC next_ncol *= 2 # Unpack - inverseC = [ - round(x[0].real, 8) + round(x[0].imag, 8) * 1j - for x in inverseC - ] + inverseC = [round(x[0].real, 8) + round(x[0].imag, 8) * 1j for x in inverseC] # Remove leading 0's while inverseC[-1] == 0: @@ -196,20 +161,13 @@ def __multiply(self): # Overwrite __str__ for print(); Shows A, B and A*B def __str__(self): A = "A = " + " + ".join( - f"{coef}*x^{i}" - for coef, i in enumerate( - self.polyA[: self.len_A] - ) + f"{coef}*x^{i}" for coef, i in enumerate(self.polyA[: self.len_A]) ) B = "B = " + " + ".join( - f"{coef}*x^{i}" - for coef, i in enumerate( - self.polyB[: self.len_B] - ) + f"{coef}*x^{i}" for coef, i in enumerate(self.polyB[: self.len_B]) ) C = "A*B = " + " + ".join( - f"{coef}*x^{i}" - for coef, i in enumerate(self.product) + f"{coef}*x^{i}" for coef, i in enumerate(self.product) ) return "\n".join((A, B, C)) diff --git a/maths/segmented_sieve.py b/maths/segmented_sieve.py index b15ec2480678..c1cc497ad33e 100644 --- a/maths/segmented_sieve.py +++ b/maths/segmented_sieve.py @@ -48,4 +48,4 @@ def sieve(n): return prime -print(sieve(10**6)) +print(sieve(10 ** 6)) diff --git a/maths/simpson_rule.py b/maths/simpson_rule.py index 5cf9c14b07ee..f4620be8e70f 100644 --- a/maths/simpson_rule.py +++ b/maths/simpson_rule.py @@ -1,4 +1,3 @@ - """ Numerical integration or quadrature for a smooth function f with known values at x_i @@ -8,39 +7,45 @@ "Simpson Rule" """ + + def method_2(boundary, steps): -# "Simpson Rule" -# int(f) = delta_x/2 * (b-a)/3*(f1 + 4f2 + 2f_3 + ... + fn) + # "Simpson Rule" + # int(f) = delta_x/2 * (b-a)/3*(f1 + 4f2 + 2f_3 + ... + fn) h = (boundary[1] - boundary[0]) / steps a = boundary[0] b = boundary[1] - x_i = make_points(a,b,h) + x_i = make_points(a, b, h) y = 0.0 - y += (h/3.0)*f(a) + y += (h / 3.0) * f(a) cnt = 2 for i in x_i: - y += (h/3)*(4-2*(cnt%2))*f(i) + y += (h / 3) * (4 - 2 * (cnt % 2)) * f(i) cnt += 1 - y += (h/3.0)*f(b) + y += (h / 3.0) * f(b) return y -def make_points(a,b,h): + +def make_points(a, b, h): x = a + h - while x < (b-h): + while x < (b - h): yield x x = x + h -def f(x): #enter your function here - y = (x-0)*(x-0) + +def f(x): # enter your function here + y = (x - 0) * (x - 0) return y + def main(): - a = 0.0 #Lower bound of integration - b = 1.0 #Upper bound of integration - steps = 10.0 #define number of steps or resolution - boundary = [a, b] #define boundary of integration + a = 0.0 # Lower bound of integration + b = 1.0 # Upper bound of integration + steps = 10.0 # define number of steps or resolution + boundary = [a, b] # define boundary of integration y = method_2(boundary, steps) - print('y = {0}'.format(y)) + print("y = {0}".format(y)) + -if __name__ == '__main__': - main() +if __name__ == "__main__": + main() diff --git a/maths/trapezoidal_rule.py b/maths/trapezoidal_rule.py index f5e5fbbc2662..0f321317614d 100644 --- a/maths/trapezoidal_rule.py +++ b/maths/trapezoidal_rule.py @@ -7,38 +7,44 @@ "extended trapezoidal rule" """ + + def method_1(boundary, steps): -# "extended trapezoidal rule" -# int(f) = dx/2 * (f1 + 2f2 + ... + fn) - h = (boundary[1] - boundary[0]) / steps - a = boundary[0] - b = boundary[1] - x_i = make_points(a,b,h) - y = 0.0 - y += (h/2.0)*f(a) - for i in x_i: - #print(i) - y += h*f(i) - y += (h/2.0)*f(b) - return y - -def make_points(a,b,h): - x = a + h - while x < (b-h): - yield x - x = x + h - -def f(x): #enter your function here - y = (x-0)*(x-0) - return y + # "extended trapezoidal rule" + # int(f) = dx/2 * (f1 + 2f2 + ... + fn) + h = (boundary[1] - boundary[0]) / steps + a = boundary[0] + b = boundary[1] + x_i = make_points(a, b, h) + y = 0.0 + y += (h / 2.0) * f(a) + for i in x_i: + # print(i) + y += h * f(i) + y += (h / 2.0) * f(b) + return y + + +def make_points(a, b, h): + x = a + h + while x < (b - h): + yield x + x = x + h + + +def f(x): # enter your function here + y = (x - 0) * (x - 0) + return y + def main(): - a = 0.0 #Lower bound of integration - b = 1.0 #Upper bound of integration - steps = 10.0 #define number of steps or resolution - boundary = [a, b] #define boundary of integration - y = method_1(boundary, steps) - print('y = {0}'.format(y)) - -if __name__ == '__main__': - main() + a = 0.0 # Lower bound of integration + b = 1.0 # Upper bound of integration + steps = 10.0 # define number of steps or resolution + boundary = [a, b] # define boundary of integration + y = method_1(boundary, steps) + print("y = {0}".format(y)) + + +if __name__ == "__main__": + main() diff --git a/maths/zellers_congruence.py b/maths/zellers_congruence.py index 67c5550802ea..277ecfaf0da9 100644 --- a/maths/zellers_congruence.py +++ b/maths/zellers_congruence.py @@ -68,24 +68,16 @@ def zeller(date_input: str) -> str: # Days of the week for response days = { - '0': 'Sunday', - '1': 'Monday', - '2': 'Tuesday', - '3': 'Wednesday', - '4': 'Thursday', - '5': 'Friday', - '6': 'Saturday' + "0": "Sunday", + "1": "Monday", + "2": "Tuesday", + "3": "Wednesday", + "4": "Thursday", + "5": "Friday", + "6": "Saturday", } - convert_datetime_days = { - 0:1, - 1:2, - 2:3, - 3:4, - 4:5, - 5:6, - 6:0 - } + convert_datetime_days = {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 0} # Validate if not 0 < len(date_input) < 11: @@ -97,9 +89,9 @@ def zeller(date_input: str) -> str: if not 0 < m < 13: raise ValueError("Month must be between 1 - 12") - sep_1:str = date_input[2] + sep_1: str = date_input[2] # Validate - if sep_1 not in ["-","/"]: + if sep_1 not in ["-", "/"]: raise ValueError("Date seperator must be '-' or '/'") # Get day @@ -111,14 +103,16 @@ def zeller(date_input: str) -> str: # Get second seperator sep_2: str = date_input[5] # Validate - if sep_2 not in ["-","/"]: + if sep_2 not in ["-", "/"]: raise ValueError("Date seperator must be '-' or '/'") # Get year y: int = int(date_input[6] + date_input[7] + date_input[8] + date_input[9]) # Arbitrary year range if not 45 < y < 8500: - raise ValueError("Year out of range. There has to be some sort of limit...right?") + raise ValueError( + "Year out of range. There has to be some sort of limit...right?" + ) # Get datetime obj for validation dt_ck = datetime.date(int(y), int(m), int(d)) @@ -130,13 +124,13 @@ def zeller(date_input: str) -> str: # maths var c: int = int(str(y)[:2]) k: int = int(str(y)[2:]) - t: int = int(2.6*m - 5.39) + t: int = int(2.6 * m - 5.39) u: int = int(c / 4) v: int = int(k / 4) x: int = int(d + k) z: int = int(t + u + v + x) w: int = int(z - (2 * c)) - f: int = round(w%7) + f: int = round(w % 7) # End math # Validate math @@ -147,10 +141,16 @@ def zeller(date_input: str) -> str: response: str = f"Your date {date_input}, is a {days[str(f)]}!" return response -if __name__ == '__main__': + +if __name__ == "__main__": import doctest + doctest.testmod() - parser = argparse.ArgumentParser(description='Find out what day of the week nearly any date is or was. Enter date as a string in the mm-dd-yyyy or mm/dd/yyyy format') - parser.add_argument('date_input', type=str, help='Date as a string (mm-dd-yyyy or mm/dd/yyyy)') + parser = argparse.ArgumentParser( + description="Find out what day of the week nearly any date is or was. Enter date as a string in the mm-dd-yyyy or mm/dd/yyyy format" + ) + parser.add_argument( + "date_input", type=str, help="Date as a string (mm-dd-yyyy or mm/dd/yyyy)" + ) args = parser.parse_args() zeller(args.date_input) diff --git a/matrix/matrix_class.py b/matrix/matrix_class.py index c82fb2cf6464..a8066e319559 100644 --- a/matrix/matrix_class.py +++ b/matrix/matrix_class.py @@ -311,8 +311,10 @@ def __mul__(self, other): return Matrix([[element * other for element in row] for row in self.rows]) elif isinstance(other, Matrix): if self.num_columns != other.num_rows: - raise ValueError("The number of columns in the first matrix must " - "be equal to the number of rows in the second") + raise ValueError( + "The number of columns in the first matrix must " + "be equal to the number of rows in the second" + ) return Matrix( [ [Matrix.dot_product(row, column) for column in other.columns()] @@ -320,7 +322,9 @@ def __mul__(self, other): ] ) else: - raise TypeError("A Matrix can only be multiplied by an int, float, or another matrix") + raise TypeError( + "A Matrix can only be multiplied by an int, float, or another matrix" + ) def __pow__(self, other): if not isinstance(other, int): diff --git a/matrix/matrix_operation.py b/matrix/matrix_operation.py index b32a4dcf7af3..5ca61b4ed023 100644 --- a/matrix/matrix_operation.py +++ b/matrix/matrix_operation.py @@ -39,8 +39,10 @@ def multiply(matrix_a, matrix_b): rows, cols = _verify_matrix_sizes(matrix_a, matrix_b) if cols[0] != rows[1]: - raise ValueError(f'Cannot multiply matrix of dimensions ({rows[0]},{cols[0]}) ' - f'and ({rows[1]},{cols[1]})') + raise ValueError( + f"Cannot multiply matrix of dimensions ({rows[0]},{cols[0]}) " + f"and ({rows[1]},{cols[1]})" + ) for i in range(rows[0]): list_1 = [] for j in range(cols[1]): @@ -59,7 +61,7 @@ def identity(n): :return: Identity matrix of shape [n, n] """ n = int(n) - return [[int(row == column) for column in range(n)] for row in range(n)] + return [[int(row == column) for column in range(n)] for row in range(n)] def transpose(matrix, return_map=True): @@ -75,15 +77,15 @@ def transpose(matrix, return_map=True): def minor(matrix, row, column): - minor = matrix[:row] + matrix[row + 1:] - minor = [row[:column] + row[column + 1:] for row in minor] + minor = matrix[:row] + matrix[row + 1 :] + minor = [row[:column] + row[column + 1 :] for row in minor] return minor def determinant(matrix): if len(matrix) == 1: return matrix[0][0] - + res = 0 for x in range(len(matrix)): res += matrix[0][x] * determinant(minor(matrix, 0, x)) * (-1) ** x @@ -99,10 +101,13 @@ def inverse(matrix): for i in range(len(matrix)): for j in range(len(matrix)): matrix_minor[i].append(determinant(minor(matrix, i, j))) - - cofactors = [[x * (-1) ** (row + col) for col, x in enumerate(matrix_minor[row])] for row in range(len(matrix))] + + cofactors = [ + [x * (-1) ** (row + col) for col, x in enumerate(matrix_minor[row])] + for row in range(len(matrix)) + ] adjugate = transpose(cofactors) - return scalar_multiply(adjugate, 1/det) + return scalar_multiply(adjugate, 1 / det) def _check_not_integer(matrix): @@ -122,8 +127,10 @@ def _verify_matrix_sizes(matrix_a, matrix_b): shape = _shape(matrix_a) shape += _shape(matrix_b) if shape[0] != shape[2] or shape[1] != shape[3]: - raise ValueError(f"operands could not be broadcast together with shape " - f"({shape[0], shape[1]}), ({shape[2], shape[3]})") + raise ValueError( + f"operands could not be broadcast together with shape " + f"({shape[0], shape[1]}), ({shape[2], shape[3]})" + ) return [shape[0], shape[2]], [shape[1], shape[3]] @@ -132,13 +139,19 @@ def main(): matrix_b = [[3, 4], [7, 4]] matrix_c = [[11, 12, 13, 14], [21, 22, 23, 24], [31, 32, 33, 34], [41, 42, 43, 44]] matrix_d = [[3, 0, 2], [2, 0, -2], [0, 1, 1]] - print('Add Operation, %s + %s = %s \n' %(matrix_a, matrix_b, (add(matrix_a, matrix_b)))) - print('Multiply Operation, %s * %s = %s \n' %(matrix_a, matrix_b, multiply(matrix_a, matrix_b))) - print('Identity: %s \n' %identity(5)) - print('Minor of %s = %s \n' %(matrix_c, minor(matrix_c, 1, 2))) - print('Determinant of %s = %s \n' %(matrix_b, determinant(matrix_b))) - print('Inverse of %s = %s\n'%(matrix_d, inverse(matrix_d))) - - -if __name__ == '__main__': + print( + "Add Operation, %s + %s = %s \n" + % (matrix_a, matrix_b, (add(matrix_a, matrix_b))) + ) + print( + "Multiply Operation, %s * %s = %s \n" + % (matrix_a, matrix_b, multiply(matrix_a, matrix_b)) + ) + print("Identity: %s \n" % identity(5)) + print("Minor of %s = %s \n" % (matrix_c, minor(matrix_c, 1, 2))) + print("Determinant of %s = %s \n" % (matrix_b, determinant(matrix_b))) + print("Inverse of %s = %s\n" % (matrix_d, inverse(matrix_d))) + + +if __name__ == "__main__": main() diff --git a/matrix/nth_fibonacci_using_matrix_exponentiation.py b/matrix/nth_fibonacci_using_matrix_exponentiation.py index 57cdfacd47dd..222779f454f9 100644 --- a/matrix/nth_fibonacci_using_matrix_exponentiation.py +++ b/matrix/nth_fibonacci_using_matrix_exponentiation.py @@ -13,6 +13,8 @@ So we just need the n times multiplication of the matrix [1,1],[1,0]]. We can decrease the n times multiplication by following the divide and conquer approach. """ + + def multiply(matrix_a, matrix_b): matrix_c = [] n = len(matrix_a) diff --git a/matrix/searching_in_sorted_matrix.py b/matrix/searching_in_sorted_matrix.py index 54913b350803..1b3eeedf3110 100644 --- a/matrix/searching_in_sorted_matrix.py +++ b/matrix/searching_in_sorted_matrix.py @@ -2,26 +2,21 @@ def search_in_a_sorted_matrix(mat, m, n, key): i, j = m - 1, 0 while i >= 0 and j < n: if key == mat[i][j]: - print('Key %s found at row- %s column- %s' % (key, i + 1, j + 1)) + print("Key %s found at row- %s column- %s" % (key, i + 1, j + 1)) return if key < mat[i][j]: i -= 1 else: j += 1 - print('Key %s not found' % (key)) + print("Key %s not found" % (key)) def main(): - mat = [ - [2, 5, 7], - [4, 8, 13], - [9, 11, 15], - [12, 17, 20] - ] + mat = [[2, 5, 7], [4, 8, 13], [9, 11, 15], [12, 17, 20]] x = int(input("Enter the element to be searched:")) print(mat) search_in_a_sorted_matrix(mat, len(mat), len(mat[0]), x) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/matrix/sherman_morrison.py b/matrix/sherman_morrison.py index 0d49d78509be..531b76cdeb94 100644 --- a/matrix/sherman_morrison.py +++ b/matrix/sherman_morrison.py @@ -28,7 +28,7 @@ def __str__(self): # Prefix s = "Matrix consist of %d rows and %d columns\n" % (self.row, self.column) - + # Make string identifier max_element_length = 0 for row_vector in self.array: @@ -37,16 +37,18 @@ def __str__(self): string_format_identifier = "%%%ds" % (max_element_length,) # Make string and return - def single_line(row_vector): + def single_line(row_vector): nonlocal string_format_identifier line = "[" line += ", ".join(string_format_identifier % (obj,) for obj in row_vector) line += "]" return line + s += "\n".join(single_line(row_vector) for row_vector in self.array) return s - def __repr__(self): return str(self) + def __repr__(self): + return str(self) def validateIndices(self, loc: tuple): """ @@ -60,9 +62,12 @@ def validateIndices(self, loc: tuple): >>> a.validateIndices((0, 0)) True """ - if not(isinstance(loc, (list, tuple)) and len(loc) == 2): return False - elif not(0 <= loc[0] < self.row and 0 <= loc[1] < self.column): return False - else: return True + if not (isinstance(loc, (list, tuple)) and len(loc) == 2): + return False + elif not (0 <= loc[0] < self.row and 0 <= loc[1] < self.column): + return False + else: + return True def __getitem__(self, loc: tuple): """ @@ -115,7 +120,7 @@ def __add__(self, another): result = Matrix(self.row, self.column) for r in range(self.row): for c in range(self.column): - result[r,c] = self[r,c] + another[r,c] + result[r, c] = self[r, c] + another[r, c] return result def __neg__(self): @@ -135,10 +140,11 @@ def __neg__(self): result = Matrix(self.row, self.column) for r in range(self.row): for c in range(self.column): - result[r,c] = -self[r,c] + result[r, c] = -self[r, c] return result - def __sub__(self, another): return self + (-another) + def __sub__(self, another): + return self + (-another) def __mul__(self, another): """ @@ -154,21 +160,24 @@ def __mul__(self, another): [-2, -2, -6] """ - if isinstance(another, (int, float)): # Scalar multiplication + if isinstance(another, (int, float)): # Scalar multiplication result = Matrix(self.row, self.column) for r in range(self.row): for c in range(self.column): - result[r,c] = self[r,c] * another + result[r, c] = self[r, c] * another return result - elif isinstance(another, Matrix): # Matrix multiplication - assert(self.column == another.row) + elif isinstance(another, Matrix): # Matrix multiplication + assert self.column == another.row result = Matrix(self.row, another.column) for r in range(self.row): for c in range(another.column): for i in range(self.column): - result[r,c] += self[r,i] * another[i,c] + result[r, c] += self[r, i] * another[i, c] return result - else: raise TypeError("Unsupported type given for another (%s)" % (type(another),)) + else: + raise TypeError( + "Unsupported type given for another (%s)" % (type(another),) + ) def transpose(self): """ @@ -191,7 +200,7 @@ def transpose(self): result = Matrix(self.column, self.row) for r in range(self.row): for c in range(self.column): - result[c,r] = self[r,c] + result[c, r] = self[r, c] return result def ShermanMorrison(self, u, v): @@ -220,28 +229,31 @@ def ShermanMorrison(self, u, v): # Size validation assert isinstance(u, Matrix) and isinstance(v, Matrix) - assert self.row == self.column == u.row == v.row # u, v should be column vector - assert u.column == v.column == 1 # u, v should be column vector + assert self.row == self.column == u.row == v.row # u, v should be column vector + assert u.column == v.column == 1 # u, v should be column vector # Calculate vT = v.transpose() numerator_factor = (vT * self * u)[0, 0] + 1 - if numerator_factor == 0: return None # It's not invertable + if numerator_factor == 0: + return None # It's not invertable return self - ((self * u) * (vT * self) * (1.0 / numerator_factor)) + # Testing if __name__ == "__main__": def test1(): # a^(-1) ainv = Matrix(3, 3, 0) - for i in range(3): ainv[i,i] = 1 + for i in range(3): + ainv[i, i] = 1 print("a^(-1) is %s" % (ainv,)) # u, v u = Matrix(3, 1, 0) - u[0,0], u[1,0], u[2,0] = 1, 2, -3 + u[0, 0], u[1, 0], u[2, 0] = 1, 2, -3 v = Matrix(3, 1, 0) - v[0,0], v[1,0], v[2,0] = 4, -2, 5 + v[0, 0], v[1, 0], v[2, 0] = 4, -2, 5 print("u is %s" % (u,)) print("v is %s" % (v,)) print("uv^T is %s" % (u * v.transpose())) @@ -250,6 +262,7 @@ def test1(): def test2(): import doctest + doctest.testmod() - test2() \ No newline at end of file + test2() diff --git a/matrix/spiral_print.py b/matrix/spiral_print.py index 447881e508e7..31d9fff84bfd 100644 --- a/matrix/spiral_print.py +++ b/matrix/spiral_print.py @@ -6,6 +6,8 @@ i) matrix should be only one or two dimensional ii)column of all the row should be equal """ + + def checkMatrix(a): # must be if type(a) == list and len(a) > 0: @@ -51,7 +53,7 @@ def spiralPrint(a): # vertical printing up for i in range(matRow - 2, 0, -1): print(a[i][0]), - remainMat = [row[1:matCol - 1] for row in a[1:matRow - 1]] + remainMat = [row[1 : matCol - 1] for row in a[1 : matRow - 1]] if len(remainMat) > 0: spiralPrint(remainMat) else: @@ -62,5 +64,5 @@ def spiralPrint(a): # driver code -a = [[1 , 2, 3, 4],[5, 6, 7, 8],[9, 10, 11, 12]] +a = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] spiralPrint(a) diff --git a/matrix/tests/test_matrix_operation.py b/matrix/tests/test_matrix_operation.py index 8b81b65d0fc8..f9f72cf59af8 100644 --- a/matrix/tests/test_matrix_operation.py +++ b/matrix/tests/test_matrix_operation.py @@ -29,8 +29,9 @@ @pytest.mark.mat_ops -@pytest.mark.parametrize(('mat1', 'mat2'), [(mat_a, mat_b), (mat_c, mat_d), (mat_d, mat_e), - (mat_f, mat_h)]) +@pytest.mark.parametrize( + ("mat1", "mat2"), [(mat_a, mat_b), (mat_c, mat_d), (mat_d, mat_e), (mat_f, mat_h)] +) def test_addition(mat1, mat2): if (np.array(mat1)).shape < (2, 2) or (np.array(mat2)).shape < (2, 2): with pytest.raises(TypeError): @@ -48,8 +49,9 @@ def test_addition(mat1, mat2): @pytest.mark.mat_ops -@pytest.mark.parametrize(('mat1', 'mat2'), [(mat_a, mat_b), (mat_c, mat_d), (mat_d, mat_e), - (mat_f, mat_h)]) +@pytest.mark.parametrize( + ("mat1", "mat2"), [(mat_a, mat_b), (mat_c, mat_d), (mat_d, mat_e), (mat_f, mat_h)] +) def test_subtraction(mat1, mat2): if (np.array(mat1)).shape < (2, 2) or (np.array(mat2)).shape < (2, 2): with pytest.raises(TypeError): @@ -67,8 +69,9 @@ def test_subtraction(mat1, mat2): @pytest.mark.mat_ops -@pytest.mark.parametrize(('mat1', 'mat2'), [(mat_a, mat_b), (mat_c, mat_d), (mat_d, mat_e), - (mat_f, mat_h)]) +@pytest.mark.parametrize( + ("mat1", "mat2"), [(mat_a, mat_b), (mat_c, mat_d), (mat_d, mat_e), (mat_f, mat_h)] +) def test_multiplication(mat1, mat2): if (np.array(mat1)).shape < (2, 2) or (np.array(mat2)).shape < (2, 2): logger.info(f"\n\t{test_multiplication.__name__} returned integer") @@ -81,7 +84,9 @@ def test_multiplication(mat1, mat2): assert theo == act else: with pytest.raises(ValueError): - logger.info(f"\n\t{test_multiplication.__name__} does not meet dim requirements") + logger.info( + f"\n\t{test_multiplication.__name__} does not meet dim requirements" + ) assert matop.subtract(mat1, mat2) @@ -100,7 +105,7 @@ def test_identity(): @pytest.mark.mat_ops -@pytest.mark.parametrize('mat', [mat_a, mat_b, mat_c, mat_d, mat_e, mat_f]) +@pytest.mark.parametrize("mat", [mat_a, mat_b, mat_c, mat_d, mat_e, mat_f]) def test_transpose(mat): if (np.array(mat)).shape < (2, 2): with pytest.raises(TypeError): diff --git a/networking_flow/ford_fulkerson.py b/networking_flow/ford_fulkerson.py index d51f1f0661b3..0028c7cc577f 100644 --- a/networking_flow/ford_fulkerson.py +++ b/networking_flow/ford_fulkerson.py @@ -4,14 +4,15 @@ (1) Start with initial flow as 0; (2) Choose augmenting path from source to sink and add path to flow; """ - + + def BFS(graph, s, t, parent): # Return True if there is node that has not iterated. - visited = [False]*len(graph) - queue=[] + visited = [False] * len(graph) + queue = [] queue.append(s) visited[s] = True - + while queue: u = queue.pop(0) for ind in range(len(graph[u])): @@ -21,36 +22,40 @@ def BFS(graph, s, t, parent): parent[ind] = u return True if visited[t] else False - + + def FordFulkerson(graph, source, sink): # This array is filled by BFS and to store path - parent = [-1]*(len(graph)) - max_flow = 0 - while BFS(graph, source, sink, parent) : + parent = [-1] * (len(graph)) + max_flow = 0 + while BFS(graph, source, sink, parent): path_flow = float("Inf") s = sink - while(s != source): + while s != source: # Find the minimum value in select path - path_flow = min (path_flow, graph[parent[s]][s]) + path_flow = min(path_flow, graph[parent[s]][s]) s = parent[s] - max_flow += path_flow + max_flow += path_flow v = sink - while(v != source): + while v != source: u = parent[v] graph[u][v] -= path_flow graph[v][u] += path_flow v = parent[v] return max_flow -graph = [[0, 16, 13, 0, 0, 0], - [0, 0, 10 ,12, 0, 0], - [0, 4, 0, 0, 14, 0], - [0, 0, 9, 0, 0, 20], - [0, 0, 0, 7, 0, 4], - [0, 0, 0, 0, 0, 0]] + +graph = [ + [0, 16, 13, 0, 0, 0], + [0, 0, 10, 12, 0, 0], + [0, 4, 0, 0, 14, 0], + [0, 0, 9, 0, 0, 20], + [0, 0, 0, 7, 0, 4], + [0, 0, 0, 0, 0, 0], +] source, sink = 0, 5 -print(FordFulkerson(graph, source, sink)) \ No newline at end of file +print(FordFulkerson(graph, source, sink)) diff --git a/neural_network/back_propagation_neural_network.py b/neural_network/back_propagation_neural_network.py index 92deaee19c6e..86797694bb0a 100644 --- a/neural_network/back_propagation_neural_network.py +++ b/neural_network/back_propagation_neural_network.py @@ -1,7 +1,7 @@ #!/usr/bin/python # encoding=utf8 -''' +""" A Framework of Back Propagation Neural Network(BP) model @@ -17,7 +17,7 @@ Github : https://github.com/RiptideBo Date: 2017.11.23 -''' +""" import numpy as np import matplotlib.pyplot as plt @@ -26,18 +26,22 @@ def sigmoid(x): return 1 / (1 + np.exp(-1 * x)) -class DenseLayer(): - ''' + +class DenseLayer: + """ Layers of BP neural network - ''' - def __init__(self,units,activation=None,learning_rate=None,is_input_layer=False): - ''' + """ + + def __init__( + self, units, activation=None, learning_rate=None, is_input_layer=False + ): + """ common connected layer of bp network :param units: numbers of neural units :param activation: activation function :param learning_rate: learning rate for paras :param is_input_layer: whether it is input layer or not - ''' + """ self.units = units self.weight = None self.bias = None @@ -47,21 +51,21 @@ def __init__(self,units,activation=None,learning_rate=None,is_input_layer=False) self.learn_rate = learning_rate self.is_input_layer = is_input_layer - def initializer(self,back_units): - self.weight = np.asmatrix(np.random.normal(0,0.5,(self.units,back_units))) - self.bias = np.asmatrix(np.random.normal(0,0.5,self.units)).T + def initializer(self, back_units): + self.weight = np.asmatrix(np.random.normal(0, 0.5, (self.units, back_units))) + self.bias = np.asmatrix(np.random.normal(0, 0.5, self.units)).T if self.activation is None: self.activation = sigmoid def cal_gradient(self): if self.activation == sigmoid: - gradient_mat = np.dot(self.output ,(1- self.output).T) + gradient_mat = np.dot(self.output, (1 - self.output).T) gradient_activation = np.diag(np.diag(gradient_mat)) else: gradient_activation = 1 return gradient_activation - def forward_propagation(self,xdata): + def forward_propagation(self, xdata): self.xdata = xdata if self.is_input_layer: # input layer @@ -69,22 +73,22 @@ def forward_propagation(self,xdata): self.output = xdata return xdata else: - self.wx_plus_b = np.dot(self.weight,self.xdata) - self.bias + self.wx_plus_b = np.dot(self.weight, self.xdata) - self.bias self.output = self.activation(self.wx_plus_b) return self.output - def back_propagation(self,gradient): + def back_propagation(self, gradient): - gradient_activation = self.cal_gradient() # i * i 维 - gradient = np.asmatrix(np.dot(gradient.T,gradient_activation)) + gradient_activation = self.cal_gradient() # i * i 维 + gradient = np.asmatrix(np.dot(gradient.T, gradient_activation)) self._gradient_weight = np.asmatrix(self.xdata) self._gradient_bias = -1 self._gradient_x = self.weight - self.gradient_weight = np.dot(gradient.T,self._gradient_weight.T) + self.gradient_weight = np.dot(gradient.T, self._gradient_weight.T) self.gradient_bias = gradient * self._gradient_bias - self.gradient = np.dot(gradient,self._gradient_x).T + self.gradient = np.dot(gradient, self._gradient_x).T # ----------------------upgrade # -----------the Negative gradient direction -------- self.weight = self.weight - self.learn_rate * self.gradient_weight @@ -93,33 +97,34 @@ def back_propagation(self,gradient): return self.gradient -class BPNN(): - ''' +class BPNN: + """ Back Propagation Neural Network model - ''' + """ + def __init__(self): self.layers = [] self.train_mse = [] self.fig_loss = plt.figure() - self.ax_loss = self.fig_loss.add_subplot(1,1,1) + self.ax_loss = self.fig_loss.add_subplot(1, 1, 1) - def add_layer(self,layer): + def add_layer(self, layer): self.layers.append(layer) def build(self): - for i,layer in enumerate(self.layers[:]): + for i, layer in enumerate(self.layers[:]): if i < 1: layer.is_input_layer = True else: - layer.initializer(self.layers[i-1].units) + layer.initializer(self.layers[i - 1].units) def summary(self): - for i,layer in enumerate(self.layers[:]): - print('------- layer %d -------'%i) - print('weight.shape ',np.shape(layer.weight)) - print('bias.shape ',np.shape(layer.bias)) + for i, layer in enumerate(self.layers[:]): + print("------- layer %d -------" % i) + print("weight.shape ", np.shape(layer.weight)) + print("bias.shape ", np.shape(layer.bias)) - def train(self,xdata,ydata,train_round,accuracy): + def train(self, xdata, ydata, train_round, accuracy): self.train_round = train_round self.accuracy = accuracy @@ -129,8 +134,8 @@ def train(self,xdata,ydata,train_round,accuracy): for round_i in range(train_round): all_loss = 0 for row in range(x_shape[0]): - _xdata = np.asmatrix(xdata[row,:]).T - _ydata = np.asmatrix(ydata[row,:]).T + _xdata = np.asmatrix(xdata[row, :]).T + _ydata = np.asmatrix(ydata[row, :]).T # forward propagation for layer in self.layers: @@ -144,40 +149,49 @@ def train(self,xdata,ydata,train_round,accuracy): for layer in self.layers[:0:-1]: gradient = layer.back_propagation(gradient) - mse = all_loss/x_shape[0] + mse = all_loss / x_shape[0] self.train_mse.append(mse) self.plot_loss() if mse < self.accuracy: - print('----达到精度----') + print("----达到精度----") return mse - def cal_loss(self,ydata,ydata_): - self.loss = np.sum(np.power((ydata - ydata_),2)) + def cal_loss(self, ydata, ydata_): + self.loss = np.sum(np.power((ydata - ydata_), 2)) self.loss_gradient = 2 * (ydata_ - ydata) # vector (shape is the same as _ydata.shape) - return self.loss,self.loss_gradient + return self.loss, self.loss_gradient def plot_loss(self): if self.ax_loss.lines: self.ax_loss.lines.remove(self.ax_loss.lines[0]) - self.ax_loss.plot(self.train_mse, 'r-') + self.ax_loss.plot(self.train_mse, "r-") plt.ion() - plt.xlabel('step') - plt.ylabel('loss') + plt.xlabel("step") + plt.ylabel("loss") plt.show() plt.pause(0.1) - - def example(): - x = np.random.randn(10,10) - y = np.asarray([[0.8,0.4],[0.4,0.3],[0.34,0.45],[0.67,0.32], - [0.88,0.67],[0.78,0.77],[0.55,0.66],[0.55,0.43],[0.54,0.1], - [0.1,0.5]]) + x = np.random.randn(10, 10) + y = np.asarray( + [ + [0.8, 0.4], + [0.4, 0.3], + [0.34, 0.45], + [0.67, 0.32], + [0.88, 0.67], + [0.78, 0.77], + [0.55, 0.66], + [0.55, 0.43], + [0.54, 0.1], + [0.1, 0.5], + ] + ) model = BPNN() model.add_layer(DenseLayer(10)) @@ -189,7 +203,8 @@ def example(): model.summary() - model.train(xdata=x,ydata=y,train_round=100,accuracy=0.01) + model.train(xdata=x, ydata=y, train_round=100, accuracy=0.01) + -if __name__ == '__main__': +if __name__ == "__main__": example() diff --git a/neural_network/convolution_neural_network.py b/neural_network/convolution_neural_network.py index 786992c054a0..9448671abace 100644 --- a/neural_network/convolution_neural_network.py +++ b/neural_network/convolution_neural_network.py @@ -1,6 +1,6 @@ -#-*- coding: utf-8 -*- +# -*- coding: utf-8 -*- -''' +""" - - - - - -- - - - - - - - - - - - - - - - - - - - - - - Name - - CNN - Convolution Neural Network For Photo Recognizing Goal - - Recognize Handing Writting Word Photo @@ -14,15 +14,17 @@ Github: 245885195@qq.com Date: 2017.9.20 - - - - - -- - - - - - - - - - - - - - - - - - - - - - - -''' +""" import pickle import numpy as np import matplotlib.pyplot as plt -class CNN(): - def __init__(self, conv1_get, size_p1, bp_num1, bp_num2, bp_num3, rate_w=0.2, rate_t=0.2): - ''' +class CNN: + def __init__( + self, conv1_get, size_p1, bp_num1, bp_num2, bp_num3, rate_w=0.2, rate_t=0.2 + ): + """ :param conv1_get: [a,c,d],size, number, step of convolution kernel :param size_p1: pooling size :param bp_num1: units number of flatten layer @@ -30,7 +32,7 @@ def __init__(self, conv1_get, size_p1, bp_num1, bp_num2, bp_num3, rate_w=0.2, ra :param bp_num3: units number of output layer :param rate_w: rate of weight learning :param rate_t: rate of threshold learning - ''' + """ self.num_bp1 = bp_num1 self.num_bp2 = bp_num2 self.num_bp3 = bp_num3 @@ -39,206 +41,246 @@ def __init__(self, conv1_get, size_p1, bp_num1, bp_num2, bp_num3, rate_w=0.2, ra self.size_pooling1 = size_p1 self.rate_weight = rate_w self.rate_thre = rate_t - self.w_conv1 = [np.mat(-1*np.random.rand(self.conv1[0],self.conv1[0])+0.5) for i in range(self.conv1[1])] + self.w_conv1 = [ + np.mat(-1 * np.random.rand(self.conv1[0], self.conv1[0]) + 0.5) + for i in range(self.conv1[1]) + ] self.wkj = np.mat(-1 * np.random.rand(self.num_bp3, self.num_bp2) + 0.5) - self.vji = np.mat(-1*np.random.rand(self.num_bp2, self.num_bp1)+0.5) - self.thre_conv1 = -2*np.random.rand(self.conv1[1])+1 - self.thre_bp2 = -2*np.random.rand(self.num_bp2)+1 - self.thre_bp3 = -2*np.random.rand(self.num_bp3)+1 - + self.vji = np.mat(-1 * np.random.rand(self.num_bp2, self.num_bp1) + 0.5) + self.thre_conv1 = -2 * np.random.rand(self.conv1[1]) + 1 + self.thre_bp2 = -2 * np.random.rand(self.num_bp2) + 1 + self.thre_bp3 = -2 * np.random.rand(self.num_bp3) + 1 def save_model(self, save_path): - #save model dict with pickle - model_dic = {'num_bp1':self.num_bp1, - 'num_bp2':self.num_bp2, - 'num_bp3':self.num_bp3, - 'conv1':self.conv1, - 'step_conv1':self.step_conv1, - 'size_pooling1':self.size_pooling1, - 'rate_weight':self.rate_weight, - 'rate_thre':self.rate_thre, - 'w_conv1':self.w_conv1, - 'wkj':self.wkj, - 'vji':self.vji, - 'thre_conv1':self.thre_conv1, - 'thre_bp2':self.thre_bp2, - 'thre_bp3':self.thre_bp3} - with open(save_path, 'wb') as f: + # save model dict with pickle + model_dic = { + "num_bp1": self.num_bp1, + "num_bp2": self.num_bp2, + "num_bp3": self.num_bp3, + "conv1": self.conv1, + "step_conv1": self.step_conv1, + "size_pooling1": self.size_pooling1, + "rate_weight": self.rate_weight, + "rate_thre": self.rate_thre, + "w_conv1": self.w_conv1, + "wkj": self.wkj, + "vji": self.vji, + "thre_conv1": self.thre_conv1, + "thre_bp2": self.thre_bp2, + "thre_bp3": self.thre_bp3, + } + with open(save_path, "wb") as f: pickle.dump(model_dic, f) - print('Model saved: %s'% save_path) + print("Model saved: %s" % save_path) @classmethod def ReadModel(cls, model_path): - #read saved model - with open(model_path, 'rb') as f: + # read saved model + with open(model_path, "rb") as f: model_dic = pickle.load(f) - conv_get= model_dic.get('conv1') - conv_get.append(model_dic.get('step_conv1')) - size_p1 = model_dic.get('size_pooling1') - bp1 = model_dic.get('num_bp1') - bp2 = model_dic.get('num_bp2') - bp3 = model_dic.get('num_bp3') - r_w = model_dic.get('rate_weight') - r_t = model_dic.get('rate_thre') - #create model instance - conv_ins = CNN(conv_get,size_p1,bp1,bp2,bp3,r_w,r_t) - #modify model parameter - conv_ins.w_conv1 = model_dic.get('w_conv1') - conv_ins.wkj = model_dic.get('wkj') - conv_ins.vji = model_dic.get('vji') - conv_ins.thre_conv1 = model_dic.get('thre_conv1') - conv_ins.thre_bp2 = model_dic.get('thre_bp2') - conv_ins.thre_bp3 = model_dic.get('thre_bp3') + conv_get = model_dic.get("conv1") + conv_get.append(model_dic.get("step_conv1")) + size_p1 = model_dic.get("size_pooling1") + bp1 = model_dic.get("num_bp1") + bp2 = model_dic.get("num_bp2") + bp3 = model_dic.get("num_bp3") + r_w = model_dic.get("rate_weight") + r_t = model_dic.get("rate_thre") + # create model instance + conv_ins = CNN(conv_get, size_p1, bp1, bp2, bp3, r_w, r_t) + # modify model parameter + conv_ins.w_conv1 = model_dic.get("w_conv1") + conv_ins.wkj = model_dic.get("wkj") + conv_ins.vji = model_dic.get("vji") + conv_ins.thre_conv1 = model_dic.get("thre_conv1") + conv_ins.thre_bp2 = model_dic.get("thre_bp2") + conv_ins.thre_bp3 = model_dic.get("thre_bp3") return conv_ins - def sig(self, x): - return 1 / (1 + np.exp(-1*x)) + return 1 / (1 + np.exp(-1 * x)) def do_round(self, x): return round(x, 3) def convolute(self, data, convs, w_convs, thre_convs, conv_step): - #convolution process + # convolution process size_conv = convs[0] - num_conv =convs[1] + num_conv = convs[1] size_data = np.shape(data)[0] - #get the data slice of original image data, data_focus + # get the data slice of original image data, data_focus data_focus = [] for i_focus in range(0, size_data - size_conv + 1, conv_step): for j_focus in range(0, size_data - size_conv + 1, conv_step): - focus = data[i_focus:i_focus + size_conv, j_focus:j_focus + size_conv] + focus = data[ + i_focus : i_focus + size_conv, j_focus : j_focus + size_conv + ] data_focus.append(focus) - #caculate the feature map of every single kernel, and saved as list of matrix + # caculate the feature map of every single kernel, and saved as list of matrix data_featuremap = [] Size_FeatureMap = int((size_data - size_conv) / conv_step + 1) for i_map in range(num_conv): featuremap = [] for i_focus in range(len(data_focus)): - net_focus = np.sum(np.multiply(data_focus[i_focus], w_convs[i_map])) - thre_convs[i_map] + net_focus = ( + np.sum(np.multiply(data_focus[i_focus], w_convs[i_map])) + - thre_convs[i_map] + ) featuremap.append(self.sig(net_focus)) - featuremap = np.asmatrix(featuremap).reshape(Size_FeatureMap, Size_FeatureMap) + featuremap = np.asmatrix(featuremap).reshape( + Size_FeatureMap, Size_FeatureMap + ) data_featuremap.append(featuremap) - #expanding the data slice to One dimenssion + # expanding the data slice to One dimenssion focus1_list = [] for each_focus in data_focus: focus1_list.extend(self.Expand_Mat(each_focus)) focus_list = np.asarray(focus1_list) - return focus_list,data_featuremap + return focus_list, data_featuremap - def pooling(self, featuremaps, size_pooling, type='average_pool'): - #pooling process + def pooling(self, featuremaps, size_pooling, type="average_pool"): + # pooling process size_map = len(featuremaps[0]) - size_pooled = int(size_map/size_pooling) + size_pooled = int(size_map / size_pooling) featuremap_pooled = [] for i_map in range(len(featuremaps)): map = featuremaps[i_map] map_pooled = [] - for i_focus in range(0,size_map,size_pooling): + for i_focus in range(0, size_map, size_pooling): for j_focus in range(0, size_map, size_pooling): - focus = map[i_focus:i_focus + size_pooling, j_focus:j_focus + size_pooling] - if type == 'average_pool': - #average pooling + focus = map[ + i_focus : i_focus + size_pooling, + j_focus : j_focus + size_pooling, + ] + if type == "average_pool": + # average pooling map_pooled.append(np.average(focus)) - elif type == 'max_pooling': - #max pooling + elif type == "max_pooling": + # max pooling map_pooled.append(np.max(focus)) - map_pooled = np.asmatrix(map_pooled).reshape(size_pooled,size_pooled) + map_pooled = np.asmatrix(map_pooled).reshape(size_pooled, size_pooled) featuremap_pooled.append(map_pooled) return featuremap_pooled def _expand(self, datas): - #expanding three dimension data to one dimension list + # expanding three dimension data to one dimension list data_expanded = [] for i in range(len(datas)): shapes = np.shape(datas[i]) - data_listed = datas[i].reshape(1,shapes[0]*shapes[1]) + data_listed = datas[i].reshape(1, shapes[0] * shapes[1]) data_listed = data_listed.getA().tolist()[0] data_expanded.extend(data_listed) data_expanded = np.asarray(data_expanded) return data_expanded def _expand_mat(self, data_mat): - #expanding matrix to one dimension list + # expanding matrix to one dimension list data_mat = np.asarray(data_mat) shapes = np.shape(data_mat) - data_expanded = data_mat.reshape(1,shapes[0]*shapes[1]) + data_expanded = data_mat.reshape(1, shapes[0] * shapes[1]) return data_expanded - def _calculate_gradient_from_pool(self, out_map, pd_pool,num_map, size_map, size_pooling): - ''' + def _calculate_gradient_from_pool( + self, out_map, pd_pool, num_map, size_map, size_pooling + ): + """ calcluate the gradient from the data slice of pool layer pd_pool: list of matrix out_map: the shape of data slice(size_map*size_map) return: pd_all: list of matrix, [num, size_map, size_map] - ''' + """ pd_all = [] i_pool = 0 for i_map in range(num_map): pd_conv1 = np.ones((size_map, size_map)) for i in range(0, size_map, size_pooling): for j in range(0, size_map, size_pooling): - pd_conv1[i:i + size_pooling, j:j + size_pooling] = pd_pool[i_pool] + pd_conv1[i : i + size_pooling, j : j + size_pooling] = pd_pool[ + i_pool + ] i_pool = i_pool + 1 - pd_conv2 = np.multiply(pd_conv1,np.multiply(out_map[i_map],(1-out_map[i_map]))) + pd_conv2 = np.multiply( + pd_conv1, np.multiply(out_map[i_map], (1 - out_map[i_map])) + ) pd_all.append(pd_conv2) return pd_all - def train(self, patterns, datas_train, datas_teach, n_repeat, error_accuracy, draw_e = bool): - #model traning - print('----------------------Start Training-------------------------') - print((' - - Shape: Train_Data ',np.shape(datas_train))) - print((' - - Shape: Teach_Data ',np.shape(datas_teach))) + def train( + self, patterns, datas_train, datas_teach, n_repeat, error_accuracy, draw_e=bool + ): + # model traning + print("----------------------Start Training-------------------------") + print((" - - Shape: Train_Data ", np.shape(datas_train))) + print((" - - Shape: Teach_Data ", np.shape(datas_teach))) rp = 0 all_mse = [] - mse = 10000 + mse = 10000 while rp < n_repeat and mse >= error_accuracy: alle = 0 - print('-------------Learning Time %d--------------'%rp) + print("-------------Learning Time %d--------------" % rp) for p in range(len(datas_train)): - #print('------------Learning Image: %d--------------'%p) + # print('------------Learning Image: %d--------------'%p) data_train = np.asmatrix(datas_train[p]) data_teach = np.asarray(datas_teach[p]) - data_focus1,data_conved1 = self.convolute(data_train,self.conv1,self.w_conv1, - self.thre_conv1,conv_step=self.step_conv1) - data_pooled1 = self.pooling(data_conved1,self.size_pooling1) + data_focus1, data_conved1 = self.convolute( + data_train, + self.conv1, + self.w_conv1, + self.thre_conv1, + conv_step=self.step_conv1, + ) + data_pooled1 = self.pooling(data_conved1, self.size_pooling1) shape_featuremap1 = np.shape(data_conved1) - ''' + """ print(' -----original shape ', np.shape(data_train)) print(' ---- after convolution ',np.shape(data_conv1)) print(' -----after pooling ',np.shape(data_pooled1)) - ''' + """ data_bp_input = self._expand(data_pooled1) bp_out1 = data_bp_input - bp_net_j = np.dot(bp_out1,self.vji.T) - self.thre_bp2 + bp_net_j = np.dot(bp_out1, self.vji.T) - self.thre_bp2 bp_out2 = self.sig(bp_net_j) - bp_net_k = np.dot(bp_out2 ,self.wkj.T) - self.thre_bp3 + bp_net_k = np.dot(bp_out2, self.wkj.T) - self.thre_bp3 bp_out3 = self.sig(bp_net_k) - #--------------Model Leaning ------------------------ + # --------------Model Leaning ------------------------ # calcluate error and gradient--------------- - pd_k_all = np.multiply((data_teach - bp_out3), np.multiply(bp_out3, (1 - bp_out3))) - pd_j_all = np.multiply(np.dot(pd_k_all,self.wkj), np.multiply(bp_out2, (1 - bp_out2))) - pd_i_all = np.dot(pd_j_all,self.vji) + pd_k_all = np.multiply( + (data_teach - bp_out3), np.multiply(bp_out3, (1 - bp_out3)) + ) + pd_j_all = np.multiply( + np.dot(pd_k_all, self.wkj), np.multiply(bp_out2, (1 - bp_out2)) + ) + pd_i_all = np.dot(pd_j_all, self.vji) - pd_conv1_pooled = pd_i_all / (self.size_pooling1*self.size_pooling1) + pd_conv1_pooled = pd_i_all / (self.size_pooling1 * self.size_pooling1) pd_conv1_pooled = pd_conv1_pooled.T.getA().tolist() - pd_conv1_all = self._calculate_gradient_from_pool(data_conved1,pd_conv1_pooled,shape_featuremap1[0], - shape_featuremap1[1],self.size_pooling1) - #weight and threshold learning process--------- - #convolution layer + pd_conv1_all = self._calculate_gradient_from_pool( + data_conved1, + pd_conv1_pooled, + shape_featuremap1[0], + shape_featuremap1[1], + self.size_pooling1, + ) + # weight and threshold learning process--------- + # convolution layer for k_conv in range(self.conv1[1]): pd_conv_list = self._expand_mat(pd_conv1_all[k_conv]) - delta_w = self.rate_weight * np.dot(pd_conv_list,data_focus1) + delta_w = self.rate_weight * np.dot(pd_conv_list, data_focus1) - self.w_conv1[k_conv] = self.w_conv1[k_conv] + delta_w.reshape((self.conv1[0],self.conv1[0])) + self.w_conv1[k_conv] = self.w_conv1[k_conv] + delta_w.reshape( + (self.conv1[0], self.conv1[0]) + ) - self.thre_conv1[k_conv] = self.thre_conv1[k_conv] - np.sum(pd_conv1_all[k_conv]) * self.rate_thre - #all connected layer + self.thre_conv1[k_conv] = ( + self.thre_conv1[k_conv] + - np.sum(pd_conv1_all[k_conv]) * self.rate_thre + ) + # all connected layer self.wkj = self.wkj + pd_k_all.T * bp_out2 * self.rate_weight self.vji = self.vji + pd_j_all.T * bp_out1 * self.rate_weight self.thre_bp3 = self.thre_bp3 - pd_k_all * self.rate_thre @@ -246,34 +288,41 @@ def train(self, patterns, datas_train, datas_teach, n_repeat, error_accuracy, dr # calculate the sum error of all single image errors = np.sum(abs((data_teach - bp_out3))) alle = alle + errors - #print(' ----Teach ',data_teach) - #print(' ----BP_output ',bp_out3) + # print(' ----Teach ',data_teach) + # print(' ----BP_output ',bp_out3) rp = rp + 1 - mse = alle/patterns + mse = alle / patterns all_mse.append(mse) + def draw_error(): yplot = [error_accuracy for i in range(int(n_repeat * 1.2))] - plt.plot(all_mse, '+-') - plt.plot(yplot, 'r--') - plt.xlabel('Learning Times') - plt.ylabel('All_mse') + plt.plot(all_mse, "+-") + plt.plot(yplot, "r--") + plt.xlabel("Learning Times") + plt.ylabel("All_mse") plt.grid(True, alpha=0.5) plt.show() - print('------------------Training Complished---------------------') - print((' - - Training epoch: ', rp, ' - - Mse: %.6f' % mse)) + + print("------------------Training Complished---------------------") + print((" - - Training epoch: ", rp, " - - Mse: %.6f" % mse)) if draw_e: draw_error() return mse def predict(self, datas_test): - #model predict + # model predict produce_out = [] - print('-------------------Start Testing-------------------------') - print((' - - Shape: Test_Data ',np.shape(datas_test))) + print("-------------------Start Testing-------------------------") + print((" - - Shape: Test_Data ", np.shape(datas_test))) for p in range(len(datas_test)): data_test = np.asmatrix(datas_test[p]) - data_focus1, data_conved1 = self.convolute(data_test, self.conv1, self.w_conv1, - self.thre_conv1, conv_step=self.step_conv1) + data_focus1, data_conved1 = self.convolute( + data_test, + self.conv1, + self.w_conv1, + self.thre_conv1, + conv_step=self.step_conv1, + ) data_pooled1 = self.pooling(data_conved1, self.size_pooling1) data_bp_input = self._expand(data_pooled1) @@ -283,20 +332,25 @@ def predict(self, datas_test): bp_net_k = bp_out2 * self.wkj.T - self.thre_bp3 bp_out3 = self.sig(bp_net_k) produce_out.extend(bp_out3.getA().tolist()) - res = [list(map(self.do_round,each)) for each in produce_out] + res = [list(map(self.do_round, each)) for each in produce_out] return np.asarray(res) def convolution(self, data): - #return the data of image after convoluting process so we can check it out + # return the data of image after convoluting process so we can check it out data_test = np.asmatrix(data) - data_focus1, data_conved1 = self.convolute(data_test, self.conv1, self.w_conv1, - self.thre_conv1, conv_step=self.step_conv1) + data_focus1, data_conved1 = self.convolute( + data_test, + self.conv1, + self.w_conv1, + self.thre_conv1, + conv_step=self.step_conv1, + ) data_pooled1 = self.pooling(data_conved1, self.size_pooling1) - return data_conved1,data_pooled1 + return data_conved1, data_pooled1 -if __name__ == '__main__': - ''' +if __name__ == "__main__": + """ I will put the example on other file - ''' + """ diff --git a/neural_network/perceptron.py b/neural_network/perceptron.py index fdc710597241..5feb8610a911 100644 --- a/neural_network/perceptron.py +++ b/neural_network/perceptron.py @@ -1,4 +1,4 @@ -''' +""" Perceptron w = w + N * (d(k) - y) * x(k) @@ -8,7 +8,7 @@ p1 = -1 p2 = 1 -''' +""" import random @@ -28,7 +28,7 @@ def training(self): sample.insert(0, self.bias) for i in range(self.col_sample): - self.weight.append(random.random()) + self.weight.append(random.random()) self.weight.insert(0, self.bias) @@ -45,15 +45,18 @@ def training(self): for j in range(self.col_sample + 1): - self.weight[j] = self.weight[j] + self.learn_rate * (self.exit[i] - y) * self.sample[i][j] + self.weight[j] = ( + self.weight[j] + + self.learn_rate * (self.exit[i] - y) * self.sample[i][j] + ) erro = True - #print('Epoch: \n',epoch_count) + # print('Epoch: \n',epoch_count) epoch_count = epoch_count + 1 # if you want controle the epoch or just by erro if erro == False: - print(('\nEpoch:\n',epoch_count)) - print('------------------------\n') - #if epoch_count > self.epoch_number or not erro: + print(("\nEpoch:\n", epoch_count)) + print("------------------------\n") + # if epoch_count > self.epoch_number or not erro: break def sort(self, sample): @@ -64,12 +67,12 @@ def sort(self, sample): y = self.sign(u) - if y == -1: - print(('Sample: ', sample)) - print('classification: P1') + if y == -1: + print(("Sample: ", sample)) + print("classification: P1") else: - print(('Sample: ', sample)) - print('classification: P2') + print(("Sample: ", sample)) + print("classification: P2") def sign(self, u): return 1 if u >= 0 else -1 @@ -105,19 +108,51 @@ def sign(self, u): [-0.1013, 0.5989, 7.1812], [2.4482, 0.9455, 11.2095], [2.0149, 0.6192, 10.9263], - [0.2012, 0.2611, 5.4631] - + [0.2012, 0.2611, 5.4631], ] -exit = [-1, -1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, 1, 1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1] +exit = [ + -1, + -1, + -1, + 1, + 1, + -1, + 1, + -1, + 1, + 1, + -1, + 1, + -1, + -1, + -1, + -1, + 1, + 1, + 1, + 1, + -1, + 1, + 1, + 1, + 1, + -1, + -1, + 1, + -1, + 1, +] -network = Perceptron(sample=samples, exit = exit, learn_rate=0.01, epoch_number=1000, bias=-1) +network = Perceptron( + sample=samples, exit=exit, learn_rate=0.01, epoch_number=1000, bias=-1 +) network.training() -if __name__ == '__main__': +if __name__ == "__main__": while True: sample = [] for i in range(3): - sample.insert(i, float(input('value: '))) + sample.insert(i, float(input("value: "))) network.sort(sample) diff --git a/other/anagrams.py b/other/anagrams.py index 1e6e38dee139..9e103296b382 100644 --- a/other/anagrams.py +++ b/other/anagrams.py @@ -1,29 +1,32 @@ import collections, pprint, time, os start_time = time.time() -print('creating word list...') +print("creating word list...") path = os.path.split(os.path.realpath(__file__)) -with open(path[0] + '/words') as f: +with open(path[0] + "/words") as f: word_list = sorted(list(set([word.strip().lower() for word in f]))) + def signature(word): - return ''.join(sorted(word)) + return "".join(sorted(word)) + word_bysig = collections.defaultdict(list) for word in word_list: word_bysig[signature(word)].append(word) + def anagram(myword): return word_bysig[signature(myword)] -print('finding anagrams...') -all_anagrams = {word: anagram(word) - for word in word_list if len(anagram(word)) > 1} -print('writing anagrams to file...') -with open('anagrams.txt', 'w') as file: - file.write('all_anagrams = ') +print("finding anagrams...") +all_anagrams = {word: anagram(word) for word in word_list if len(anagram(word)) > 1} + +print("writing anagrams to file...") +with open("anagrams.txt", "w") as file: + file.write("all_anagrams = ") file.write(pprint.pformat(all_anagrams)) total_time = round(time.time() - start_time, 2) -print(('Done [', total_time, 'seconds ]')) +print(("Done [", total_time, "seconds ]")) diff --git a/other/binary_exponentiation.py b/other/binary_exponentiation.py index 1a30fb8fd266..dd4e70e74129 100644 --- a/other/binary_exponentiation.py +++ b/other/binary_exponentiation.py @@ -14,7 +14,7 @@ def b_expo(a, b): res = 1 while b > 0: - if b&1: + if b & 1: res *= a a *= a @@ -26,14 +26,15 @@ def b_expo(a, b): def b_expo_mod(a, b, c): res = 1 while b > 0: - if b&1: - res = ((res%c) * (a%c)) % c + if b & 1: + res = ((res % c) * (a % c)) % c a *= a b >>= 1 return res + """ * Wondering how this method works ! * It's pretty simple. diff --git a/other/binary_exponentiation_2.py b/other/binary_exponentiation_2.py index 217a616c99fb..51ec4baf2598 100644 --- a/other/binary_exponentiation_2.py +++ b/other/binary_exponentiation_2.py @@ -14,7 +14,7 @@ def b_expo(a, b): res = 0 while b > 0: - if b&1: + if b & 1: res += a a += a @@ -26,8 +26,8 @@ def b_expo(a, b): def b_expo_mod(a, b, c): res = 0 while b > 0: - if b&1: - res = ((res%c) + (a%c)) % c + if b & 1: + res = ((res % c) + (a % c)) % c a += a b >>= 1 diff --git a/other/detecting_english_programmatically.py b/other/detecting_english_programmatically.py index 8b73ff6cf0c3..4b0bb37ce520 100644 --- a/other/detecting_english_programmatically.py +++ b/other/detecting_english_programmatically.py @@ -1,18 +1,21 @@ import os -UPPERLETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' -LETTERS_AND_SPACE = UPPERLETTERS + UPPERLETTERS.lower() + ' \t\n' +UPPERLETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +LETTERS_AND_SPACE = UPPERLETTERS + UPPERLETTERS.lower() + " \t\n" + def loadDictionary(): path = os.path.split(os.path.realpath(__file__)) englishWords = {} - with open(path[0] + '/dictionary.txt') as dictionaryFile: - for word in dictionaryFile.read().split('\n'): + with open(path[0] + "/dictionary.txt") as dictionaryFile: + for word in dictionaryFile.read().split("\n"): englishWords[word] = None return englishWords + ENGLISH_WORDS = loadDictionary() + def getEnglishCount(message): message = message.upper() message = removeNonLetters(message) @@ -28,14 +31,16 @@ def getEnglishCount(message): return float(matches) / len(possibleWords) + def removeNonLetters(message): lettersOnly = [] for symbol in message: if symbol in LETTERS_AND_SPACE: lettersOnly.append(symbol) - return ''.join(lettersOnly) + return "".join(lettersOnly) + -def isEnglish(message, wordPercentage = 20, letterPercentage = 85): +def isEnglish(message, wordPercentage=20, letterPercentage=85): """ >>> isEnglish('Hello World') True @@ -51,4 +56,5 @@ def isEnglish(message, wordPercentage = 20, letterPercentage = 85): import doctest + doctest.testmod() diff --git a/other/euclidean_gcd.py b/other/euclidean_gcd.py index 13378379f286..c6c11f947a08 100644 --- a/other/euclidean_gcd.py +++ b/other/euclidean_gcd.py @@ -1,5 +1,6 @@ # https://en.wikipedia.org/wiki/Euclidean_algorithm + def euclidean_gcd(a, b): while b: t = b @@ -7,6 +8,7 @@ def euclidean_gcd(a, b): a = t return a + def main(): print("GCD(3, 5) = " + str(euclidean_gcd(3, 5))) print("GCD(5, 3) = " + str(euclidean_gcd(5, 3))) @@ -14,5 +16,6 @@ def main(): print("GCD(3, 6) = " + str(euclidean_gcd(3, 6))) print("GCD(6, 3) = " + str(euclidean_gcd(6, 3))) -if __name__ == '__main__': + +if __name__ == "__main__": main() diff --git a/other/fischer_yates_shuffle.py b/other/fischer_yates_shuffle.py index bc2b136344c7..977e5f131e4f 100644 --- a/other/fischer_yates_shuffle.py +++ b/other/fischer_yates_shuffle.py @@ -7,16 +7,18 @@ """ import random + def FYshuffle(LIST): for i in range(len(LIST)): - a = random.randint(0, len(LIST)-1) - b = random.randint(0, len(LIST)-1) + a = random.randint(0, len(LIST) - 1) + b = random.randint(0, len(LIST) - 1) LIST[a], LIST[b] = LIST[b], LIST[a] return LIST -if __name__ == '__main__': - integers = [0,1,2,3,4,5,6,7] - strings = ['python', 'says', 'hello', '!'] - print('Fisher-Yates Shuffle:') - print('List',integers, strings) - print('FY Shuffle',FYshuffle(integers), FYshuffle(strings)) + +if __name__ == "__main__": + integers = [0, 1, 2, 3, 4, 5, 6, 7] + strings = ["python", "says", "hello", "!"] + print("Fisher-Yates Shuffle:") + print("List", integers, strings) + print("FY Shuffle", FYshuffle(integers), FYshuffle(strings)) diff --git a/other/frequency_finder.py b/other/frequency_finder.py index 6264b25bf303..48760a9deb09 100644 --- a/other/frequency_finder.py +++ b/other/frequency_finder.py @@ -1,29 +1,78 @@ # Frequency Finder # frequency taken from http://en.wikipedia.org/wiki/Letter_frequency -englishLetterFreq = {'E': 12.70, 'T': 9.06, 'A': 8.17, 'O': 7.51, 'I': 6.97, - 'N': 6.75, 'S': 6.33, 'H': 6.09, 'R': 5.99, 'D': 4.25, - 'L': 4.03, 'C': 2.78, 'U': 2.76, 'M': 2.41, 'W': 2.36, - 'F': 2.23, 'G': 2.02, 'Y': 1.97, 'P': 1.93, 'B': 1.29, - 'V': 0.98, 'K': 0.77, 'J': 0.15, 'X': 0.15, 'Q': 0.10, - 'Z': 0.07} -ETAOIN = 'ETAOINSHRDLCUMWFGYPBVKJXQZ' -LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' +englishLetterFreq = { + "E": 12.70, + "T": 9.06, + "A": 8.17, + "O": 7.51, + "I": 6.97, + "N": 6.75, + "S": 6.33, + "H": 6.09, + "R": 5.99, + "D": 4.25, + "L": 4.03, + "C": 2.78, + "U": 2.76, + "M": 2.41, + "W": 2.36, + "F": 2.23, + "G": 2.02, + "Y": 1.97, + "P": 1.93, + "B": 1.29, + "V": 0.98, + "K": 0.77, + "J": 0.15, + "X": 0.15, + "Q": 0.10, + "Z": 0.07, +} +ETAOIN = "ETAOINSHRDLCUMWFGYPBVKJXQZ" +LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + def getLetterCount(message): - letterCount = {'A': 0, 'B': 0, 'C': 0, 'D': 0, 'E': 0, 'F': 0, 'G': 0, 'H': 0, - 'I': 0, 'J': 0, 'K': 0, 'L': 0, 'M': 0, 'N': 0, 'O': 0, 'P': 0, - 'Q': 0, 'R': 0, 'S': 0, 'T': 0, 'U': 0, 'V': 0, 'W': 0, 'X': 0, - 'Y': 0, 'Z': 0} + letterCount = { + "A": 0, + "B": 0, + "C": 0, + "D": 0, + "E": 0, + "F": 0, + "G": 0, + "H": 0, + "I": 0, + "J": 0, + "K": 0, + "L": 0, + "M": 0, + "N": 0, + "O": 0, + "P": 0, + "Q": 0, + "R": 0, + "S": 0, + "T": 0, + "U": 0, + "V": 0, + "W": 0, + "X": 0, + "Y": 0, + "Z": 0, + } for letter in message.upper(): if letter in LETTERS: letterCount[letter] += 1 return letterCount + def getItemAtIndexZero(x): return x[0] + def getFrequencyOrder(message): letterToFreq = getLetterCount(message) freqToLetter = {} @@ -34,23 +83,24 @@ def getFrequencyOrder(message): freqToLetter[letterToFreq[letter]].append(letter) for freq in freqToLetter: - freqToLetter[freq].sort(key = ETAOIN.find, reverse = True) - freqToLetter[freq] = ''.join(freqToLetter[freq]) + freqToLetter[freq].sort(key=ETAOIN.find, reverse=True) + freqToLetter[freq] = "".join(freqToLetter[freq]) freqPairs = list(freqToLetter.items()) - freqPairs.sort(key = getItemAtIndexZero, reverse = True) + freqPairs.sort(key=getItemAtIndexZero, reverse=True) freqOrder = [] for freqPair in freqPairs: freqOrder.append(freqPair[1]) - return ''.join(freqOrder) + return "".join(freqOrder) + def englishFreqMatchScore(message): - ''' + """ >>> englishFreqMatchScore('Hello World') 1 - ''' + """ freqOrder = getFrequencyOrder(message) matchScore = 0 for commonLetter in ETAOIN[:6]: @@ -63,6 +113,8 @@ def englishFreqMatchScore(message): return matchScore -if __name__ == '__main__': + +if __name__ == "__main__": import doctest + doctest.testmod() diff --git a/other/game_of_life.py b/other/game_of_life.py index 1fdaa21b4a7b..2b4d1116fa8c 100644 --- a/other/game_of_life.py +++ b/other/game_of_life.py @@ -1,4 +1,4 @@ -'''Conway's Game Of Life, Author Anurag Kumar(mailto:anuragkumarak95@gmail.com) +"""Conway's Game Of Life, Author Anurag Kumar(mailto:anuragkumarak95@gmail.com) Requirements: - numpy @@ -26,28 +26,31 @@ 4. Any dead cell with exactly three live neighbours be- comes a live cell, as if by reproduction. - ''' + """ import numpy as np import random, sys from matplotlib import pyplot as plt from matplotlib.colors import ListedColormap -usage_doc='Usage of script: script_nama ' +usage_doc = "Usage of script: script_nama " -choice = [0]*100 + [1]*10 +choice = [0] * 100 + [1] * 10 random.shuffle(choice) + def create_canvas(size): - canvas = [ [False for i in range(size)] for j in range(size)] + canvas = [[False for i in range(size)] for j in range(size)] return canvas + def seed(canvas): - for i,row in enumerate(canvas): - for j,_ in enumerate(row): - canvas[i][j]=bool(random.getrandbits(1)) + for i, row in enumerate(canvas): + for j, _ in enumerate(row): + canvas[i][j] = bool(random.getrandbits(1)) + def run(canvas): - ''' This function runs the rules of game through all points, and changes their status accordingly.(in the same canvas) + """ This function runs the rules of game through all points, and changes their status accordingly.(in the same canvas) @Args: -- canvas : canvas of population to run the rules on. @@ -55,63 +58,71 @@ def run(canvas): @returns: -- None - ''' + """ canvas = np.array(canvas) next_gen_canvas = np.array(create_canvas(canvas.shape[0])) for r, row in enumerate(canvas): for c, pt in enumerate(row): # print(r-1,r+2,c-1,c+2) - next_gen_canvas[r][c] = __judge_point(pt,canvas[r-1:r+2,c-1:c+2]) - + next_gen_canvas[r][c] = __judge_point( + pt, canvas[r - 1 : r + 2, c - 1 : c + 2] + ) + canvas = next_gen_canvas - del next_gen_canvas # cleaning memory as we move on. - return canvas.tolist() + del next_gen_canvas # cleaning memory as we move on. + return canvas.tolist() + -def __judge_point(pt,neighbours): - dead = 0 +def __judge_point(pt, neighbours): + dead = 0 alive = 0 # finding dead or alive neighbours count. for i in neighbours: for status in i: - if status: alive+=1 - else: dead+=1 + if status: + alive += 1 + else: + dead += 1 # handling duplicate entry for focus pt. - if pt : alive-=1 - else : dead-=1 - + if pt: + alive -= 1 + else: + dead -= 1 + # running the rules of game here. state = pt if pt: - if alive<2: - state=False - elif alive==2 or alive==3: - state=True - elif alive>3: - state=False + if alive < 2: + state = False + elif alive == 2 or alive == 3: + state = True + elif alive > 3: + state = False else: - if alive==3: - state=True + if alive == 3: + state = True return state -if __name__=='__main__': - if len(sys.argv) != 2: raise Exception(usage_doc) - +if __name__ == "__main__": + if len(sys.argv) != 2: + raise Exception(usage_doc) + canvas_size = int(sys.argv[1]) # main working structure of this module. - c=create_canvas(canvas_size) + c = create_canvas(canvas_size) seed(c) fig, ax = plt.subplots() - fig.show() - cmap = ListedColormap(['w','k']) + fig.show() + cmap = ListedColormap(["w", "k"]) try: while True: - c = run(c) - ax.matshow(c,cmap=cmap) + c = run(c) + ax.matshow(c, cmap=cmap) fig.canvas.draw() - ax.cla() + ax.cla() except KeyboardInterrupt: # do nothing. pass diff --git a/other/linear_congruential_generator.py b/other/linear_congruential_generator.py index 7c592a6400b5..3b150f422e4f 100644 --- a/other/linear_congruential_generator.py +++ b/other/linear_congruential_generator.py @@ -2,12 +2,13 @@ from time import time + class LinearCongruentialGenerator(object): """ A pseudorandom number generator. """ - def __init__( self, multiplier, increment, modulo, seed=int(time()) ): + def __init__(self, multiplier, increment, modulo, seed=int(time())): """ These parameters are saved and used when nextNumber() is called. @@ -19,7 +20,7 @@ def __init__( self, multiplier, increment, modulo, seed=int(time()) ): self.modulo = modulo self.seed = seed - def next_number( self ): + def next_number(self): """ The smallest number that can be generated is zero. The largest number that can be generated is modulo-1. modulo is set in the constructor. @@ -27,8 +28,9 @@ def next_number( self ): self.seed = (self.multiplier * self.seed + self.increment) % self.modulo return self.seed + if __name__ == "__main__": # Show the LCG in action. - lcg = LinearCongruentialGenerator(1664525, 1013904223, 2<<31) - while True : - print(lcg.next_number()) \ No newline at end of file + lcg = LinearCongruentialGenerator(1664525, 1013904223, 2 << 31) + while True: + print(lcg.next_number()) diff --git a/other/nested_brackets.py b/other/nested_brackets.py index 14147eaa6456..011e94b92928 100644 --- a/other/nested_brackets.py +++ b/other/nested_brackets.py @@ -1,4 +1,4 @@ -''' +""" The nested brackets problem is a problem that determines if a sequence of brackets are properly nested. A sequence of brackets s is considered properly nested if any of the following conditions are true: @@ -12,13 +12,15 @@ The function called is_balanced takes as input a string S which is a sequence of brackets and returns true if S is nested and false otherwise. -''' +""" + + def is_balanced(S): stack = [] - open_brackets = set({'(', '[', '{'}) - closed_brackets = set({')', ']', '}'}) - open_to_closed = dict({'{':'}', '[':']', '(':')'}) + open_brackets = set({"(", "[", "{"}) + closed_brackets = set({")", "]", "}"}) + open_to_closed = dict({"{": "}", "[": "]", "(": ")"}) for i in range(len(S)): @@ -26,7 +28,9 @@ def is_balanced(S): stack.append(S[i]) elif S[i] in closed_brackets: - if len(stack) == 0 or (len(stack) > 0 and open_to_closed[stack.pop()] != S[i]): + if len(stack) == 0 or ( + len(stack) > 0 and open_to_closed[stack.pop()] != S[i] + ): return False return len(stack) == 0 diff --git a/other/palindrome.py b/other/palindrome.py index 990ec844f9fb..2ca453b64702 100644 --- a/other/palindrome.py +++ b/other/palindrome.py @@ -22,10 +22,10 @@ def recursive_palindrome(str): def main(): - str = 'ama' + str = "ama" print(recursive_palindrome(str.lower())) print(is_palindrome(str.lower())) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/other/password_generator.py b/other/password_generator.py index 16b7e16b22a1..f72686bfb1c0 100644 --- a/other/password_generator.py +++ b/other/password_generator.py @@ -17,7 +17,7 @@ def password_generator(length=8): 0 """ chars = tuple(ascii_letters) + tuple(digits) + tuple(punctuation) - return ''.join(choice(chars) for x in range(length)) + return "".join(choice(chars) for x in range(length)) # ALTERNATIVE METHODS @@ -42,11 +42,10 @@ def random_characters(ctbi, i): def main(): - length = int( - input('Please indicate the max length of your password: ').strip()) - print('Password generated:', password_generator(length)) - print('[If you are thinking of using this passsword, You better save it.]') + length = int(input("Please indicate the max length of your password: ").strip()) + print("Password generated:", password_generator(length)) + print("[If you are thinking of using this passsword, You better save it.]") -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/other/primelib.py b/other/primelib.py index c000213a7a42..6fc5eddeb257 100644 --- a/other/primelib.py +++ b/other/primelib.py @@ -49,8 +49,9 @@ def isPrime(number): """ # precondition - assert isinstance(number,int) and (number >= 0) , \ - "'number' must been an int and positive" + assert isinstance(number, int) and ( + number >= 0 + ), "'number' must been an int and positive" status = True @@ -58,7 +59,7 @@ def isPrime(number): if number <= 1: status = False - for divisor in range(2,int(round(sqrt(number)))+1): + for divisor in range(2, int(round(sqrt(number))) + 1): # if 'number' divisible by 'divisor' then sets 'status' # of false and break up the loop. @@ -67,12 +68,14 @@ def isPrime(number): break # precondition - assert isinstance(status,bool), "'status' must been from type bool" + assert isinstance(status, bool), "'status' must been from type bool" return status + # ------------------------------------------ + def sieveEr(N): """ input: positive integer 'N' > 2 @@ -84,33 +87,33 @@ def sieveEr(N): """ # precondition - assert isinstance(N,int) and (N > 2), "'N' must been an int and > 2" + assert isinstance(N, int) and (N > 2), "'N' must been an int and > 2" # beginList: conatins all natural numbers from 2 upt to N - beginList = [x for x in range(2,N+1)] + beginList = [x for x in range(2, N + 1)] - ans = [] # this list will be returns. + ans = [] # this list will be returns. # actual sieve of erathostenes for i in range(len(beginList)): - for j in range(i+1,len(beginList)): + for j in range(i + 1, len(beginList)): - if (beginList[i] != 0) and \ - (beginList[j] % beginList[i] == 0): + if (beginList[i] != 0) and (beginList[j] % beginList[i] == 0): beginList[j] = 0 # filters actual prime numbers. ans = [x for x in beginList if x != 0] # precondition - assert isinstance(ans,list), "'ans' must been from type list" + assert isinstance(ans, list), "'ans' must been from type list" return ans # -------------------------------- + def getPrimeNumbers(N): """ input: positive integer 'N' > 2 @@ -119,26 +122,27 @@ def getPrimeNumbers(N): """ # precondition - assert isinstance(N,int) and (N > 2), "'N' must been an int and > 2" + assert isinstance(N, int) and (N > 2), "'N' must been an int and > 2" ans = [] # iterates over all numbers between 2 up to N+1 # if a number is prime then appends to list 'ans' - for number in range(2,N+1): + for number in range(2, N + 1): if isPrime(number): ans.append(number) # precondition - assert isinstance(ans,list), "'ans' must been from type list" + assert isinstance(ans, list), "'ans' must been from type list" return ans # ----------------------------------------- + def primeFactorization(number): """ input: positive integer 'number' @@ -146,10 +150,9 @@ def primeFactorization(number): """ # precondition - assert isinstance(number,int) and number >= 0, \ - "'number' must been an int and >= 0" + assert isinstance(number, int) and number >= 0, "'number' must been an int and >= 0" - ans = [] # this list will be returns of the function. + ans = [] # this list will be returns of the function. # potential prime number factors. @@ -157,7 +160,6 @@ def primeFactorization(number): quotient = number - if number == 0 or number == 1: ans.append(number) @@ -165,25 +167,26 @@ def primeFactorization(number): # if 'number' not prime then builds the prime factorization of 'number' elif not isPrime(number): - while (quotient != 1): + while quotient != 1: if isPrime(factor) and (quotient % factor == 0): - ans.append(factor) - quotient /= factor + ans.append(factor) + quotient /= factor else: - factor += 1 + factor += 1 else: ans.append(number) # precondition - assert isinstance(ans,list), "'ans' must been from type list" + assert isinstance(ans, list), "'ans' must been from type list" return ans # ----------------------------------------- + def greatestPrimeFactor(number): """ input: positive integer 'number' >= 0 @@ -191,8 +194,9 @@ def greatestPrimeFactor(number): """ # precondition - assert isinstance(number,int) and (number >= 0), \ - "'number' bust been an int and >= 0" + assert isinstance(number, int) and ( + number >= 0 + ), "'number' bust been an int and >= 0" ans = 0 @@ -202,7 +206,7 @@ def greatestPrimeFactor(number): ans = max(primeFactors) # precondition - assert isinstance(ans,int), "'ans' must been from type int" + assert isinstance(ans, int), "'ans' must been from type int" return ans @@ -217,8 +221,9 @@ def smallestPrimeFactor(number): """ # precondition - assert isinstance(number,int) and (number >= 0), \ - "'number' bust been an int and >= 0" + assert isinstance(number, int) and ( + number >= 0 + ), "'number' bust been an int and >= 0" ans = 0 @@ -228,13 +233,14 @@ def smallestPrimeFactor(number): ans = min(primeFactors) # precondition - assert isinstance(ans,int), "'ans' must been from type int" + assert isinstance(ans, int), "'ans' must been from type int" return ans # ---------------------- + def isEven(number): """ input: integer 'number' @@ -247,8 +253,10 @@ def isEven(number): return number % 2 == 0 + # ------------------------ + def isOdd(number): """ input: integer 'number' @@ -261,6 +269,7 @@ def isOdd(number): return number % 2 != 0 + # ------------------------ @@ -272,10 +281,11 @@ def goldbach(number): """ # precondition - assert isinstance(number,int) and (number > 2) and isEven(number), \ - "'number' must been an int, even and > 2" + assert ( + isinstance(number, int) and (number > 2) and isEven(number) + ), "'number' must been an int, even and > 2" - ans = [] # this list will returned + ans = [] # this list will returned # creates a list of prime numbers between 2 up to 'number' primeNumbers = getPrimeNumbers(number) @@ -288,12 +298,11 @@ def goldbach(number): # exit variable. for break up the loops loop = True - while (i < lenPN and loop): + while i < lenPN and loop: - j = i+1 + j = i + 1 - - while (j < lenPN and loop): + while j < lenPN and loop: if primeNumbers[i] + primeNumbers[j] == number: loop = False @@ -305,15 +314,21 @@ def goldbach(number): i += 1 # precondition - assert isinstance(ans,list) and (len(ans) == 2) and \ - (ans[0] + ans[1] == number) and isPrime(ans[0]) and isPrime(ans[1]), \ - "'ans' must contains two primes. And sum of elements must been eq 'number'" + assert ( + isinstance(ans, list) + and (len(ans) == 2) + and (ans[0] + ans[1] == number) + and isPrime(ans[0]) + and isPrime(ans[1]) + ), "'ans' must contains two primes. And sum of elements must been eq 'number'" return ans + # ---------------------------------------------- -def gcd(number1,number2): + +def gcd(number1, number2): """ Greatest common divisor input: two positive integer 'number1' and 'number2' @@ -321,9 +336,12 @@ def gcd(number1,number2): """ # precondition - assert isinstance(number1,int) and isinstance(number2,int) \ - and (number1 >= 0) and (number2 >= 0), \ - "'number1' and 'number2' must been positive integer." + assert ( + isinstance(number1, int) + and isinstance(number2, int) + and (number1 >= 0) + and (number2 >= 0) + ), "'number1' and 'number2' must been positive integer." rest = 0 @@ -334,13 +352,16 @@ def gcd(number1,number2): number2 = rest # precondition - assert isinstance(number1,int) and (number1 >= 0), \ - "'number' must been from type int and positive" + assert isinstance(number1, int) and ( + number1 >= 0 + ), "'number' must been from type int and positive" return number1 + # ---------------------------------------------------- + def kgV(number1, number2): """ Least common multiple @@ -349,11 +370,14 @@ def kgV(number1, number2): """ # precondition - assert isinstance(number1,int) and isinstance(number2,int) \ - and (number1 >= 1) and (number2 >= 1), \ - "'number1' and 'number2' must been positive integer." + assert ( + isinstance(number1, int) + and isinstance(number2, int) + and (number1 >= 1) + and (number2 >= 1) + ), "'number1' and 'number2' must been positive integer." - ans = 1 # actual answer that will be return. + ans = 1 # actual answer that will be return. # for kgV (x,1) if number1 > 1 and number2 > 1: @@ -366,12 +390,12 @@ def kgV(number1, number2): primeFac1 = [] primeFac2 = [] - ans = max(number1,number2) + ans = max(number1, number2) count1 = 0 count2 = 0 - done = [] # captured numbers int both 'primeFac1' and 'primeFac2' + done = [] # captured numbers int both 'primeFac1' and 'primeFac2' # iterates through primeFac1 for n in primeFac1: @@ -383,7 +407,7 @@ def kgV(number1, number2): count1 = primeFac1.count(n) count2 = primeFac2.count(n) - for i in range(max(count1,count2)): + for i in range(max(count1, count2)): ans *= n else: @@ -408,13 +432,16 @@ def kgV(number1, number2): done.append(n) # precondition - assert isinstance(ans,int) and (ans >= 0), \ - "'ans' must been from type int and positive" + assert isinstance(ans, int) and ( + ans >= 0 + ), "'ans' must been from type int and positive" return ans + # ---------------------------------- + def getPrime(n): """ Gets the n-th prime number. @@ -423,16 +450,16 @@ def getPrime(n): """ # precondition - assert isinstance(n,int) and (n >= 0), "'number' must been a positive int" + assert isinstance(n, int) and (n >= 0), "'number' must been a positive int" index = 0 - ans = 2 # this variable holds the answer + ans = 2 # this variable holds the answer while index < n: index += 1 - ans += 1 # counts to the next number + ans += 1 # counts to the next number # if ans not prime then # runs to the next prime number. @@ -440,13 +467,16 @@ def getPrime(n): ans += 1 # precondition - assert isinstance(ans,int) and isPrime(ans), \ - "'ans' must been a prime number and from type int" + assert isinstance(ans, int) and isPrime( + ans + ), "'ans' must been a prime number and from type int" return ans + # --------------------------------------------------- + def getPrimesBetween(pNumber1, pNumber2): """ input: prime numbers 'pNumber1' and 'pNumber2' @@ -456,12 +486,13 @@ def getPrimesBetween(pNumber1, pNumber2): """ # precondition - assert isPrime(pNumber1) and isPrime(pNumber2) and (pNumber1 < pNumber2), \ - "The arguments must been prime numbers and 'pNumber1' < 'pNumber2'" + assert ( + isPrime(pNumber1) and isPrime(pNumber2) and (pNumber1 < pNumber2) + ), "The arguments must been prime numbers and 'pNumber1' < 'pNumber2'" - number = pNumber1 + 1 # jump to the next number + number = pNumber1 + 1 # jump to the next number - ans = [] # this list will be returns. + ans = [] # this list will be returns. # if number is not prime then # fetch the next prime number. @@ -479,15 +510,17 @@ def getPrimesBetween(pNumber1, pNumber2): number += 1 # precondition - assert isinstance(ans,list) and ans[0] != pNumber1 \ - and ans[len(ans)-1] != pNumber2, \ - "'ans' must been a list without the arguments" + assert ( + isinstance(ans, list) and ans[0] != pNumber1 and ans[len(ans) - 1] != pNumber2 + ), "'ans' must been a list without the arguments" # 'ans' contains not 'pNumber1' and 'pNumber2' ! return ans + # ---------------------------------------------------- + def getDivisors(n): """ input: positive integer 'n' >= 1 @@ -495,20 +528,17 @@ def getDivisors(n): """ # precondition - assert isinstance(n,int) and (n >= 1), "'n' must been int and >= 1" + assert isinstance(n, int) and (n >= 1), "'n' must been int and >= 1" - ans = [] # will be returned. + ans = [] # will be returned. - for divisor in range(1,n+1): + for divisor in range(1, n + 1): if n % divisor == 0: ans.append(divisor) - - #precondition - assert ans[0] == 1 and ans[len(ans)-1] == n, \ - "Error in function getDivisiors(...)" - + # precondition + assert ans[0] == 1 and ans[len(ans) - 1] == n, "Error in function getDivisiors(...)" return ans @@ -523,21 +553,26 @@ def isPerfectNumber(number): """ # precondition - assert isinstance(number,int) and (number > 1), \ - "'number' must been an int and >= 1" + assert isinstance(number, int) and ( + number > 1 + ), "'number' must been an int and >= 1" divisors = getDivisors(number) # precondition - assert isinstance(divisors,list) and(divisors[0] == 1) and \ - (divisors[len(divisors)-1] == number), \ - "Error in help-function getDivisiors(...)" + assert ( + isinstance(divisors, list) + and (divisors[0] == 1) + and (divisors[len(divisors) - 1] == number) + ), "Error in help-function getDivisiors(...)" # summed all divisors up to 'number' (exclusive), hence [:-1] return sum(divisors[:-1]) == number + # ------------------------------------------------------------ + def simplifyFraction(numerator, denominator): """ input: two integer 'numerator' and 'denominator' @@ -546,22 +581,28 @@ def simplifyFraction(numerator, denominator): """ # precondition - assert isinstance(numerator, int) and isinstance(denominator,int) \ - and (denominator != 0), \ - "The arguments must been from type int and 'denominator' != 0" + assert ( + isinstance(numerator, int) + and isinstance(denominator, int) + and (denominator != 0) + ), "The arguments must been from type int and 'denominator' != 0" # build the greatest common divisor of numerator and denominator. gcdOfFraction = gcd(abs(numerator), abs(denominator)) # precondition - assert isinstance(gcdOfFraction, int) and (numerator % gcdOfFraction == 0) \ - and (denominator % gcdOfFraction == 0), \ - "Error in function gcd(...,...)" + assert ( + isinstance(gcdOfFraction, int) + and (numerator % gcdOfFraction == 0) + and (denominator % gcdOfFraction == 0) + ), "Error in function gcd(...,...)" return (numerator // gcdOfFraction, denominator // gcdOfFraction) + # ----------------------------------------------------------------- + def factorial(n): """ input: positive integer 'n' @@ -569,17 +610,19 @@ def factorial(n): """ # precondition - assert isinstance(n,int) and (n >= 0), "'n' must been a int and >= 0" + assert isinstance(n, int) and (n >= 0), "'n' must been a int and >= 0" - ans = 1 # this will be return. + ans = 1 # this will be return. - for factor in range(1,n+1): + for factor in range(1, n + 1): ans *= factor return ans + # ------------------------------------------------------------------- + def fib(n): """ input: positive integer 'n' @@ -591,9 +634,9 @@ def fib(n): tmp = 0 fib1 = 1 - ans = 1 # this will be return + ans = 1 # this will be return - for i in range(n-1): + for i in range(n - 1): tmp = ans ans += fib1 diff --git a/other/sierpinski_triangle.py b/other/sierpinski_triangle.py index fc22aad96059..0e6ce43e35d3 100644 --- a/other/sierpinski_triangle.py +++ b/other/sierpinski_triangle.py @@ -1,7 +1,7 @@ #!/usr/bin/python # encoding=utf8 -'''Author Anurag Kumar | anuragkumarak95@gmail.com | git/anuragkumarak95 +"""Author Anurag Kumar | anuragkumarak95@gmail.com | git/anuragkumarak95 Simple example of Fractal generation using recursive function. @@ -23,46 +23,51 @@ Credits: This code was written by editing the code from http://www.riannetrujillo.com/blog/python-fractal/ -''' +""" import turtle import sys -PROGNAME = 'Sierpinski Triangle' -points = [[-175,-125],[0,175],[175,-125]] #size of triangle +PROGNAME = "Sierpinski Triangle" -def getMid(p1,p2): - return ( (p1[0]+p2[0]) / 2, (p1[1] + p2[1]) / 2) #find midpoint +points = [[-175, -125], [0, 175], [175, -125]] # size of triangle -def triangle(points,depth): + +def getMid(p1, p2): + return ((p1[0] + p2[0]) / 2, (p1[1] + p2[1]) / 2) # find midpoint + + +def triangle(points, depth): myPen.up() - myPen.goto(points[0][0],points[0][1]) + myPen.goto(points[0][0], points[0][1]) myPen.down() - myPen.goto(points[1][0],points[1][1]) - myPen.goto(points[2][0],points[2][1]) - myPen.goto(points[0][0],points[0][1]) - - if depth>0: - triangle([points[0], - getMid(points[0], points[1]), - getMid(points[0], points[2])], - depth-1) - triangle([points[1], - getMid(points[0], points[1]), - getMid(points[1], points[2])], - depth-1) - triangle([points[2], - getMid(points[2], points[1]), - getMid(points[0], points[2])], - depth-1) - - -if __name__ == '__main__': - if len(sys.argv) !=2: - raise ValueError('right format for using this script: ' - '$python fractals.py ') + myPen.goto(points[1][0], points[1][1]) + myPen.goto(points[2][0], points[2][1]) + myPen.goto(points[0][0], points[0][1]) + + if depth > 0: + triangle( + [points[0], getMid(points[0], points[1]), getMid(points[0], points[2])], + depth - 1, + ) + triangle( + [points[1], getMid(points[0], points[1]), getMid(points[1], points[2])], + depth - 1, + ) + triangle( + [points[2], getMid(points[2], points[1]), getMid(points[0], points[2])], + depth - 1, + ) + + +if __name__ == "__main__": + if len(sys.argv) != 2: + raise ValueError( + "right format for using this script: " + "$python fractals.py " + ) myPen = turtle.Turtle() myPen.ht() myPen.speed(5) - myPen.pencolor('red') - triangle(points,int(sys.argv[1])) + myPen.pencolor("red") + triangle(points, int(sys.argv[1])) diff --git a/other/tower_of_hanoi.py b/other/tower_of_hanoi.py index cd6fbf4d88ac..3cc0e40b369f 100644 --- a/other/tower_of_hanoi.py +++ b/other/tower_of_hanoi.py @@ -1,5 +1,5 @@ def moveTower(height, fromPole, toPole, withPole): - ''' + """ >>> moveTower(3, 'A', 'B', 'C') moving disk from A to B moving disk from A to C @@ -8,18 +8,21 @@ def moveTower(height, fromPole, toPole, withPole): moving disk from C to A moving disk from C to B moving disk from A to B - ''' + """ if height >= 1: - moveTower(height-1, fromPole, withPole, toPole) + moveTower(height - 1, fromPole, withPole, toPole) moveDisk(fromPole, toPole) - moveTower(height-1, withPole, toPole, fromPole) + moveTower(height - 1, withPole, toPole, fromPole) + + +def moveDisk(fp, tp): + print("moving disk from", fp, "to", tp) -def moveDisk(fp,tp): - print('moving disk from', fp, 'to', tp) def main(): - height = int(input('Height of hanoi: ').strip()) - moveTower(height, 'A', 'B', 'C') + height = int(input("Height of hanoi: ").strip()) + moveTower(height, "A", "B", "C") + -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/other/two_sum.py b/other/two_sum.py index b784da82767a..70d5c5375026 100644 --- a/other/two_sum.py +++ b/other/two_sum.py @@ -9,6 +9,8 @@ Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1]. """ + + def twoSum(nums, target): """ :type nums: List[int] @@ -17,11 +19,11 @@ def twoSum(nums, target): """ chk_map = {} for index, val in enumerate(nums): - compl = target - val - if compl in chk_map: - indices = [chk_map[compl], index] - print(indices) - return [indices] - else: - chk_map[val] = index + compl = target - val + if compl in chk_map: + indices = [chk_map[compl], index] + print(indices) + return [indices] + else: + chk_map[val] = index return False diff --git a/other/word_patterns.py b/other/word_patterns.py index 1364d1277255..16089019704b 100644 --- a/other/word_patterns.py +++ b/other/word_patterns.py @@ -1,5 +1,6 @@ import pprint, time + def getWordPattern(word): word = word.upper() nextNum = 0 @@ -11,14 +12,15 @@ def getWordPattern(word): letterNums[letter] = str(nextNum) nextNum += 1 wordPattern.append(letterNums[letter]) - return '.'.join(wordPattern) + return ".".join(wordPattern) + def main(): startTime = time.time() allPatterns = {} - with open('Dictionary.txt') as fo: - wordList = fo.read().split('\n') + with open("Dictionary.txt") as fo: + wordList = fo.read().split("\n") for word in wordList: pattern = getWordPattern(word) @@ -28,11 +30,12 @@ def main(): else: allPatterns[pattern].append(word) - with open('Word Patterns.txt', 'w') as fo: + with open("Word Patterns.txt", "w") as fo: fo.write(pprint.pformat(allPatterns)) totalTime = round(time.time() - startTime, 2) - print(('Done! [', totalTime, 'seconds ]')) + print(("Done! [", totalTime, "seconds ]")) + -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/project_euler/problem_01/sol1.py b/project_euler/problem_01/sol1.py index 76b13b852c87..e81156edaee4 100644 --- a/project_euler/problem_01/sol1.py +++ b/project_euler/problem_01/sol1.py @@ -4,6 +4,8 @@ we get 3,5,6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below N. """ + + def solution(n): """Returns the sum of all the multiples of 3 or 5 below n. diff --git a/project_euler/problem_01/sol3.py b/project_euler/problem_01/sol3.py index 532203ddd95d..c0bcbc06ec83 100644 --- a/project_euler/problem_01/sol3.py +++ b/project_euler/problem_01/sol3.py @@ -4,6 +4,8 @@ we get 3,5,6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below N. """ + + def solution(n): """ This solution is based on the pattern that the successive numbers in the diff --git a/project_euler/problem_01/sol4.py b/project_euler/problem_01/sol4.py index 3e6712618870..e01dc977d8cf 100644 --- a/project_euler/problem_01/sol4.py +++ b/project_euler/problem_01/sol4.py @@ -4,6 +4,8 @@ we get 3,5,6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below N. """ + + def solution(n): """Returns the sum of all the multiples of 3 or 5 below n. diff --git a/project_euler/problem_01/sol6.py b/project_euler/problem_01/sol6.py index b9c3db4f8550..c9f94b9f77c8 100644 --- a/project_euler/problem_01/sol6.py +++ b/project_euler/problem_01/sol6.py @@ -4,6 +4,8 @@ we get 3,5,6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below N. """ + + def solution(n): """Returns the sum of all the multiples of 3 or 5 below n. diff --git a/project_euler/problem_02/sol1.py b/project_euler/problem_02/sol1.py index d2ad67e2f424..ec89ddaeb2b5 100644 --- a/project_euler/problem_02/sol1.py +++ b/project_euler/problem_02/sol1.py @@ -9,6 +9,8 @@ n, find the sum of the even-valued terms. e.g. for n=10, we have {2,8}, sum is 10. """ + + def solution(n): """Returns the sum of all fibonacci sequence even elements that are lower or equals to n. diff --git a/project_euler/problem_02/sol2.py b/project_euler/problem_02/sol2.py index 71f51b695e84..bc5040cc6b3b 100644 --- a/project_euler/problem_02/sol2.py +++ b/project_euler/problem_02/sol2.py @@ -9,6 +9,8 @@ n, find the sum of the even-valued terms. e.g. for n=10, we have {2,8}, sum is 10. """ + + def solution(n): """Returns the sum of all fibonacci sequence even elements that are lower or equals to n. diff --git a/project_euler/problem_02/sol3.py b/project_euler/problem_02/sol3.py index c698b8e38ab2..f29f21c287e5 100644 --- a/project_euler/problem_02/sol3.py +++ b/project_euler/problem_02/sol3.py @@ -9,6 +9,8 @@ n, find the sum of the even-valued terms. e.g. for n=10, we have {2,8}, sum is 10. """ + + def solution(n): """Returns the sum of all fibonacci sequence even elements that are lower or equals to n. diff --git a/project_euler/problem_04/sol1.py b/project_euler/problem_04/sol1.py index 51417b146bbf..53fff8bed4d4 100644 --- a/project_euler/problem_04/sol1.py +++ b/project_euler/problem_04/sol1.py @@ -6,6 +6,8 @@ Find the largest palindrome made from the product of two 3-digit numbers which is less than N. """ + + def solution(n): """Returns the largest palindrome made from the product of two 3-digit numbers which is less than n. @@ -31,9 +33,7 @@ def solution(n): # if 'number' is a product of two 3-digit numbers # then number is the answer otherwise fetch next number. while divisor != 99: - if (number % divisor == 0) and ( - len(str(int(number / divisor))) == 3 - ): + if (number % divisor == 0) and (len(str(int(number / divisor))) == 3): return number divisor -= 1 diff --git a/project_euler/problem_04/sol2.py b/project_euler/problem_04/sol2.py index 8740ee44a4b4..ecc503912c34 100644 --- a/project_euler/problem_04/sol2.py +++ b/project_euler/problem_04/sol2.py @@ -6,6 +6,8 @@ Find the largest palindrome made from the product of two 3-digit numbers which is less than N. """ + + def solution(n): """Returns the largest palindrome made from the product of two 3-digit numbers which is less than n. diff --git a/project_euler/problem_05/sol1.py b/project_euler/problem_05/sol1.py index 83c387e4ae6e..b3a231f4dcf5 100644 --- a/project_euler/problem_05/sol1.py +++ b/project_euler/problem_05/sol1.py @@ -6,6 +6,8 @@ What is the smallest positive number that is evenly divisible(divisible with no remainder) by all of the numbers from 1 to N? """ + + def solution(n): """Returns the smallest positive number that is evenly divisible(divisible with no remainder) by all of the numbers from 1 to n. diff --git a/project_euler/problem_06/sol1.py b/project_euler/problem_06/sol1.py index 0a964272e7e8..c69b6c89e35a 100644 --- a/project_euler/problem_06/sol1.py +++ b/project_euler/problem_06/sol1.py @@ -14,6 +14,8 @@ Find the difference between the sum of the squares of the first N natural numbers and the square of the sum. """ + + def solution(n): """Returns the difference between the sum of the squares of the first n natural numbers and the square of the sum. diff --git a/project_euler/problem_06/sol2.py b/project_euler/problem_06/sol2.py index 45d08d244647..1698a3fb61fd 100644 --- a/project_euler/problem_06/sol2.py +++ b/project_euler/problem_06/sol2.py @@ -14,6 +14,8 @@ Find the difference between the sum of the squares of the first N natural numbers and the square of the sum. """ + + def solution(n): """Returns the difference between the sum of the squares of the first n natural numbers and the square of the sum. diff --git a/project_euler/problem_07/sol2.py b/project_euler/problem_07/sol2.py index 7d078af32176..5d30e540b3e7 100644 --- a/project_euler/problem_07/sol2.py +++ b/project_euler/problem_07/sol2.py @@ -6,6 +6,8 @@ We can see that the 6th prime is 13. What is the Nth prime number? """ + + def isprime(number): for i in range(2, int(number ** 0.5) + 1): if number % i == 0: diff --git a/project_euler/problem_09/sol2.py b/project_euler/problem_09/sol2.py index 502f334417c8..de7b12d40c09 100644 --- a/project_euler/problem_09/sol2.py +++ b/project_euler/problem_09/sol2.py @@ -7,6 +7,8 @@ There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc. """ + + def solution(n): """ Return the product of a,b,c which are Pythagorean Triplet that satisfies diff --git a/project_euler/problem_09/sol3.py b/project_euler/problem_09/sol3.py index bbe7dcf743e7..a6df46a3a66b 100644 --- a/project_euler/problem_09/sol3.py +++ b/project_euler/problem_09/sol3.py @@ -10,6 +10,8 @@ There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc. """ + + def solution(): """ Returns the product of a,b,c which are Pythagorean Triplet that satisfies diff --git a/project_euler/problem_11/sol1.py b/project_euler/problem_11/sol1.py index 1473439ae00d..4e49013c8210 100644 --- a/project_euler/problem_11/sol1.py +++ b/project_euler/problem_11/sol1.py @@ -39,12 +39,8 @@ def largest_product(grid): # for nxn grid) for i in range(nColumns): for j in range(nRows - 3): - vertProduct = ( - grid[j][i] * grid[j + 1][i] * grid[j + 2][i] * grid[j + 3][i] - ) - horzProduct = ( - grid[i][j] * grid[i][j + 1] * grid[i][j + 2] * grid[i][j + 3] - ) + vertProduct = grid[j][i] * grid[j + 1][i] * grid[j + 2][i] * grid[j + 3][i] + horzProduct = grid[i][j] * grid[i][j + 1] * grid[i][j + 2] * grid[i][j + 3] # Left-to-right diagonal (\) product if i < nColumns - 3: @@ -64,9 +60,7 @@ def largest_product(grid): * grid[i - 3][j + 3] ) - maxProduct = max( - vertProduct, horzProduct, lrDiagProduct, rlDiagProduct - ) + maxProduct = max(vertProduct, horzProduct, lrDiagProduct, rlDiagProduct) if maxProduct > largest: largest = maxProduct diff --git a/project_euler/problem_11/sol2.py b/project_euler/problem_11/sol2.py index be6c11a378ad..64702e852b0f 100644 --- a/project_euler/problem_11/sol2.py +++ b/project_euler/problem_11/sol2.py @@ -57,24 +57,14 @@ def solution(): # diagonal 1 for i in range(17): for j in range(17): - temp = ( - l[i][j] - * l[i + 1][j + 1] - * l[i + 2][j + 2] - * l[i + 3][j + 3] - ) + temp = l[i][j] * l[i + 1][j + 1] * l[i + 2][j + 2] * l[i + 3][j + 3] if temp > maximum: maximum = temp # diagonal 2 for i in range(17): for j in range(3, 20): - temp = ( - l[i][j] - * l[i + 1][j - 1] - * l[i + 2][j - 2] - * l[i + 3][j - 3] - ) + temp = l[i][j] * l[i + 1][j - 1] * l[i + 2][j - 2] * l[i + 3][j - 3] if temp > maximum: maximum = temp return maximum diff --git a/project_euler/problem_12/sol2.py b/project_euler/problem_12/sol2.py index 97a4910723ac..5ff0d8349b90 100644 --- a/project_euler/problem_12/sol2.py +++ b/project_euler/problem_12/sol2.py @@ -21,15 +21,15 @@ What is the value of the first triangle number to have over five hundred divisors? """ + + def triangle_number_generator(): for n in range(1, 1000000): yield n * (n + 1) // 2 def count_divisors(n): - return sum( - [2 for i in range(1, int(n ** 0.5) + 1) if n % i == 0 and i * i != n] - ) + return sum([2 for i in range(1, int(n ** 0.5) + 1) if n % i == 0 and i * i != n]) def solution(): @@ -40,9 +40,7 @@ def solution(): # >>> solution() # 76576500 """ - return next( - i for i in triangle_number_generator() if count_divisors(i) > 500 - ) + return next(i for i in triangle_number_generator() if count_divisors(i) > 500) if __name__ == "__main__": diff --git a/project_euler/problem_14/sol1.py b/project_euler/problem_14/sol1.py index 156322b7d507..ab09937fb315 100644 --- a/project_euler/problem_14/sol1.py +++ b/project_euler/problem_14/sol1.py @@ -16,6 +16,8 @@ Which starting number, under one million, produces the longest chain? """ + + def solution(n): """Returns the number under n that generates the longest sequence using the formula: @@ -56,11 +58,5 @@ def solution(n): if __name__ == "__main__": result = solution(int(input().strip())) print( - ( - "Largest Number:", - result["largest_number"], - "->", - result["counter"], - "digits", - ) + ("Largest Number:", result["largest_number"], "->", result["counter"], "digits") ) diff --git a/project_euler/problem_14/sol2.py b/project_euler/problem_14/sol2.py index 25ebd41571c2..9b8857e710b4 100644 --- a/project_euler/problem_14/sol2.py +++ b/project_euler/problem_14/sol2.py @@ -24,6 +24,8 @@ Which starting number, under one million, produces the longest chain? """ + + def collatz_sequence(n): """Returns the Collatz sequence for n.""" sequence = [n] diff --git a/project_euler/problem_15/sol1.py b/project_euler/problem_15/sol1.py index de58bb436d68..1be7d10ed674 100644 --- a/project_euler/problem_15/sol1.py +++ b/project_euler/problem_15/sol1.py @@ -35,9 +35,7 @@ def lattice_paths(n): 2 """ - n = ( - 2 * n - ) # middle entry of odd rows starting at row 3 is the solution for n = 1, + n = 2 * n # middle entry of odd rows starting at row 3 is the solution for n = 1, # 2, 3,... k = n / 2 diff --git a/project_euler/problem_18/solution.py b/project_euler/problem_18/solution.py index f9762e8b0176..38593813901e 100644 --- a/project_euler/problem_18/solution.py +++ b/project_euler/problem_18/solution.py @@ -39,12 +39,12 @@ def solution(): 1074 """ script_dir = os.path.dirname(os.path.realpath(__file__)) - triangle = os.path.join(script_dir, 'triangle.txt') + triangle = os.path.join(script_dir, "triangle.txt") - with open(triangle, 'r') as f: + with open(triangle, "r") as f: triangle = f.readlines() - a = [[int(y) for y in x.rstrip('\r\n').split(' ')] for x in triangle] + a = [[int(y) for y in x.rstrip("\r\n").split(" ")] for x in triangle] for i in range(1, len(a)): for j in range(len(a[i])): diff --git a/project_euler/problem_21/sol1.py b/project_euler/problem_21/sol1.py index a890e6a98611..49c2db964316 100644 --- a/project_euler/problem_21/sol1.py +++ b/project_euler/problem_21/sol1.py @@ -16,6 +16,8 @@ Evaluate the sum of all the amicable numbers under 10000. """ + + def sum_of_divisors(n): total = 0 for i in range(1, int(sqrt(n) + 1)): @@ -44,8 +46,7 @@ def solution(n): [ i for i in range(1, n) - if sum_of_divisors(sum_of_divisors(i)) == i - and sum_of_divisors(i) != i + if sum_of_divisors(sum_of_divisors(i)) == i and sum_of_divisors(i) != i ] ) return total diff --git a/project_euler/problem_23/sol1.py b/project_euler/problem_23/sol1.py index e76be053040f..a72b6123e3ee 100644 --- a/project_euler/problem_23/sol1.py +++ b/project_euler/problem_23/sol1.py @@ -18,7 +18,8 @@ of two abundant numbers. """ -def solution(limit = 28123): + +def solution(limit=28123): """ Finds the sum of all the positive integers which cannot be written as the sum of two abundant numbers @@ -42,7 +43,7 @@ def solution(limit = 28123): abundants.add(n) if not any((n - a in abundants) for a in abundants): - res+=n + res += n return res diff --git a/project_euler/problem_234/sol1.py b/project_euler/problem_234/sol1.py index c0d2949285e9..28d82b550c85 100644 --- a/project_euler/problem_234/sol1.py +++ b/project_euler/problem_234/sol1.py @@ -17,18 +17,19 @@ What is the sum of all semidivisible numbers not exceeding 999966663333 ? """ + def fib(a, b, n): - - if n==1: + + if n == 1: return a - elif n==2: + elif n == 2: return b - elif n==3: - return str(a)+str(b) - + elif n == 3: + return str(a) + str(b) + temp = 0 - for x in range(2,n): - c=str(a) + str(b) + for x in range(2, n): + c = str(a) + str(b) temp = b b = c a = temp @@ -39,14 +40,14 @@ def solution(n): """Returns the sum of all semidivisible numbers not exceeding n.""" semidivisible = [] for x in range(n): - l=[i for i in input().split()] - c2=1 - while(1): - if len(fib(l[0],l[1],c2)) int: """ Considering natural numbers of the form, a**b, where a, b < 100, @@ -18,9 +16,17 @@ def maximum_digital_sum(a: int, b: int) -> int: """ # RETURN the MAXIMUM from the list of SUMs of the list of INT converted from STR of BASE raised to the POWER - return max([sum([int(x) for x in str(base**power)]) for base in range(a) for power in range(b)]) + return max( + [ + sum([int(x) for x in str(base ** power)]) + for base in range(a) + for power in range(b) + ] + ) -#Tests + +# Tests if __name__ == "__main__": import doctest + doctest.testmod() diff --git a/project_euler/problem_67/sol1.py b/project_euler/problem_67/sol1.py index 2da757e303aa..9494ff7bbabd 100644 --- a/project_euler/problem_67/sol1.py +++ b/project_euler/problem_67/sol1.py @@ -23,12 +23,12 @@ def solution(): 7273 """ script_dir = os.path.dirname(os.path.realpath(__file__)) - triangle = os.path.join(script_dir, 'triangle.txt') + triangle = os.path.join(script_dir, "triangle.txt") - with open(triangle, 'r') as f: + with open(triangle, "r") as f: triangle = f.readlines() - a = map(lambda x: x.rstrip('\r\n').split(' '), triangle) + a = map(lambda x: x.rstrip("\r\n").split(" "), triangle) a = list(map(lambda x: list(map(lambda y: int(y), x)), a)) for i in range(1, len(a)): diff --git a/scripts/build_directory_md.py b/scripts/build_directory_md.py index b39edca6c933..cae38c13dbf4 100755 --- a/scripts/build_directory_md.py +++ b/scripts/build_directory_md.py @@ -14,7 +14,7 @@ def good_filepaths(top_dir: str = ".") -> Iterator[str]: continue if os.path.splitext(filename)[1] in (".py", ".ipynb"): yield os.path.join(dirpath, filename).lstrip("./") - + def md_prefix(i): return f"{i * ' '}*" if i else "##" @@ -36,7 +36,9 @@ def print_directory_md(top_dir: str = ".") -> None: if filepath != old_path: old_path = print_path(old_path, filepath) indent = (filepath.count(os.sep) + 1) if filepath else 0 - url = "/".join((URL_BASE, filepath.split(os.sep)[1], filename)).replace(" ", "%20") + url = "/".join((URL_BASE, filepath.split(os.sep)[1], filename)).replace( + " ", "%20" + ) filename = os.path.splitext(filename.replace("_", " "))[0] print(f"{md_prefix(indent)} [{filename}]({url})") diff --git a/scripts/validate_filenames.py b/scripts/validate_filenames.py index 9e1f1503321b..51dd6a40cb41 100755 --- a/scripts/validate_filenames.py +++ b/scripts/validate_filenames.py @@ -25,4 +25,5 @@ bad_files = len(upper_files + space_files + nodir_files) if bad_files: import sys + sys.exit(bad_files) diff --git a/searches/binary_search.py b/searches/binary_search.py index 77abf90239ab..9237c0e1f6f5 100644 --- a/searches/binary_search.py +++ b/searches/binary_search.py @@ -79,6 +79,7 @@ def binary_search_std_lib(sorted_collection, item): return index return None + def binary_search_by_recursion(sorted_collection, item, left, right): """Pure implementation of binary search algorithm in Python by recursion @@ -104,7 +105,7 @@ def binary_search_by_recursion(sorted_collection, item, left, right): >>> binary_search_std_lib([0, 5, 7, 10, 15], 6) """ - if (right < left): + if right < left: return None midpoint = left + (right - left) // 2 @@ -112,9 +113,10 @@ def binary_search_by_recursion(sorted_collection, item, left, right): if sorted_collection[midpoint] == item: return midpoint elif sorted_collection[midpoint] > item: - return binary_search_by_recursion(sorted_collection, item, left, midpoint-1) + return binary_search_by_recursion(sorted_collection, item, left, midpoint - 1) else: - return binary_search_by_recursion(sorted_collection, item, midpoint+1, right) + return binary_search_by_recursion(sorted_collection, item, midpoint + 1, right) + def __assert_sorted(collection): """Check if collection is ascending sorted, if not - raises :py:class:`ValueError` @@ -133,23 +135,24 @@ def __assert_sorted(collection): ValueError: Collection must be ascending sorted """ if collection != sorted(collection): - raise ValueError('Collection must be ascending sorted') + raise ValueError("Collection must be ascending sorted") return True -if __name__ == '__main__': +if __name__ == "__main__": import sys - user_input = input('Enter numbers separated by comma:\n').strip() - collection = [int(item) for item in user_input.split(',')] + + user_input = input("Enter numbers separated by comma:\n").strip() + collection = [int(item) for item in user_input.split(",")] try: __assert_sorted(collection) except ValueError: - sys.exit('Sequence must be ascending sorted to apply binary search') + sys.exit("Sequence must be ascending sorted to apply binary search") - target_input = input('Enter a single number to be found in the list:\n') + target_input = input("Enter a single number to be found in the list:\n") target = int(target_input) result = binary_search(collection, target) if result is not None: - print('{} found at positions: {}'.format(target, result)) + print("{} found at positions: {}".format(target, result)) else: - print('Not found') + print("Not found") diff --git a/searches/interpolation_search.py b/searches/interpolation_search.py index 27ee979bb649..d1873083bf8a 100644 --- a/searches/interpolation_search.py +++ b/searches/interpolation_search.py @@ -15,27 +15,29 @@ def interpolation_search(sorted_collection, item): right = len(sorted_collection) - 1 while left <= right: - #avoid devided by 0 during interpolation - if sorted_collection[left]==sorted_collection[right]: - if sorted_collection[left]==item: + # avoid devided by 0 during interpolation + if sorted_collection[left] == sorted_collection[right]: + if sorted_collection[left] == item: return left else: return None - point = left + ((item - sorted_collection[left]) * (right - left)) // (sorted_collection[right] - sorted_collection[left]) + point = left + ((item - sorted_collection[left]) * (right - left)) // ( + sorted_collection[right] - sorted_collection[left] + ) - #out of range check - if point<0 or point>=len(sorted_collection): + # out of range check + if point < 0 or point >= len(sorted_collection): return None current_item = sorted_collection[point] if current_item == item: return point else: - if pointright: + elif point > right: left = right right = point else: @@ -45,6 +47,7 @@ def interpolation_search(sorted_collection, item): left = point + 1 return None + def interpolation_search_by_recursion(sorted_collection, item, left, right): """Pure implementation of interpolation search algorithm in Python by recursion @@ -56,30 +59,37 @@ def interpolation_search_by_recursion(sorted_collection, item, left, right): :return: index of found item or None if item is not found """ - #avoid devided by 0 during interpolation - if sorted_collection[left]==sorted_collection[right]: - if sorted_collection[left]==item: + # avoid devided by 0 during interpolation + if sorted_collection[left] == sorted_collection[right]: + if sorted_collection[left] == item: return left else: return None - point = left + ((item - sorted_collection[left]) * (right - left)) // (sorted_collection[right] - sorted_collection[left]) + point = left + ((item - sorted_collection[left]) * (right - left)) // ( + sorted_collection[right] - sorted_collection[left] + ) - #out of range check - if point<0 or point>=len(sorted_collection): + # out of range check + if point < 0 or point >= len(sorted_collection): return None if sorted_collection[point] == item: return point - elif pointright: + elif point > right: return interpolation_search_by_recursion(sorted_collection, item, right, left) else: if sorted_collection[point] > item: - return interpolation_search_by_recursion(sorted_collection, item, left, point-1) + return interpolation_search_by_recursion( + sorted_collection, item, left, point - 1 + ) else: - return interpolation_search_by_recursion(sorted_collection, item, point+1, right) + return interpolation_search_by_recursion( + sorted_collection, item, point + 1, right + ) + def __assert_sorted(collection): """Check if collection is ascending sorted, if not - raises :py:class:`ValueError` @@ -95,11 +105,11 @@ def __assert_sorted(collection): ValueError: Collection must be ascending sorted """ if collection != sorted(collection): - raise ValueError('Collection must be ascending sorted') + raise ValueError("Collection must be ascending sorted") return True -if __name__ == '__main__': +if __name__ == "__main__": import sys """ @@ -116,15 +126,15 @@ def __assert_sorted(collection): debug = 0 if debug == 1: - collection = [10,30,40,45,50,66,77,93] + collection = [10, 30, 40, 45, 50, 66, 77, 93] try: __assert_sorted(collection) except ValueError: - sys.exit('Sequence must be ascending sorted to apply interpolation search') + sys.exit("Sequence must be ascending sorted to apply interpolation search") target = 67 result = interpolation_search(collection, target) if result is not None: - print('{} found at positions: {}'.format(target, result)) + print("{} found at positions: {}".format(target, result)) else: - print('Not found') + print("Not found") diff --git a/searches/jump_search.py b/searches/jump_search.py index 78d9f79dc6a8..e191cf2d4b27 100644 --- a/searches/jump_search.py +++ b/searches/jump_search.py @@ -1,9 +1,11 @@ import math + + def jump_search(arr, x): n = len(arr) step = int(math.floor(math.sqrt(n))) prev = 0 - while arr[min(step, n)-1] < x: + while arr[min(step, n) - 1] < x: prev = step step += int(math.floor(math.sqrt(n))) if prev >= n: @@ -18,8 +20,7 @@ def jump_search(arr, x): return -1 - -arr = [ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610] +arr = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610] x = 55 index = jump_search(arr, x) -print("\nNumber " + str(x) +" is at index " + str(index)); +print("\nNumber " + str(x) + " is at index " + str(index)) diff --git a/searches/linear_search.py b/searches/linear_search.py index fb784924132e..ab20f3527bb3 100644 --- a/searches/linear_search.py +++ b/searches/linear_search.py @@ -37,14 +37,14 @@ def linear_search(sequence, target): return None -if __name__ == '__main__': - user_input = input('Enter numbers separated by comma:\n').strip() - sequence = [int(item) for item in user_input.split(',')] +if __name__ == "__main__": + user_input = input("Enter numbers separated by comma:\n").strip() + sequence = [int(item) for item in user_input.split(",")] - target_input = input('Enter a single number to be found in the list:\n') + target_input = input("Enter a single number to be found in the list:\n") target = int(target_input) result = linear_search(sequence, target) if result is not None: - print('{} found at positions: {}'.format(target, result)) + print("{} found at positions: {}".format(target, result)) else: - print('Not found') + print("Not found") diff --git a/searches/quick_select.py b/searches/quick_select.py index 76d09cb97f97..6b70562bd78f 100644 --- a/searches/quick_select.py +++ b/searches/quick_select.py @@ -4,6 +4,8 @@ A python implementation of the quick select algorithm, which is efficient for calculating the value that would appear in the index of a list if it would be sorted, even if it is not already sorted https://en.wikipedia.org/wiki/Quickselect """ + + def _partition(data, pivot): """ Three way partition the data into smaller, equal and greater lists, @@ -21,29 +23,30 @@ def _partition(data, pivot): else: equal.append(element) return less, equal, greater - + + def quickSelect(list, k): - #k = len(list) // 2 when trying to find the median (index that value would be when list is sorted) - - #invalid input - if k>=len(list) or k<0: + # k = len(list) // 2 when trying to find the median (index that value would be when list is sorted) + + # invalid input + if k >= len(list) or k < 0: return None - + smaller = [] larger = [] pivot = random.randint(0, len(list) - 1) pivot = list[pivot] count = 0 - smaller, equal, larger =_partition(list, pivot) + smaller, equal, larger = _partition(list, pivot) count = len(equal) m = len(smaller) - #k is the pivot + # k is the pivot if m <= k < m + count: return pivot # must be in smaller elif m > k: return quickSelect(smaller, k) - #must be in larger + # must be in larger else: - return quickSelect(larger, k - (m + count)) \ No newline at end of file + return quickSelect(larger, k - (m + count)) diff --git a/searches/sentinel_linear_search.py b/searches/sentinel_linear_search.py index eb9d32e5f503..6c4da9b21189 100644 --- a/searches/sentinel_linear_search.py +++ b/searches/sentinel_linear_search.py @@ -10,6 +10,7 @@ python sentinel_linear_search.py """ + def sentinel_linear_search(sequence, target): """Pure implementation of sentinel linear search algorithm in Python @@ -44,14 +45,14 @@ def sentinel_linear_search(sequence, target): return index -if __name__ == '__main__': - user_input = input('Enter numbers separated by comma:\n').strip() - sequence = [int(item) for item in user_input.split(',')] +if __name__ == "__main__": + user_input = input("Enter numbers separated by comma:\n").strip() + sequence = [int(item) for item in user_input.split(",")] - target_input = input('Enter a single number to be found in the list:\n') + target_input = input("Enter a single number to be found in the list:\n") target = int(target_input) result = sentinel_linear_search(sequence, target) if result is not None: - print('{} found at positions: {}'.format(target, result)) + print("{} found at positions: {}".format(target, result)) else: - print('Not found') \ No newline at end of file + print("Not found") diff --git a/searches/tabu_search.py b/searches/tabu_search.py index ffd84f8ac031..9a1478244503 100644 --- a/searches/tabu_search.py +++ b/searches/tabu_search.py @@ -55,13 +55,17 @@ def generate_neighbours(path): _list.append([line.split()[1], line.split()[2]]) dict_of_neighbours[line.split()[0]] = _list else: - dict_of_neighbours[line.split()[0]].append([line.split()[1], line.split()[2]]) + dict_of_neighbours[line.split()[0]].append( + [line.split()[1], line.split()[2]] + ) if line.split()[1] not in dict_of_neighbours: _list = list() _list.append([line.split()[0], line.split()[2]]) dict_of_neighbours[line.split()[1]] = _list else: - dict_of_neighbours[line.split()[1]].append([line.split()[0], line.split()[2]]) + dict_of_neighbours[line.split()[1]].append( + [line.split()[0], line.split()[2]] + ) return dict_of_neighbours @@ -111,8 +115,11 @@ def generate_first_solution(path, dict_of_neighbours): break position += 1 - distance_of_first_solution = distance_of_first_solution + int( - dict_of_neighbours[first_solution[-2]][position][1]) - 10000 + distance_of_first_solution = ( + distance_of_first_solution + + int(dict_of_neighbours[first_solution[-2]][position][1]) + - 10000 + ) return first_solution, distance_of_first_solution @@ -167,7 +174,9 @@ def find_neighborhood(solution, dict_of_neighbours): return neighborhood_of_solution -def tabu_search(first_solution, distance_of_first_solution, dict_of_neighbours, iters, size): +def tabu_search( + first_solution, distance_of_first_solution, dict_of_neighbours, iters, size +): """ Pure implementation of Tabu search algorithm for a Travelling Salesman Problem in Python. @@ -207,8 +216,10 @@ def tabu_search(first_solution, distance_of_first_solution, dict_of_neighbours, break i = i + 1 - if [first_exchange_node, second_exchange_node] not in tabu_list and [second_exchange_node, - first_exchange_node] not in tabu_list: + if [first_exchange_node, second_exchange_node] not in tabu_list and [ + second_exchange_node, + first_exchange_node, + ] not in tabu_list: tabu_list.append([first_exchange_node, second_exchange_node]) found = True solution = best_solution[:-1] @@ -231,10 +242,17 @@ def tabu_search(first_solution, distance_of_first_solution, dict_of_neighbours, def main(args=None): dict_of_neighbours = generate_neighbours(args.File) - first_solution, distance_of_first_solution = generate_first_solution(args.File, dict_of_neighbours) + first_solution, distance_of_first_solution = generate_first_solution( + args.File, dict_of_neighbours + ) - best_sol, best_cost = tabu_search(first_solution, distance_of_first_solution, dict_of_neighbours, args.Iterations, - args.Size) + best_sol, best_cost = tabu_search( + first_solution, + distance_of_first_solution, + dict_of_neighbours, + args.Iterations, + args.Size, + ) print("Best solution: {0}, with total distance: {1}.".format(best_sol, best_cost)) @@ -242,11 +260,22 @@ def main(args=None): if __name__ == "__main__": parser = argparse.ArgumentParser(description="Tabu Search") parser.add_argument( - "-f", "--File", type=str, help="Path to the file containing the data", required=True) + "-f", + "--File", + type=str, + help="Path to the file containing the data", + required=True, + ) parser.add_argument( - "-i", "--Iterations", type=int, help="How many iterations the algorithm should perform", required=True) + "-i", + "--Iterations", + type=int, + help="How many iterations the algorithm should perform", + required=True, + ) parser.add_argument( - "-s", "--Size", type=int, help="Size of the tabu list", required=True) + "-s", "--Size", type=int, help="Size of the tabu list", required=True + ) # Pass the arguments to main method sys.exit(main(parser.parse_args())) diff --git a/searches/ternary_search.py b/searches/ternary_search.py index 41033f33cec6..43407b7e5538 100644 --- a/searches/ternary_search.py +++ b/searches/ternary_search.py @@ -1,11 +1,11 @@ -''' +""" This is a type of divide and conquer algorithm which divides the search space into 3 parts and finds the target value based on the property of the array or list (usually monotonic property). Time Complexity : O(log3 N) Space Complexity : O(1) -''' +""" import sys # This is the precision for this function which can be altered. @@ -14,87 +14,90 @@ # This is the linear search that will occur after the search space has become smaller. def lin_search(left, right, A, target): - for i in range(left, right+1): - if(A[i] == target): + for i in range(left, right + 1): + if A[i] == target: return i + # This is the iterative method of the ternary search algorithm. def ite_ternary_search(A, target): left = 0 - right = len(A) - 1; - while(True): - if(left a[j]) agrees with the direction, -# then a[i] and a[j] are interchanged.*/ +# The parameter dir indicates the sorting direction, ASCENDING +# or DESCENDING; if (a[i] > a[j]) agrees with the direction, +# then a[i] and a[j] are interchanged.*/ def compAndSwap(a, i, j, dire): if (dire == 1 and a[i] > a[j]) or (dire == 0 and a[i] < a[j]): a[i], a[j] = a[j], a[i] @@ -12,8 +12,8 @@ def compAndSwap(a, i, j, dire): # if dir = 1, and in descending order otherwise (means dir=0). -# The sequence to be sorted starts at index position low, -# the parameter cnt is the number of elements to be sorted. +# The sequence to be sorted starts at index position low, +# the parameter cnt is the number of elements to be sorted. def bitonicMerge(a, low, cnt, dire): if cnt > 1: k = int(cnt / 2) @@ -26,7 +26,7 @@ def bitonicMerge(a, low, cnt, dire): # sorting its two halves in opposite sorting orders, and then -# calls bitonicMerge to make them in the same order +# calls bitonicMerge to make them in the same order def bitonicSort(a, low, cnt, dire): if cnt > 1: k = int(cnt / 2) diff --git a/sorts/bogo_sort.py b/sorts/bogo_sort.py index a3b2cbc1aa29..0afa444e5b8e 100644 --- a/sorts/bogo_sort.py +++ b/sorts/bogo_sort.py @@ -37,7 +37,8 @@ def isSorted(collection): random.shuffle(collection) return collection -if __name__ == '__main__': - user_input = input('Enter numbers separated by a comma:\n').strip() - unsorted = [int(item) for item in user_input.split(',')] + +if __name__ == "__main__": + user_input = input("Enter numbers separated by a comma:\n").strip() + unsorted = [int(item) for item in user_input.split(",")] print(bogo_sort(unsorted)) diff --git a/sorts/bubble_sort.py b/sorts/bubble_sort.py index c41a51ea3cbf..ccd8a2e11ee1 100644 --- a/sorts/bubble_sort.py +++ b/sorts/bubble_sort.py @@ -22,18 +22,18 @@ def bubble_sort(collection): True """ length = len(collection) - for i in range(length-1): + for i in range(length - 1): swapped = False - for j in range(length-1-i): - if collection[j] > collection[j+1]: + for j in range(length - 1 - i): + if collection[j] > collection[j + 1]: swapped = True - collection[j], collection[j+1] = collection[j+1], collection[j] + collection[j], collection[j + 1] = collection[j + 1], collection[j] if not swapped: break # Stop iteration if the collection is sorted. return collection -if __name__ == '__main__': - user_input = input('Enter numbers separated by a comma:').strip() - unsorted = [int(item) for item in user_input.split(',')] - print(*bubble_sort(unsorted), sep=',') +if __name__ == "__main__": + user_input = input("Enter numbers separated by a comma:").strip() + unsorted = [int(item) for item in user_input.split(",")] + print(*bubble_sort(unsorted), sep=",") diff --git a/sorts/bucket_sort.py b/sorts/bucket_sort.py index 0678b1194657..217ee5893c4b 100644 --- a/sorts/bucket_sort.py +++ b/sorts/bucket_sort.py @@ -17,8 +17,8 @@ # number of buckets. # Time Complexity of Solution: -# Worst case scenario occurs when all the elements are placed in a single bucket. The overall performance -# would then be dominated by the algorithm used to sort each bucket. In this case, O(n log n), because of TimSort +# Worst case scenario occurs when all the elements are placed in a single bucket. The overall performance +# would then be dominated by the algorithm used to sort each bucket. In this case, O(n log n), because of TimSort # # Average Case O(n + (n^2)/k + k), where k is the number of buckets # @@ -32,18 +32,18 @@ def bucket_sort(my_list, bucket_size=DEFAULT_BUCKET_SIZE): raise Exception("Please add some elements in the array.") min_value, max_value = (min(my_list), max(my_list)) - bucket_count = ((max_value - min_value) // bucket_size + 1) + bucket_count = (max_value - min_value) // bucket_size + 1 buckets = [[] for _ in range(int(bucket_count))] for i in range(len(my_list)): - buckets[int((my_list[i] - min_value) // bucket_size) - ].append(my_list[i]) + buckets[int((my_list[i] - min_value) // bucket_size)].append(my_list[i]) - return sorted([buckets[i][j] for i in range(len(buckets)) - for j in range(len(buckets[i]))]) + return sorted( + [buckets[i][j] for i in range(len(buckets)) for j in range(len(buckets[i]))] + ) if __name__ == "__main__": - user_input = input('Enter numbers separated by a comma:').strip() - unsorted = [float(n) for n in user_input.split(',') if len(user_input) > 0] + user_input = input("Enter numbers separated by a comma:").strip() + unsorted = [float(n) for n in user_input.split(",") if len(user_input) > 0] print(bucket_sort(unsorted)) diff --git a/sorts/cocktail_shaker_sort.py b/sorts/cocktail_shaker_sort.py index d486e6a11dfa..ab624421a3d6 100644 --- a/sorts/cocktail_shaker_sort.py +++ b/sorts/cocktail_shaker_sort.py @@ -2,24 +2,25 @@ def cocktail_shaker_sort(unsorted): """ Pure implementation of the cocktail shaker sort algorithm in Python. """ - for i in range(len(unsorted)-1, 0, -1): + for i in range(len(unsorted) - 1, 0, -1): swapped = False for j in range(i, 0, -1): - if unsorted[j] < unsorted[j-1]: - unsorted[j], unsorted[j-1] = unsorted[j-1], unsorted[j] + if unsorted[j] < unsorted[j - 1]: + unsorted[j], unsorted[j - 1] = unsorted[j - 1], unsorted[j] swapped = True for j in range(i): - if unsorted[j] > unsorted[j+1]: - unsorted[j], unsorted[j+1] = unsorted[j+1], unsorted[j] + if unsorted[j] > unsorted[j + 1]: + unsorted[j], unsorted[j + 1] = unsorted[j + 1], unsorted[j] swapped = True if not swapped: return unsorted -if __name__ == '__main__': - user_input = input('Enter numbers separated by a comma:\n').strip() - unsorted = [int(item) for item in user_input.split(',')] + +if __name__ == "__main__": + user_input = input("Enter numbers separated by a comma:\n").strip() + unsorted = [int(item) for item in user_input.split(",")] cocktail_shaker_sort(unsorted) print(unsorted) diff --git a/sorts/comb_sort.py b/sorts/comb_sort.py index 6ce6c1c094f9..3c4c57483e3f 100644 --- a/sorts/comb_sort.py +++ b/sorts/comb_sort.py @@ -12,6 +12,7 @@ python comb_sort.py """ + def comb_sort(data): """Pure implementation of comb sort algorithm in Python :param collection: some mutable ordered collection with heterogeneous @@ -38,16 +39,16 @@ def comb_sort(data): i = 0 while gap + i < len(data): - if data[i] > data[i+gap]: + if data[i] > data[i + gap]: # Swap values - data[i], data[i+gap] = data[i+gap], data[i] + data[i], data[i + gap] = data[i + gap], data[i] swapped = True i += 1 return data -if __name__ == '__main__': - user_input = input('Enter numbers separated by a comma:\n').strip() - unsorted = [int(item) for item in user_input.split(',')] +if __name__ == "__main__": + user_input = input("Enter numbers separated by a comma:\n").strip() + unsorted = [int(item) for item in user_input.split(",")] print(comb_sort(unsorted)) diff --git a/sorts/counting_sort.py b/sorts/counting_sort.py index a3de1811849e..b672d4af47cb 100644 --- a/sorts/counting_sort.py +++ b/sorts/counting_sort.py @@ -42,7 +42,7 @@ def counting_sort(collection): # sum each position with it's predecessors. now, counting_arr[i] tells # us how many elements <= i has in the collection for i in range(1, counting_arr_length): - counting_arr[i] = counting_arr[i] + counting_arr[i-1] + counting_arr[i] = counting_arr[i] + counting_arr[i - 1] # create the output collection ordered = [0] * coll_len @@ -50,23 +50,24 @@ def counting_sort(collection): # place the elements in the output, respecting the original order (stable # sort) from end to begin, updating counting_arr for i in reversed(range(0, coll_len)): - ordered[counting_arr[collection[i] - coll_min]-1] = collection[i] + ordered[counting_arr[collection[i] - coll_min] - 1] = collection[i] counting_arr[collection[i] - coll_min] -= 1 return ordered + def counting_sort_string(string): """ >>> counting_sort_string("thisisthestring") 'eghhiiinrsssttt' """ - return ''.join([chr(i) for i in counting_sort([ord(c) for c in string])]) + return "".join([chr(i) for i in counting_sort([ord(c) for c in string])]) -if __name__ == '__main__': +if __name__ == "__main__": # Test string sort assert "eghhiiinrsssttt" == counting_sort_string("thisisthestring") - user_input = input('Enter numbers separated by a comma:\n').strip() - unsorted = [int(item) for item in user_input.split(',')] + user_input = input("Enter numbers separated by a comma:\n").strip() + unsorted = [int(item) for item in user_input.split(",")] print(counting_sort(unsorted)) diff --git a/sorts/cycle_sort.py b/sorts/cycle_sort.py index 06a377cbd906..4ce6a2a0e757 100644 --- a/sorts/cycle_sort.py +++ b/sorts/cycle_sort.py @@ -41,12 +41,12 @@ def cycle_sort(array): # Main Code starts here -if __name__ == '__main__': - user_input = input('Enter numbers separated by a comma:\n') - unsorted = [int(item) for item in user_input.split(',')] +if __name__ == "__main__": + user_input = input("Enter numbers separated by a comma:\n") + unsorted = [int(item) for item in user_input.split(",")] n = len(unsorted) cycle_sort(unsorted) print("After sort : ") for i in range(0, n): - print(unsorted[i], end=' ') + print(unsorted[i], end=" ") diff --git a/sorts/external_sort.py b/sorts/external_sort.py index 1638e9efafee..abdcb29f95b2 100644 --- a/sorts/external_sort.py +++ b/sorts/external_sort.py @@ -6,8 +6,9 @@ import os import argparse + class FileSplitter(object): - BLOCK_FILENAME_FORMAT = 'block_{0}.dat' + BLOCK_FILENAME_FORMAT = "block_{0}.dat" def __init__(self, filename): self.filename = filename @@ -15,7 +16,7 @@ def __init__(self, filename): def write_block(self, data, block_number): filename = self.BLOCK_FILENAME_FORMAT.format(block_number) - with open(filename, 'w') as file: + with open(filename, "w") as file: file.write(data) self.block_filenames.append(filename) @@ -36,7 +37,7 @@ def split(self, block_size, sort_key=None): else: lines.sort(key=sort_key) - self.write_block(''.join(lines), i) + self.write_block("".join(lines), i) i += 1 def cleanup(self): @@ -63,14 +64,16 @@ def __init__(self, files): self.buffers = {i: None for i in range(self.num_buffers)} def get_dict(self): - return {i: self.buffers[i] for i in range(self.num_buffers) if i not in self.empty} + return { + i: self.buffers[i] for i in range(self.num_buffers) if i not in self.empty + } def refresh(self): for i in range(self.num_buffers): if self.buffers[i] is None and i not in self.empty: self.buffers[i] = self.files[i].readline() - if self.buffers[i] == '': + if self.buffers[i] == "": self.empty.add(i) self.files[i].close() @@ -92,7 +95,7 @@ def __init__(self, merge_strategy): def merge(self, filenames, outfilename, buffer_size): buffers = FilesArray(self.get_file_handles(filenames, buffer_size)) - with open(outfilename, 'w', buffer_size) as outfile: + with open(outfilename, "w", buffer_size) as outfile: while buffers.refresh(): min_index = self.merge_strategy.select(buffers.get_dict()) outfile.write(buffers.unshift(min_index)) @@ -101,12 +104,11 @@ def get_file_handles(self, filenames, buffer_size): files = {} for i in range(len(filenames)): - files[i] = open(filenames[i], 'r', buffer_size) + files[i] = open(filenames[i], "r", buffer_size) return files - class ExternalSort(object): def __init__(self, block_size): self.block_size = block_size @@ -118,7 +120,7 @@ def sort(self, filename, sort_key=None): merger = FileMerger(NWayMerge()) buffer_size = self.block_size / (num_blocks + 1) - merger.merge(splitter.get_block_filenames(), filename + '.out', buffer_size) + merger.merge(splitter.get_block_filenames(), filename + ".out", buffer_size) splitter.cleanup() @@ -127,32 +129,29 @@ def get_number_blocks(self, filename, block_size): def parse_memory(string): - if string[-1].lower() == 'k': + if string[-1].lower() == "k": return int(string[:-1]) * 1024 - elif string[-1].lower() == 'm': + elif string[-1].lower() == "m": return int(string[:-1]) * 1024 * 1024 - elif string[-1].lower() == 'g': + elif string[-1].lower() == "g": return int(string[:-1]) * 1024 * 1024 * 1024 else: return int(string) - def main(): parser = argparse.ArgumentParser() - parser.add_argument('-m', - '--mem', - help='amount of memory to use for sorting', - default='100M') - parser.add_argument('filename', - metavar='', - nargs=1, - help='name of file to sort') + parser.add_argument( + "-m", "--mem", help="amount of memory to use for sorting", default="100M" + ) + parser.add_argument( + "filename", metavar="", nargs=1, help="name of file to sort" + ) args = parser.parse_args() sorter = ExternalSort(parse_memory(args.mem)) sorter.sort(args.filename[0]) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/sorts/gnome_sort.py b/sorts/gnome_sort.py index fed70eb6bc1b..58a44c94da43 100644 --- a/sorts/gnome_sort.py +++ b/sorts/gnome_sort.py @@ -14,12 +14,12 @@ def gnome_sort(unsorted): else: unsorted[i - 1], unsorted[i] = unsorted[i], unsorted[i - 1] i -= 1 - if (i == 0): + if i == 0: i = 1 -if __name__ == '__main__': - user_input = input('Enter numbers separated by a comma:\n').strip() - unsorted = [int(item) for item in user_input.split(',')] +if __name__ == "__main__": + user_input = input("Enter numbers separated by a comma:\n").strip() + unsorted = [int(item) for item in user_input.split(",")] gnome_sort(unsorted) print(unsorted) diff --git a/sorts/heap_sort.py b/sorts/heap_sort.py index ca4a061afbb7..a39ae2b88da2 100644 --- a/sorts/heap_sort.py +++ b/sorts/heap_sort.py @@ -1,4 +1,4 @@ -''' +""" This is a pure python implementation of the heap sort algorithm. For doctests run following command: @@ -8,7 +8,8 @@ For manual testing run: python heap_sort.py -''' +""" + def heapify(unsorted, index, heap_size): largest = index @@ -26,7 +27,7 @@ def heapify(unsorted, index, heap_size): def heap_sort(unsorted): - ''' + """ Pure implementation of the heap sort algorithm in Python :param collection: some mutable ordered collection with heterogeneous comparable items inside @@ -41,7 +42,7 @@ def heap_sort(unsorted): >>> heap_sort([-2, -5, -45]) [-45, -5, -2] - ''' + """ n = len(unsorted) for i in range(n // 2 - 1, -1, -1): heapify(unsorted, i, n) @@ -50,7 +51,8 @@ def heap_sort(unsorted): heapify(unsorted, 0, i) return unsorted -if __name__ == '__main__': - user_input = input('Enter numbers separated by a comma:\n').strip() - unsorted = [int(item) for item in user_input.split(',')] + +if __name__ == "__main__": + user_input = input("Enter numbers separated by a comma:\n").strip() + unsorted = [int(item) for item in user_input.split(",")] print(heap_sort(unsorted)) diff --git a/sorts/insertion_sort.py b/sorts/insertion_sort.py index e10497b0e282..b767018c3d57 100644 --- a/sorts/insertion_sort.py +++ b/sorts/insertion_sort.py @@ -9,6 +9,8 @@ For manual testing run: python insertion_sort.py """ + + def insertion_sort(collection): """Pure implementation of the insertion sort algorithm in Python @@ -29,14 +31,20 @@ def insertion_sort(collection): for loop_index in range(1, len(collection)): insertion_index = loop_index - while insertion_index > 0 and collection[insertion_index - 1] > collection[insertion_index]: - collection[insertion_index], collection[insertion_index - 1] = collection[insertion_index - 1], collection[insertion_index] + while ( + insertion_index > 0 + and collection[insertion_index - 1] > collection[insertion_index] + ): + collection[insertion_index], collection[insertion_index - 1] = ( + collection[insertion_index - 1], + collection[insertion_index], + ) insertion_index -= 1 return collection -if __name__ == '__main__': - user_input = input('Enter numbers separated by a comma:\n').strip() - unsorted = [int(item) for item in user_input.split(',')] +if __name__ == "__main__": + user_input = input("Enter numbers separated by a comma:\n").strip() + unsorted = [int(item) for item in user_input.split(",")] print(insertion_sort(unsorted)) diff --git a/sorts/merge_sort.py b/sorts/merge_sort.py index e64e90785a32..13f1144d4ad3 100644 --- a/sorts/merge_sort.py +++ b/sorts/merge_sort.py @@ -9,6 +9,8 @@ For manual testing run: python merge_sort.py """ + + def merge_sort(collection): """Pure implementation of the merge sort algorithm in Python @@ -26,23 +28,25 @@ def merge_sort(collection): >>> merge_sort([-2, -5, -45]) [-45, -5, -2] """ + def merge(left, right): - '''merge left and right + """merge left and right :param left: left collection :param right: right collection :return: merge result - ''' + """ result = [] while left and right: result.append((left if left[0] <= right[0] else right).pop(0)) return result + left + right + if len(collection) <= 1: return collection mid = len(collection) // 2 return merge(merge_sort(collection[:mid]), merge_sort(collection[mid:])) -if __name__ == '__main__': - user_input = input('Enter numbers separated by a comma:\n').strip() - unsorted = [int(item) for item in user_input.split(',')] - print(*merge_sort(unsorted), sep=',') +if __name__ == "__main__": + user_input = input("Enter numbers separated by a comma:\n").strip() + unsorted = [int(item) for item in user_input.split(",")] + print(*merge_sort(unsorted), sep=",") diff --git a/sorts/merge_sort_fastest.py b/sorts/merge_sort_fastest.py index 3c9ed3e9e8ee..f3c067795dd5 100644 --- a/sorts/merge_sort_fastest.py +++ b/sorts/merge_sort_fastest.py @@ -1,9 +1,11 @@ -''' +""" Python implementation of the fastest merge sort algorithm. Takes an average of 0.6 microseconds to sort a list of length 1000 items. Best Case Scenario : O(n) Worst Case Scenario : O(n^2) because native python functions:min, max and remove are already O(n) -''' +""" + + def merge_sort(collection): """Pure implementation of the fastest merge sort algorithm in Python @@ -32,7 +34,7 @@ def merge_sort(collection): return start + collection + end -if __name__ == '__main__': - user_input = input('Enter numbers separated by a comma:\n').strip() - unsorted = [int(item) for item in user_input.split(',')] - print(*merge_sort(unsorted), sep=',') +if __name__ == "__main__": + user_input = input("Enter numbers separated by a comma:\n").strip() + unsorted = [int(item) for item in user_input.split(",")] + print(*merge_sort(unsorted), sep=",") diff --git a/sorts/odd_even_transposition_parallel.py b/sorts/odd_even_transposition_parallel.py index 9bf81a39e27a..4d2f377024d2 100644 --- a/sorts/odd_even_transposition_parallel.py +++ b/sorts/odd_even_transposition_parallel.py @@ -12,7 +12,7 @@ """ from multiprocessing import Process, Pipe, Lock -#lock used to ensure that two processes do not access a pipe at the same time +# lock used to ensure that two processes do not access a pipe at the same time processLock = Lock() """ @@ -25,89 +25,117 @@ LRcv, RRcv = the pipes we use to receive from our left and right neighbors resultPipe = the pipe used to send results back to main """ + + def oeProcess(position, value, LSend, RSend, LRcv, RRcv, resultPipe): global processLock - #we perform n swaps since after n swaps we know we are sorted - #we *could* stop early if we are sorted already, but it takes as long to - #find out we are sorted as it does to sort the list with this algorithm + # we perform n swaps since after n swaps we know we are sorted + # we *could* stop early if we are sorted already, but it takes as long to + # find out we are sorted as it does to sort the list with this algorithm for i in range(0, 10): - if( (i + position) % 2 == 0 and RSend != None): - #send your value to your right neighbor + if (i + position) % 2 == 0 and RSend != None: + # send your value to your right neighbor processLock.acquire() RSend[1].send(value) processLock.release() - #receive your right neighbor's value + # receive your right neighbor's value processLock.acquire() temp = RRcv[0].recv() processLock.release() - #take the lower value since you are on the left + # take the lower value since you are on the left value = min(value, temp) - elif( (i + position) % 2 != 0 and LSend != None): - #send your value to your left neighbor + elif (i + position) % 2 != 0 and LSend != None: + # send your value to your left neighbor processLock.acquire() LSend[1].send(value) processLock.release() - #receive your left neighbor's value + # receive your left neighbor's value processLock.acquire() temp = LRcv[0].recv() processLock.release() - #take the higher value since you are on the right + # take the higher value since you are on the right value = max(value, temp) - #after all swaps are performed, send the values back to main + # after all swaps are performed, send the values back to main resultPipe[1].send(value) + """ the function which creates the processes that perform the parallel swaps arr = the list to be sorted """ + + def OddEvenTransposition(arr): processArray = [] resultPipe = [] - #initialize the list of pipes where the values will be retrieved + # initialize the list of pipes where the values will be retrieved for _ in arr: resultPipe.append(Pipe()) - #creates the processes - #the first and last process only have one neighbor so they are made outside - #of the loop + # creates the processes + # the first and last process only have one neighbor so they are made outside + # of the loop tempRs = Pipe() tempRr = Pipe() - processArray.append(Process(target = oeProcess, args = (0, arr[0], None, tempRs, None, tempRr, resultPipe[0]))) + processArray.append( + Process( + target=oeProcess, + args=(0, arr[0], None, tempRs, None, tempRr, resultPipe[0]), + ) + ) tempLr = tempRs tempLs = tempRr for i in range(1, len(arr) - 1): tempRs = Pipe() tempRr = Pipe() - processArray.append(Process(target = oeProcess, args = (i, arr[i], tempLs, tempRs, tempLr, tempRr, resultPipe[i]))) + processArray.append( + Process( + target=oeProcess, + args=(i, arr[i], tempLs, tempRs, tempLr, tempRr, resultPipe[i]), + ) + ) tempLr = tempRs tempLs = tempRr - processArray.append(Process(target = oeProcess, args = (len(arr) - 1, arr[len(arr) - 1], tempLs, None, tempLr, None, resultPipe[len(arr) - 1]))) - - #start the processes + processArray.append( + Process( + target=oeProcess, + args=( + len(arr) - 1, + arr[len(arr) - 1], + tempLs, + None, + tempLr, + None, + resultPipe[len(arr) - 1], + ), + ) + ) + + # start the processes for p in processArray: p.start() - #wait for the processes to end and write their values to the list + # wait for the processes to end and write their values to the list for p in range(0, len(resultPipe)): arr[p] = resultPipe[p][0].recv() processArray[p].join() - return(arr) + return arr -#creates a reverse sorted list and sorts it +# creates a reverse sorted list and sorts it def main(): arr = [] @@ -121,5 +149,6 @@ def main(): print("Sorted List\n") print(*arr) + if __name__ == "__main__": main() diff --git a/sorts/odd_even_transposition_single_threaded.py b/sorts/odd_even_transposition_single_threaded.py index ec5f3cf14e55..ec045d9dd08d 100644 --- a/sorts/odd_even_transposition_single_threaded.py +++ b/sorts/odd_even_transposition_single_threaded.py @@ -5,6 +5,7 @@ is no better than bubble sort. """ + def OddEvenTransposition(arr): for i in range(0, len(arr)): for i in range(i % 2, len(arr) - 1, 2): @@ -14,7 +15,8 @@ def OddEvenTransposition(arr): return arr -#creates a list and sorts it + +# creates a list and sorts it def main(): list = [] @@ -28,5 +30,6 @@ def main(): print("Sorted List\n") print(*list) + if __name__ == "__main__": main() diff --git a/sorts/pancake_sort.py b/sorts/pancake_sort.py index 873c14a0a174..ee54e57f9e0f 100644 --- a/sorts/pancake_sort.py +++ b/sorts/pancake_sort.py @@ -8,6 +8,7 @@ python pancake_sort.py """ + def pancake_sort(arr): """Sort Array with Pancake Sort. :param arr: Collection containing comparable items @@ -25,14 +26,14 @@ def pancake_sort(arr): # Find the maximum number in arr mi = arr.index(max(arr[0:cur])) # Reverse from 0 to mi - arr = arr[mi::-1] + arr[mi + 1:len(arr)] + arr = arr[mi::-1] + arr[mi + 1 : len(arr)] # Reverse whole list - arr = arr[cur - 1::-1] + arr[cur:len(arr)] + arr = arr[cur - 1 :: -1] + arr[cur : len(arr)] cur -= 1 return arr -if __name__ == '__main__': - user_input = input('Enter numbers separated by a comma:\n').strip() - unsorted = [int(item) for item in user_input.split(',')] +if __name__ == "__main__": + user_input = input("Enter numbers separated by a comma:\n").strip() + unsorted = [int(item) for item in user_input.split(",")] print(pancake_sort(unsorted)) diff --git a/sorts/pigeon_sort.py b/sorts/pigeon_sort.py index 5417234d331b..cf900699bc8d 100644 --- a/sorts/pigeon_sort.py +++ b/sorts/pigeon_sort.py @@ -1,4 +1,4 @@ -''' +""" This is an implementation of Pigeon Hole Sort. For doctests run following command: @@ -8,7 +8,9 @@ For manual testing run: python pigeon_sort.py -''' +""" + + def pigeon_sort(array): """ Implementation of pigeon hole sort algorithm @@ -21,7 +23,7 @@ def pigeon_sort(array): >>> pigeon_sort([-2, -5, -45]) [-45, -5, -2] """ - if(len(array) == 0): + if len(array) == 0: return array # Manually finds the minimum and maximum of the array. @@ -29,26 +31,29 @@ def pigeon_sort(array): max = array[0] for i in range(len(array)): - if(array[i] < min): min = array[i] - elif(array[i] > max): max = array[i] + if array[i] < min: + min = array[i] + elif array[i] > max: + max = array[i] # Compute the variables - holes_range = max-min + 1 + holes_range = max - min + 1 holes = [0 for _ in range(holes_range)] holes_repeat = [0 for _ in range(holes_range)] # Make the sorting. for i in range(len(array)): index = array[i] - min - if(holes[index] != array[i]): + if holes[index] != array[i]: holes[index] = array[i] holes_repeat[index] += 1 - else: holes_repeat[index] += 1 + else: + holes_repeat[index] += 1 # Makes the array back by replacing the numbers. index = 0 for i in range(holes_range): - while(holes_repeat[i] > 0): + while holes_repeat[i] > 0: array[index] = holes[i] index += 1 holes_repeat[i] -= 1 @@ -56,7 +61,8 @@ def pigeon_sort(array): # Returns the sorted array. return array -if __name__ == '__main__': - user_input = input('Enter numbers separated by comma:\n') - unsorted = [int(x) for x in user_input.split(',')] + +if __name__ == "__main__": + user_input = input("Enter numbers separated by comma:\n") + unsorted = [int(x) for x in user_input.split(",")] print(pigeon_sort(unsorted)) diff --git a/sorts/quick_sort.py b/sorts/quick_sort.py index 60f8803cb79c..29e10206f720 100644 --- a/sorts/quick_sort.py +++ b/sorts/quick_sort.py @@ -9,6 +9,8 @@ For manual testing run: python quick_sort.py """ + + def quick_sort(collection): """Pure implementation of quick sort algorithm in Python @@ -43,7 +45,7 @@ def quick_sort(collection): return quick_sort(lesser) + [pivot] + quick_sort(greater) -if __name__ == '__main__': - user_input = input('Enter numbers separated by a comma:\n').strip() - unsorted = [ int(item) for item in user_input.split(',') ] - print( quick_sort(unsorted) ) +if __name__ == "__main__": + user_input = input("Enter numbers separated by a comma:\n").strip() + unsorted = [int(item) for item in user_input.split(",")] + print(quick_sort(unsorted)) diff --git a/sorts/quick_sort_3_partition.py b/sorts/quick_sort_3_partition.py index 9056b204740a..a25ac7def802 100644 --- a/sorts/quick_sort_3_partition.py +++ b/sorts/quick_sort_3_partition.py @@ -17,8 +17,9 @@ def quick_sort_3partition(sorting, left, right): quick_sort_3partition(sorting, left, a - 1) quick_sort_3partition(sorting, b + 1, right) -if __name__ == '__main__': - user_input = input('Enter numbers separated by a comma:\n').strip() - unsorted = [ int(item) for item in user_input.split(',') ] - quick_sort_3partition(unsorted,0,len(unsorted)-1) + +if __name__ == "__main__": + user_input = input("Enter numbers separated by a comma:\n").strip() + unsorted = [int(item) for item in user_input.split(",")] + quick_sort_3partition(unsorted, 0, len(unsorted) - 1) print(unsorted) diff --git a/sorts/radix_sort.py b/sorts/radix_sort.py index 8dfc66b17b23..2990247a0ac0 100644 --- a/sorts/radix_sort.py +++ b/sorts/radix_sort.py @@ -6,21 +6,21 @@ def radix_sort(lst): max_digit = max(lst) while placement < max_digit: - # declare and initialize buckets - buckets = [list() for _ in range( RADIX )] + # declare and initialize buckets + buckets = [list() for _ in range(RADIX)] - # split lst between lists - for i in lst: - tmp = int((i / placement) % RADIX) - buckets[tmp].append(i) + # split lst between lists + for i in lst: + tmp = int((i / placement) % RADIX) + buckets[tmp].append(i) - # empty lists into lst array - a = 0 - for b in range( RADIX ): - buck = buckets[b] - for i in buck: - lst[a] = i - a += 1 + # empty lists into lst array + a = 0 + for b in range(RADIX): + buck = buckets[b] + for i in buck: + lst[a] = i + a += 1 - # move to next - placement *= RADIX + # move to next + placement *= RADIX diff --git a/sorts/random_normal_distribution_quicksort.py b/sorts/random_normal_distribution_quicksort.py index 39c54c46e263..be3b90190407 100644 --- a/sorts/random_normal_distribution_quicksort.py +++ b/sorts/random_normal_distribution_quicksort.py @@ -3,62 +3,60 @@ import numpy as np -def _inPlaceQuickSort(A,start,end): +def _inPlaceQuickSort(A, start, end): count = 0 - if start>> print(arr) [1, 2, 3, 4, 5] """ - stooge(arr,0,len(arr)-1) + stooge(arr, 0, len(arr) - 1) - -def stooge(arr, i, h): +def stooge(arr, i, h): if i >= h: return - + # If first element is smaller than the last then swap them - if arr[i]>arr[h]: + if arr[i] > arr[h]: arr[i], arr[h] = arr[h], arr[i] - + # If there are more than 2 elements in the array - if h-i+1 > 2: - t = (int)((h-i+1)/3) - + if h - i + 1 > 2: + t = (int)((h - i + 1) / 3) + # Recursively sort first 2/3 elements - stooge(arr, i, (h-t)) - + stooge(arr, i, (h - t)) + # Recursively sort last 2/3 elements - stooge(arr, i+t, (h)) - - # Recursively sort first 2/3 elements - stooge(arr, i, (h-t)) + stooge(arr, i + t, (h)) - + # Recursively sort first 2/3 elements + stooge(arr, i, (h - t)) diff --git a/sorts/topological_sort.py b/sorts/topological_sort.py index 74e58899a9a0..e7a52f7c7714 100644 --- a/sorts/topological_sort.py +++ b/sorts/topological_sort.py @@ -5,8 +5,8 @@ # b c # / \ # d e -edges = {'a': ['c', 'b'], 'b': ['d', 'e'], 'c': [], 'd': [], 'e': []} -vertices = ['a', 'b', 'c', 'd', 'e'] +edges = {"a": ["c", "b"], "b": ["d", "e"], "c": [], "d": [], "e": []} +vertices = ["a", "b", "c", "d", "e"] def topological_sort(start, visited, sort): @@ -30,6 +30,6 @@ def topological_sort(start, visited, sort): return sort -if __name__ == '__main__': - sort = topological_sort('a', [], []) +if __name__ == "__main__": + sort = topological_sort("a", [], []) print(sort) diff --git a/sorts/tree_sort.py b/sorts/tree_sort.py index baa4fc1acc20..716170a94fd1 100644 --- a/sorts/tree_sort.py +++ b/sorts/tree_sort.py @@ -5,7 +5,7 @@ """ -class node(): +class node: # BST data structure def __init__(self, val): self.val = val @@ -49,5 +49,5 @@ def tree_sort(arr): return res -if __name__ == '__main__': +if __name__ == "__main__": print(tree_sort([10, 1, 3, 2, 9, 14, 13])) diff --git a/sorts/wiggle_sort.py b/sorts/wiggle_sort.py index 606feb4d3dd1..5e5220ffbf05 100644 --- a/sorts/wiggle_sort.py +++ b/sorts/wiggle_sort.py @@ -16,7 +16,7 @@ def wiggle_sort(nums): nums[i - 1], nums[i] = nums[i], nums[i - 1] -if __name__ == '__main__': +if __name__ == "__main__": print("Enter the array elements:\n") array = list(map(int, input().split())) print("The unsorted array is:\n") diff --git a/strings/boyer_moore_search.py b/strings/boyer_moore_search.py index 2d67043dc028..59ee76b860d3 100644 --- a/strings/boyer_moore_search.py +++ b/strings/boyer_moore_search.py @@ -20,12 +20,9 @@ class BoyerMooreSearch: - - def __init__(self, text, pattern): self.text, self.pattern = text, pattern self.textLen, self.patLen = len(text), len(pattern) - def match_in_pattern(self, char): """ finds the index of char in pattern in reverse order @@ -36,14 +33,13 @@ def match_in_pattern(self, char): Returns : i (int): index of char from last in pattern -1 (int): if char is not found in pattern - """ + """ - for i in range(self.patLen-1, -1, -1): + for i in range(self.patLen - 1, -1, -1): if char == self.pattern[i]: return i return -1 - def mismatch_in_text(self, currentPos): """ finds the index of mis-matched character in text when compared with pattern from last @@ -55,14 +51,13 @@ def mismatch_in_text(self, currentPos): -1 (int): if there is no mis-match between pattern and text block """ - for i in range(self.patLen-1, -1, -1): + for i in range(self.patLen - 1, -1, -1): if self.pattern[i] != self.text[currentPos + i]: return currentPos + i return -1 - def bad_character_heuristic(self): - # searches pattern in text and returns index positions + # searches pattern in text and returns index positions positions = [] for i in range(self.textLen - self.patLen + 1): mismatch_index = self.mismatch_in_text(i) @@ -70,12 +65,14 @@ def bad_character_heuristic(self): positions.append(i) else: match_index = self.match_in_pattern(self.text[mismatch_index]) - i = mismatch_index - match_index #shifting index lgtm [py/multiple-definition] + i = ( + mismatch_index - match_index + ) # shifting index lgtm [py/multiple-definition] return positions - + text = "ABAABA" -pattern = "AB" +pattern = "AB" bms = BoyerMooreSearch(text, pattern) positions = bms.bad_character_heuristic() @@ -84,5 +81,3 @@ def bad_character_heuristic(self): else: print("Pattern found in following positions: ") print(positions) - - diff --git a/strings/knuth_morris_pratt.py b/strings/knuth_morris_pratt.py index 4553944284be..c7e96887c387 100644 --- a/strings/knuth_morris_pratt.py +++ b/strings/knuth_morris_pratt.py @@ -46,14 +46,14 @@ def get_failure_array(pattern): if pattern[i] == pattern[j]: i += 1 elif i > 0: - i = failure[i-1] + i = failure[i - 1] continue j += 1 failure.append(i) return failure -if __name__ == '__main__': +if __name__ == "__main__": # Test 1) pattern = "abc1abc12" text1 = "alskfjaldsabc1abc1abc12k23adsfabcabc" diff --git a/strings/levenshtein_distance.py b/strings/levenshtein_distance.py index 78175576194b..9b8793544a99 100644 --- a/strings/levenshtein_distance.py +++ b/strings/levenshtein_distance.py @@ -64,10 +64,13 @@ def levenshtein_distance(first_word, second_word): return previous_row[-1] -if __name__ == '__main__': - first_word = input('Enter the first word:\n').strip() - second_word = input('Enter the second word:\n').strip() +if __name__ == "__main__": + first_word = input("Enter the first word:\n").strip() + second_word = input("Enter the second word:\n").strip() result = levenshtein_distance(first_word, second_word) - print('Levenshtein distance between {} and {} is {}'.format( - first_word, second_word, result)) + print( + "Levenshtein distance between {} and {} is {}".format( + first_word, second_word, result + ) + ) diff --git a/strings/manacher.py b/strings/manacher.py index e73e173b43e0..ef8a724d027d 100644 --- a/strings/manacher.py +++ b/strings/manacher.py @@ -1,10 +1,15 @@ # calculate palindromic length from center with incrementing difference -def palindromic_length( center, diff, string): - if center-diff == -1 or center+diff == len(string) or string[center-diff] != string[center+diff] : +def palindromic_length(center, diff, string): + if ( + center - diff == -1 + or center + diff == len(string) + or string[center - diff] != string[center + diff] + ): return 0 - return 1 + palindromic_length(center, diff+1, string) + return 1 + palindromic_length(center, diff + 1, string) -def palindromic_string( input_string ): + +def palindromic_string(input_string): """ Manacher’s algorithm which finds Longest Palindromic Substring in linear time. @@ -16,37 +21,36 @@ def palindromic_string( input_string ): 3. return output_string from center - max_length to center + max_length and remove all "|" """ max_length = 0 - + # if input_string is "aba" than new_input_string become "a|b|a" new_input_string = "" output_string = "" # append each character + "|" in new_string for range(0, length-1) - for i in input_string[:len(input_string)-1] : + for i in input_string[: len(input_string) - 1]: new_input_string += i + "|" - #append last character + # append last character new_input_string += input_string[-1] - # for each character in new_string find corresponding palindromic string - for i in range(len(new_input_string)) : + for i in range(len(new_input_string)): # get palindromic length from ith position length = palindromic_length(i, 1, new_input_string) # update max_length and start position - if max_length < length : + if max_length < length: max_length = length start = i - - #create that string - for i in new_input_string[start-max_length:start+max_length+1] : + + # create that string + for i in new_input_string[start - max_length : start + max_length + 1]: if i != "|": output_string += i - + return output_string -if __name__ == '__main__': +if __name__ == "__main__": n = input() print(palindromic_string(n)) diff --git a/strings/min_cost_string_conversion.py b/strings/min_cost_string_conversion.py index 95840c484ba7..abc9d2c65158 100644 --- a/strings/min_cost_string_conversion.py +++ b/strings/min_cost_string_conversion.py @@ -1,114 +1,118 @@ -''' +""" Algorithm for calculating the most cost-efficient sequence for converting one string into another. The only allowed operations are ---Copy character with cost cC ---Replace character with cost cR ---Delete character with cost cD ---Insert character with cost cI -''' +""" + + def compute_transform_tables(X, Y, cC, cR, cD, cI): - X = list(X) - Y = list(Y) - m = len(X) - n = len(Y) + X = list(X) + Y = list(Y) + m = len(X) + n = len(Y) + + costs = [[0 for _ in range(n + 1)] for _ in range(m + 1)] + ops = [[0 for _ in range(n + 1)] for _ in range(m + 1)] - costs = [[0 for _ in range(n+1)] for _ in range(m+1)] - ops = [[0 for _ in range(n+1)] for _ in range(m+1)] + for i in range(1, m + 1): + costs[i][0] = i * cD + ops[i][0] = "D%c" % X[i - 1] - for i in range(1, m+1): - costs[i][0] = i*cD - ops[i][0] = 'D%c' % X[i-1] + for i in range(1, n + 1): + costs[0][i] = i * cI + ops[0][i] = "I%c" % Y[i - 1] - for i in range(1, n+1): - costs[0][i] = i*cI - ops[0][i] = 'I%c' % Y[i-1] + for i in range(1, m + 1): + for j in range(1, n + 1): + if X[i - 1] == Y[j - 1]: + costs[i][j] = costs[i - 1][j - 1] + cC + ops[i][j] = "C%c" % X[i - 1] + else: + costs[i][j] = costs[i - 1][j - 1] + cR + ops[i][j] = "R%c" % X[i - 1] + str(Y[j - 1]) - for i in range(1, m+1): - for j in range(1, n+1): - if X[i-1] == Y[j-1]: - costs[i][j] = costs[i-1][j-1] + cC - ops[i][j] = 'C%c' % X[i-1] - else: - costs[i][j] = costs[i-1][j-1] + cR - ops[i][j] = 'R%c' % X[i-1] + str(Y[j-1]) + if costs[i - 1][j] + cD < costs[i][j]: + costs[i][j] = costs[i - 1][j] + cD + ops[i][j] = "D%c" % X[i - 1] - if costs[i-1][j] + cD < costs[i][j]: - costs[i][j] = costs[i-1][j] + cD - ops[i][j] = 'D%c' % X[i-1] + if costs[i][j - 1] + cI < costs[i][j]: + costs[i][j] = costs[i][j - 1] + cI + ops[i][j] = "I%c" % Y[j - 1] - if costs[i][j-1] + cI < costs[i][j]: - costs[i][j] = costs[i][j-1] + cI - ops[i][j] = 'I%c' % Y[j-1] + return costs, ops - return costs, ops def assemble_transformation(ops, i, j): - if i == 0 and j == 0: - seq = [] - return seq - else: - if ops[i][j][0] == 'C' or ops[i][j][0] == 'R': - seq = assemble_transformation(ops, i-1, j-1) - seq.append(ops[i][j]) - return seq - elif ops[i][j][0] == 'D': - seq = assemble_transformation(ops, i-1, j) - seq.append(ops[i][j]) - return seq - else: - seq = assemble_transformation(ops, i, j-1) - seq.append(ops[i][j]) - return seq - -if __name__ == '__main__': - _, operations = compute_transform_tables('Python', 'Algorithms', -1, 1, 2, 2) - - m = len(operations) - n = len(operations[0]) - sequence = assemble_transformation(operations, m-1, n-1) - - string = list('Python') - i = 0 - cost = 0 - - with open('min_cost.txt', 'w') as file: - for op in sequence: - print(''.join(string)) - - if op[0] == 'C': - file.write('%-16s' % 'Copy %c' % op[1]) - file.write('\t\t\t' + ''.join(string)) - file.write('\r\n') - - cost -= 1 - elif op[0] == 'R': - string[i] = op[2] - - file.write('%-16s' % ('Replace %c' % op[1] + ' with ' + str(op[2]))) - file.write('\t\t' + ''.join(string)) - file.write('\r\n') - - cost += 1 - elif op[0] == 'D': - string.pop(i) - - file.write('%-16s' % 'Delete %c' % op[1]) - file.write('\t\t\t' + ''.join(string)) - file.write('\r\n') - - cost += 2 - else: - string.insert(i, op[1]) - - file.write('%-16s' % 'Insert %c' % op[1]) - file.write('\t\t\t' + ''.join(string)) - file.write('\r\n') - - cost += 2 - - i += 1 - - print(''.join(string)) - print('Cost: ', cost) - - file.write('\r\nMinimum cost: ' + str(cost)) + if i == 0 and j == 0: + seq = [] + return seq + else: + if ops[i][j][0] == "C" or ops[i][j][0] == "R": + seq = assemble_transformation(ops, i - 1, j - 1) + seq.append(ops[i][j]) + return seq + elif ops[i][j][0] == "D": + seq = assemble_transformation(ops, i - 1, j) + seq.append(ops[i][j]) + return seq + else: + seq = assemble_transformation(ops, i, j - 1) + seq.append(ops[i][j]) + return seq + + +if __name__ == "__main__": + _, operations = compute_transform_tables("Python", "Algorithms", -1, 1, 2, 2) + + m = len(operations) + n = len(operations[0]) + sequence = assemble_transformation(operations, m - 1, n - 1) + + string = list("Python") + i = 0 + cost = 0 + + with open("min_cost.txt", "w") as file: + for op in sequence: + print("".join(string)) + + if op[0] == "C": + file.write("%-16s" % "Copy %c" % op[1]) + file.write("\t\t\t" + "".join(string)) + file.write("\r\n") + + cost -= 1 + elif op[0] == "R": + string[i] = op[2] + + file.write("%-16s" % ("Replace %c" % op[1] + " with " + str(op[2]))) + file.write("\t\t" + "".join(string)) + file.write("\r\n") + + cost += 1 + elif op[0] == "D": + string.pop(i) + + file.write("%-16s" % "Delete %c" % op[1]) + file.write("\t\t\t" + "".join(string)) + file.write("\r\n") + + cost += 2 + else: + string.insert(i, op[1]) + + file.write("%-16s" % "Insert %c" % op[1]) + file.write("\t\t\t" + "".join(string)) + file.write("\r\n") + + cost += 2 + + i += 1 + + print("".join(string)) + print("Cost: ", cost) + + file.write("\r\nMinimum cost: " + str(cost)) diff --git a/strings/naive_string_search.py b/strings/naive_string_search.py index 04c0d8157b24..a8c2ea584399 100644 --- a/strings/naive_string_search.py +++ b/strings/naive_string_search.py @@ -7,23 +7,26 @@ n=length of main string m=length of pattern string """ -def naivePatternSearch(mainString,pattern): - patLen=len(pattern) - strLen=len(mainString) - position=[] - for i in range(strLen-patLen+1): - match_found=True + + +def naivePatternSearch(mainString, pattern): + patLen = len(pattern) + strLen = len(mainString) + position = [] + for i in range(strLen - patLen + 1): + match_found = True for j in range(patLen): - if mainString[i+j]!=pattern[j]: - match_found=False + if mainString[i + j] != pattern[j]: + match_found = False break if match_found: position.append(i) return position -mainString="ABAAABCDBBABCDDEBCABC" -pattern="ABC" -position=naivePatternSearch(mainString,pattern) + +mainString = "ABAAABCDBBABCDDEBCABC" +pattern = "ABC" +position = naivePatternSearch(mainString, pattern) print("Pattern found in position ") for x in position: - print(x) \ No newline at end of file + print(x) diff --git a/traversals/binary_tree_traversals.py b/traversals/binary_tree_traversals.py index 389311a7cfde..31a73ae0c6a4 100644 --- a/traversals/binary_tree_traversals.py +++ b/traversals/binary_tree_traversals.py @@ -266,6 +266,7 @@ def prompt(s: str = "", width=50, char="*") -> str: if __name__ == "__main__": import doctest + doctest.testmod() print(prompt("Binary Tree Traversals"))