Skip to content

Commit

Permalink
Modernize Python 2 code to get ready for Python 3
Browse files Browse the repository at this point in the history
  • Loading branch information
cclauss committed Nov 25, 2017
1 parent 4e06949 commit e31c780
Show file tree
Hide file tree
Showing 17 changed files with 38 additions and 9 deletions.
2 changes: 1 addition & 1 deletion ciphers/playfair_cipher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions data_structures/AVL/AVL.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'''
A AVL tree
'''
from __future__ import print_function


class Node:
Expand Down
2 changes: 2 additions & 0 deletions data_structures/Graph/BellmanFord.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import print_function

def printDist(dist, V):
print("\nVertex Distance")
for i in range(V):
Expand Down
2 changes: 2 additions & 0 deletions data_structures/Graph/BreadthFirstSearch.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Author: OMKAR PATHAK
from __future__ import print_function


class Graph():
def __init__(self):
Expand Down
2 changes: 2 additions & 0 deletions data_structures/Graph/DepthFirstSearch.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Author: OMKAR PATHAK
from __future__ import print_function


class Graph():
def __init__(self):
Expand Down
1 change: 1 addition & 0 deletions data_structures/Graph/Dijkstra.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function

def printDist(dist, V):
print("\nVertex Distance")
Expand Down
11 changes: 6 additions & 5 deletions data_structures/Graph/FloydWarshall.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function

def printDist(dist, V):
print("\nThe shortest path matrix using Floyd Warshall algorithm\n")
Expand All @@ -7,7 +8,7 @@ def printDist(dist, V):
print(int(dist[i][j]),end = "\t")
else:
print("INF",end="\t")
print();
print()



Expand All @@ -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)
3 changes: 3 additions & 0 deletions data_structures/Graph/Graph_list.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from __future__ import print_function


class Graph:
def __init__(self, vertex):
self.vertex = vertex
Expand Down
3 changes: 3 additions & 0 deletions data_structures/Graph/Graph_matrix.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from __future__ import print_function


class Graph:

def __init__(self, vertex):
Expand Down
1 change: 1 addition & 0 deletions data_structures/Graph/dijkstra_algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion data_structures/LinkedList/DoublyLinkedList.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -70,4 +73,4 @@ class Link:
def __init__(self, x):
self.value = x
def displayLink(self):
print("{}".format(self.value), end=" ")
print("{}".format(self.value), end=" ")
2 changes: 2 additions & 0 deletions dynamic_programming/edit_distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
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:
"""
Expand Down
1 change: 1 addition & 0 deletions dynamic_programming/fibonacci.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
3 changes: 2 additions & 1 deletion machine_learning/k_means_clust.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
5. Have fun..
'''
from __future__ import print_function
from sklearn.metrics import pairwise_distances
import numpy as np

Expand Down Expand Up @@ -169,4 +170,4 @@ def kmeans(data, k, initial_centroids, maxiter=500, record_heterogeneity=None, v
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)
plot_heterogeneity(heterogeneity, k)
2 changes: 1 addition & 1 deletion machine_learning/scoring_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions other/FindingPrimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions sorts/cyclesort.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Code contributed by Honey Sharma
from __future__ import print_function


def cycle_sort(array):
ans = 0

Expand Down

0 comments on commit e31c780

Please sign in to comment.