Skip to content

Commit e31c780

Browse files
author
cclauss
committed
Modernize Python 2 code to get ready for Python 3
1 parent 4e06949 commit e31c780

17 files changed

+38
-9
lines changed

ciphers/playfair_cipher.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def decode(ciphertext, key):
8585
plaintext = ""
8686

8787
# https://en.wikipedia.org/wiki/Playfair_cipher#Description
88-
for char1, char2 in chunk(ciphertext, 2):
88+
for char1, char2 in chunker(ciphertext, 2):
8989
row1, col1 = divmod(table.index(char1), 5)
9090
row2, col2 = divmod(table.index(char2), 5)
9191

data_structures/AVL/AVL.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'''
22
A AVL tree
33
'''
4+
from __future__ import print_function
45

56

67
class Node:

data_structures/Graph/BellmanFord.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import print_function
2+
13
def printDist(dist, V):
24
print("\nVertex Distance")
35
for i in range(V):

data_structures/Graph/BreadthFirstSearch.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# Author: OMKAR PATHAK
2+
from __future__ import print_function
3+
24

35
class Graph():
46
def __init__(self):

data_structures/Graph/DepthFirstSearch.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# Author: OMKAR PATHAK
2+
from __future__ import print_function
3+
24

35
class Graph():
46
def __init__(self):

data_structures/Graph/Dijkstra.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import print_function
12

23
def printDist(dist, V):
34
print("\nVertex Distance")

data_structures/Graph/FloydWarshall.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import print_function
12

23
def printDist(dist, V):
34
print("\nThe shortest path matrix using Floyd Warshall algorithm\n")
@@ -7,7 +8,7 @@ def printDist(dist, V):
78
print(int(dist[i][j]),end = "\t")
89
else:
910
print("INF",end="\t")
10-
print();
11+
print()
1112

1213

1314

@@ -29,19 +30,19 @@ def FloydWarshall(graph, V):
2930

3031

3132
#MAIN
32-
V = int(input("Enter number of vertices: "));
33-
E = int(input("Enter number of edges: "));
33+
V = int(input("Enter number of vertices: "))
34+
E = int(input("Enter number of edges: "))
3435

3536
graph = [[float('inf') for i in range(V)] for j in range(V)]
3637

3738
for i in range(V):
38-
graph[i][i] = 0.0;
39+
graph[i][i] = 0.0
3940

4041
for i in range(E):
4142
print("\nEdge ",i+1)
4243
src = int(input("Enter source:"))
4344
dst = int(input("Enter destination:"))
4445
weight = float(input("Enter weight:"))
45-
graph[src][dst] = weight;
46+
graph[src][dst] = weight
4647

4748
FloydWarshall(graph, V)

data_structures/Graph/Graph_list.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from __future__ import print_function
2+
3+
14
class Graph:
25
def __init__(self, vertex):
36
self.vertex = vertex

data_structures/Graph/Graph_matrix.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from __future__ import print_function
2+
3+
14
class Graph:
25

36
def __init__(self, vertex):

data_structures/Graph/dijkstra_algorithm.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Author: Shubham Malik
33
# References: https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
44

5+
from __future__ import print_function
56
import math
67
import sys
78
# For storing the vertex set to retreive node with the lowest distance

data_structures/LinkedList/DoublyLinkedList.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
- This is an example of a double ended, doubly linked list.
44
- Each link references the next link and the previous one.
55
'''
6+
from __future__ import print_function
7+
8+
69
class LinkedList:
710
def __init__(self):
811
self.head = None
@@ -70,4 +73,4 @@ class Link:
7073
def __init__(self, x):
7174
self.value = x
7275
def displayLink(self):
73-
print("{}".format(self.value), end=" ")
76+
print("{}".format(self.value), end=" ")

dynamic_programming/edit_distance.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
The problem is :
88
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.
99
"""
10+
from __future__ import print_function
11+
1012

1113
class EditDistance:
1214
"""

dynamic_programming/fibonacci.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
This is a pure Python implementation of Dynamic Programming solution to the fibonacci sequence problem.
33
"""
4+
from __future__ import print_function
45

56

67
class Fibonacci:

machine_learning/k_means_clust.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
5. Have fun..
4747
4848
'''
49+
from __future__ import print_function
4950
from sklearn.metrics import pairwise_distances
5051
import numpy as np
5152

@@ -169,4 +170,4 @@ def kmeans(data, k, initial_centroids, maxiter=500, record_heterogeneity=None, v
169170
initial_centroids = get_initial_centroids(dataset['data'], k, seed=0)
170171
centroids, cluster_assignment = kmeans(dataset['data'], k, initial_centroids, maxiter=400,
171172
record_heterogeneity=heterogeneity, verbose=True)
172-
plot_heterogeneity(heterogeneity, k)
173+
plot_heterogeneity(heterogeneity, k)

machine_learning/scoring_functions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def rmse(predict, actual):
4141
actual = np.array(actual)
4242

4343
difference = predict - actual
44-
square_diff = np.square(dfference)
44+
square_diff = np.square(difference)
4545
mean_square_diff = square_diff.mean()
4646
score = np.sqrt(mean_square_diff)
4747
return score

other/FindingPrimes.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
-The sieve of Eratosthenes is an algorithm used to find prime numbers, less than or equal to a given value.
33
-Illustration: https://upload.wikimedia.org/wikipedia/commons/b/b9/Sieve_of_Eratosthenes_animation.gif
44
'''
5+
from __future__ import print_function
6+
7+
58
from math import sqrt
69
def SOE(n):
710
check = round(sqrt(n)) #Need not check for multiples past the square root of n

sorts/cyclesort.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# Code contributed by Honey Sharma
2+
from __future__ import print_function
3+
4+
25
def cycle_sort(array):
36
ans = 0
47

0 commit comments

Comments
 (0)