From e31c780d94acc0c3041f0b790dff3466262f4867 Mon Sep 17 00:00:00 2001 From: cclauss Date: Sat, 25 Nov 2017 12:41:55 +0100 Subject: [PATCH] Modernize Python 2 code to get ready for Python 3 --- ciphers/playfair_cipher.py | 2 +- data_structures/AVL/AVL.py | 1 + data_structures/Graph/BellmanFord.py | 2 ++ data_structures/Graph/BreadthFirstSearch.py | 2 ++ data_structures/Graph/DepthFirstSearch.py | 2 ++ data_structures/Graph/Dijkstra.py | 1 + data_structures/Graph/FloydWarshall.py | 11 ++++++----- data_structures/Graph/Graph_list.py | 3 +++ data_structures/Graph/Graph_matrix.py | 3 +++ data_structures/Graph/dijkstra_algorithm.py | 1 + data_structures/LinkedList/DoublyLinkedList.py | 5 ++++- dynamic_programming/edit_distance.py | 2 ++ dynamic_programming/fibonacci.py | 1 + machine_learning/k_means_clust.py | 3 ++- machine_learning/scoring_functions.py | 2 +- other/FindingPrimes.py | 3 +++ sorts/cyclesort.py | 3 +++ 17 files changed, 38 insertions(+), 9 deletions(-) diff --git a/ciphers/playfair_cipher.py b/ciphers/playfair_cipher.py index da547a337..8ef09160d 100644 --- a/ciphers/playfair_cipher.py +++ b/ciphers/playfair_cipher.py @@ -85,7 +85,7 @@ def decode(ciphertext, key): plaintext = "" # https://en.wikipedia.org/wiki/Playfair_cipher#Description - for char1, char2 in chunk(ciphertext, 2): + for char1, char2 in chunker(ciphertext, 2): row1, col1 = divmod(table.index(char1), 5) row2, col2 = divmod(table.index(char2), 5) diff --git a/data_structures/AVL/AVL.py b/data_structures/AVL/AVL.py index 9717f1b6e..84f79fd1c 100644 --- a/data_structures/AVL/AVL.py +++ b/data_structures/AVL/AVL.py @@ -1,6 +1,7 @@ ''' A AVL tree ''' +from __future__ import print_function class Node: diff --git a/data_structures/Graph/BellmanFord.py b/data_structures/Graph/BellmanFord.py index de3077f09..b9207c422 100644 --- a/data_structures/Graph/BellmanFord.py +++ b/data_structures/Graph/BellmanFord.py @@ -1,3 +1,5 @@ +from __future__ import print_function + def printDist(dist, V): print("\nVertex Distance") for i in range(V): diff --git a/data_structures/Graph/BreadthFirstSearch.py b/data_structures/Graph/BreadthFirstSearch.py index 16b1b2007..02f6af83f 100644 --- a/data_structures/Graph/BreadthFirstSearch.py +++ b/data_structures/Graph/BreadthFirstSearch.py @@ -1,4 +1,6 @@ # Author: OMKAR PATHAK +from __future__ import print_function + class Graph(): def __init__(self): diff --git a/data_structures/Graph/DepthFirstSearch.py b/data_structures/Graph/DepthFirstSearch.py index 94ef3cb86..0f10a8600 100644 --- a/data_structures/Graph/DepthFirstSearch.py +++ b/data_structures/Graph/DepthFirstSearch.py @@ -1,4 +1,6 @@ # Author: OMKAR PATHAK +from __future__ import print_function + class Graph(): def __init__(self): diff --git a/data_structures/Graph/Dijkstra.py b/data_structures/Graph/Dijkstra.py index 30ab51d76..ae6a1a283 100644 --- a/data_structures/Graph/Dijkstra.py +++ b/data_structures/Graph/Dijkstra.py @@ -1,3 +1,4 @@ +from __future__ import print_function def printDist(dist, V): print("\nVertex Distance") diff --git a/data_structures/Graph/FloydWarshall.py b/data_structures/Graph/FloydWarshall.py index f0db329d0..fae8b19b3 100644 --- a/data_structures/Graph/FloydWarshall.py +++ b/data_structures/Graph/FloydWarshall.py @@ -1,3 +1,4 @@ +from __future__ import print_function def printDist(dist, V): print("\nThe shortest path matrix using Floyd Warshall algorithm\n") @@ -7,7 +8,7 @@ def printDist(dist, V): print(int(dist[i][j]),end = "\t") else: print("INF",end="\t") - print(); + print() @@ -29,19 +30,19 @@ def FloydWarshall(graph, V): #MAIN -V = int(input("Enter number of vertices: ")); -E = int(input("Enter number of edges: ")); +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; + graph[i][i] = 0.0 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; + graph[src][dst] = weight FloydWarshall(graph, V) diff --git a/data_structures/Graph/Graph_list.py b/data_structures/Graph/Graph_list.py index 8e33cf55c..d67bc96c4 100644 --- a/data_structures/Graph/Graph_list.py +++ b/data_structures/Graph/Graph_list.py @@ -1,3 +1,6 @@ +from __future__ import print_function + + class Graph: def __init__(self, vertex): self.vertex = vertex diff --git a/data_structures/Graph/Graph_matrix.py b/data_structures/Graph/Graph_matrix.py index 1998fec8d..de25301d6 100644 --- a/data_structures/Graph/Graph_matrix.py +++ b/data_structures/Graph/Graph_matrix.py @@ -1,3 +1,6 @@ +from __future__ import print_function + + class Graph: def __init__(self, vertex): diff --git a/data_structures/Graph/dijkstra_algorithm.py b/data_structures/Graph/dijkstra_algorithm.py index c43ff37f5..985c7f6c1 100644 --- a/data_structures/Graph/dijkstra_algorithm.py +++ b/data_structures/Graph/dijkstra_algorithm.py @@ -2,6 +2,7 @@ # Author: Shubham Malik # References: https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm +from __future__ import print_function import math import sys # For storing the vertex set to retreive node with the lowest distance diff --git a/data_structures/LinkedList/DoublyLinkedList.py b/data_structures/LinkedList/DoublyLinkedList.py index d152e465b..6f17b7c81 100644 --- a/data_structures/LinkedList/DoublyLinkedList.py +++ b/data_structures/LinkedList/DoublyLinkedList.py @@ -3,6 +3,9 @@ - This is an example of a double ended, doubly linked list. - Each link references the next link and the previous one. ''' +from __future__ import print_function + + class LinkedList: def __init__(self): self.head = None @@ -70,4 +73,4 @@ class Link: def __init__(self, x): self.value = x def displayLink(self): - print("{}".format(self.value), end=" ") \ No newline at end of file + print("{}".format(self.value), end=" ") diff --git a/dynamic_programming/edit_distance.py b/dynamic_programming/edit_distance.py index 4a6e4cecf..335e5196e 100644 --- a/dynamic_programming/edit_distance.py +++ b/dynamic_programming/edit_distance.py @@ -7,6 +7,8 @@ This is a pure Python implementation of Dynamic Programming solution to the edit The problem is : Given two strings A and B. Find the minimum number of operations to string B such that A = B. The permitted operations are removal, insertion, and substitution. """ +from __future__ import print_function + class EditDistance: """ diff --git a/dynamic_programming/fibonacci.py b/dynamic_programming/fibonacci.py index abe628d03..b453ce255 100644 --- a/dynamic_programming/fibonacci.py +++ b/dynamic_programming/fibonacci.py @@ -1,6 +1,7 @@ """ This is a pure Python implementation of Dynamic Programming solution to the fibonacci sequence problem. """ +from __future__ import print_function class Fibonacci: diff --git a/machine_learning/k_means_clust.py b/machine_learning/k_means_clust.py index c19832726..368739a45 100644 --- a/machine_learning/k_means_clust.py +++ b/machine_learning/k_means_clust.py @@ -46,6 +46,7 @@ Usage: 5. Have fun.. ''' +from __future__ import print_function from sklearn.metrics import pairwise_distances import numpy as np @@ -169,4 +170,4 @@ if False: # change to true to run this test case. 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) \ No newline at end of file + plot_heterogeneity(heterogeneity, k) diff --git a/machine_learning/scoring_functions.py b/machine_learning/scoring_functions.py index 64806a096..0a988737f 100644 --- a/machine_learning/scoring_functions.py +++ b/machine_learning/scoring_functions.py @@ -41,7 +41,7 @@ def rmse(predict, actual): actual = np.array(actual) difference = predict - actual - square_diff = np.square(dfference) + square_diff = np.square(difference) mean_square_diff = square_diff.mean() score = np.sqrt(mean_square_diff) return score diff --git a/other/FindingPrimes.py b/other/FindingPrimes.py index 3cb10f701..035a14f4a 100644 --- a/other/FindingPrimes.py +++ b/other/FindingPrimes.py @@ -2,6 +2,9 @@ -The sieve of Eratosthenes is an algorithm used to find prime numbers, less than or equal to a given value. -Illustration: https://upload.wikimedia.org/wikipedia/commons/b/b9/Sieve_of_Eratosthenes_animation.gif ''' +from __future__ import print_function + + from math import sqrt def SOE(n): check = round(sqrt(n)) #Need not check for multiples past the square root of n diff --git a/sorts/cyclesort.py b/sorts/cyclesort.py index 2cc635417..d6762b083 100644 --- a/sorts/cyclesort.py +++ b/sorts/cyclesort.py @@ -1,4 +1,7 @@ # Code contributed by Honey Sharma +from __future__ import print_function + + def cycle_sort(array): ans = 0