Skip to content

Commit 47a9ea2

Browse files
authored
Simplify code by dropping support for legacy Python (#1143)
* Simplify code by dropping support for legacy Python * sort() --> sorted()
1 parent 32aa7ff commit 47a9ea2

File tree

145 files changed

+367
-976
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

145 files changed

+367
-976
lines changed

Diff for: CONTRIBUTING.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ We want your work to be readable by others; therefore, we encourage you to note
5656

5757
```python
5858
"""
59-
This function sums a and b
59+
This function sums a and b
6060
"""
6161
def sum(a, b):
6262
return a + b
@@ -82,13 +82,13 @@ We want your work to be readable by others; therefore, we encourage you to note
8282
The following "testing" approaches are **not** encouraged:
8383

8484
```python
85-
input('Enter your input:')
85+
input('Enter your input:')
8686
# Or even worse...
87-
input = eval(raw_input("Enter your input: "))
87+
input = eval(input("Enter your input: "))
8888
```
89-
89+
9090
However, if your code uses __input()__ then we encourage you to gracefully deal with leading and trailing whitespace in user input by adding __.strip()__ to the end as in:
91-
91+
9292
```python
9393
starting_value = int(input("Please enter a starting value: ").strip())
9494
```
@@ -99,13 +99,13 @@ We want your work to be readable by others; therefore, we encourage you to note
9999
def sumab(a, b):
100100
return a + b
101101
# Write tests this way:
102-
print(sumab(1,2)) # 1+2 = 3
103-
print(sumab(6,4)) # 6+4 = 10
102+
print(sumab(1, 2)) # 1+2 = 3
103+
print(sumab(6, 4)) # 6+4 = 10
104104
# Or this way:
105-
print("1 + 2 = ", sumab(1,2)) # 1+2 = 3
106-
print("6 + 4 = ", sumab(6,4)) # 6+4 = 10
105+
print("1 + 2 = ", sumab(1, 2)) # 1+2 = 3
106+
print("6 + 4 = ", sumab(6, 4)) # 6+4 = 10
107107
```
108-
108+
109109
Better yet, if you know how to write [__doctests__](https://docs.python.org/3/library/doctest.html), please consider adding them.
110110

111111
- Avoid importing external libraries for basic algorithms. Only use those libraries for complicated algorithms.

Diff for: ciphers/affine_cipher.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import print_function
21
import sys, random, cryptomath_module as cryptoMath
32

43
SYMBOLS = r""" !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"""

Diff for: ciphers/atbash.py

+6-14
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
1-
try: # Python 2
2-
raw_input
3-
unichr
4-
except NameError: # Python 3
5-
raw_input = input
6-
unichr = chr
7-
8-
9-
def Atbash():
1+
def atbash():
102
output=""
11-
for i in raw_input("Enter the sentence to be encrypted ").strip():
3+
for i in input("Enter the sentence to be encrypted ").strip():
124
extract = ord(i)
135
if 65 <= extract <= 90:
14-
output += unichr(155-extract)
6+
output += chr(155-extract)
157
elif 97 <= extract <= 122:
16-
output += unichr(219-extract)
8+
output += chr(219-extract)
179
else:
18-
output+=i
10+
output += i
1911
print(output)
2012

2113

2214
if __name__ == '__main__':
23-
Atbash()
15+
atbash()

Diff for: ciphers/brute_force_caesar_cipher.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import print_function
21
def decrypt(message):
32
"""
43
>>> decrypt('TMDETUX PMDVU')

Diff for: ciphers/onepad_cipher.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from __future__ import print_function
2-
31
import random
42

53

@@ -15,7 +13,7 @@ def encrypt(self, text):
1513
cipher.append(c)
1614
key.append(k)
1715
return cipher, key
18-
16+
1917
def decrypt(self, cipher, key):
2018
'''Function to decrypt text using psedo-random numbers.'''
2119
plain = []

Diff for: ciphers/rabin_miller.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import print_function
21
# Primality Testing with the Rabin-Miller Algorithm
32

43
import random

Diff for: ciphers/rot13.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import print_function
21
def dencrypt(s, n):
32
out = ''
43
for c in s:

Diff for: ciphers/rsa_cipher.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import print_function
21
import sys, rsa_key_generator as rkg, os
32

43
DEFAULT_BLOCK_SIZE = 128
@@ -16,7 +15,7 @@ def main():
1615
if mode == 'encrypt':
1716
if not os.path.exists('rsa_pubkey.txt'):
1817
rkg.makeKeyFiles('rsa', 1024)
19-
18+
2019
message = input('\nEnter message: ')
2120
pubKeyFilename = 'rsa_pubkey.txt'
2221
print('Encrypting and writing to %s...' % (filename))

Diff for: ciphers/rsa_key_generator.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import print_function
21
import random, sys, os
32
import rabin_miller as rabinMiller, cryptomath_module as cryptoMath
43

Diff for: ciphers/simple_substitution_cipher.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import print_function
21
import sys, random
32

43
LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
@@ -18,7 +17,7 @@ def main():
1817
translated = decryptMessage(key, message)
1918

2019
print('\n%sion: \n%s' % (mode.title(), translated))
21-
20+
2221
def checkValidKey(key):
2322
keyList = list(key)
2423
lettersList = list(LETTERS)
@@ -49,7 +48,7 @@ def translateMessage(key, message, mode):
4948

5049
if mode == 'decrypt':
5150
charsA, charsB = charsB, charsA
52-
51+
5352
for symbol in message:
5453
if symbol.upper() in charsA:
5554
symIndex = charsA.find(symbol.upper())

Diff for: ciphers/transposition_cipher.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import print_function
21
import math
32

43
def main():

Diff for: ciphers/transposition_cipher_encrypt_decrypt_file.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import print_function
21
import time, os, sys
32
import transposition_cipher as transCipher
43

@@ -16,7 +15,7 @@ def main():
1615
response = input('> ')
1716
if not response.lower().startswith('y'):
1817
sys.exit()
19-
18+
2019
startTime = time.time()
2120
if mode.lower().startswith('e'):
2221
with open(inputFile) as f:
@@ -29,9 +28,9 @@ def main():
2928

3029
with open(outputFile, 'w') as outputObj:
3130
outputObj.write(translated)
32-
31+
3332
totalTime = round(time.time() - startTime, 2)
3433
print(('Done (', totalTime, 'seconds )'))
35-
34+
3635
if __name__ == '__main__':
3736
main()

Diff for: ciphers/vigenere_cipher.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import print_function
21
LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
32

43
def main():

Diff for: data_structures/binary_tree/binary_search_tree.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'''
22
A binary search Tree
33
'''
4-
from __future__ import print_function
54
class Node:
65

76
def __init__(self, label, parent):
@@ -66,8 +65,8 @@ def insert(self, label):
6665
else:
6766
parent_node.setRight(new_node)
6867
#Set parent to the new node
69-
new_node.setParent(parent_node)
70-
68+
new_node.setParent(parent_node)
69+
7170
def delete(self, label):
7271
if (not self.empty()):
7372
#Look for the node with that label
@@ -92,7 +91,7 @@ def delete(self, label):
9291
self.delete(tmpNode.getLabel())
9392
#Assigns the value to the node to delete and keesp tree structure
9493
node.setLabel(tmpNode.getLabel())
95-
94+
9695
def getNode(self, label):
9796
curr_node = None
9897
#If the tree is not empty
@@ -177,7 +176,7 @@ def traversalTree(self, traversalFunction = None, root = None):
177176
#Returns a list of nodes in the order that the users wants to
178177
return traversalFunction(self.root)
179178

180-
#Returns an string of all the nodes labels in the list
179+
#Returns an string of all the nodes labels in the list
181180
#In Order Traversal
182181
def __str__(self):
183182
list = self.__InOrderTraversal(self.root)
@@ -203,7 +202,7 @@ def testBinarySearchTree():
203202
/ \ \
204203
1 6 14
205204
/ \ /
206-
4 7 13
205+
4 7 13
207206
'''
208207

209208
r'''
@@ -236,11 +235,11 @@ def testBinarySearchTree():
236235
print("The label -1 exists")
237236
else:
238237
print("The label -1 doesn't exist")
239-
238+
240239
if(not t.empty()):
241240
print(("Max Value: ", t.getMax().getLabel()))
242241
print(("Min Value: ", t.getMin().getLabel()))
243-
242+
244243
t.delete(13)
245244
t.delete(10)
246245
t.delete(8)

Diff for: data_structures/binary_tree/fenwick_tree.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import print_function
21
class FenwickTree:
32

43
def __init__(self, SIZE): # create fenwick tree with size SIZE

Diff for: data_structures/binary_tree/lazy_segment_tree.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import print_function
21
import math
32

43
class SegmentTree:

Diff for: data_structures/binary_tree/segment_tree.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import print_function
21
import math
32

43
class SegmentTree:

Diff for: data_structures/heap/heap.py

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
#!/usr/bin/python
22

3-
from __future__ import print_function, division
4-
5-
try:
6-
raw_input # Python 2
7-
except NameError:
8-
raw_input = input # Python 3
9-
10-
#This heap class start from here.
3+
# This heap class start from here.
114
class Heap:
12-
def __init__(self): #Default constructor of heap class.
5+
def __init__(self): # Default constructor of heap class.
136
self.h = []
147
self.currsize = 0
158

@@ -79,7 +72,7 @@ def display(self): #This function is used to print the heap.
7972
print(self.h)
8073

8174
def main():
82-
l = list(map(int, raw_input().split()))
75+
l = list(map(int, input().split()))
8376
h = Heap()
8477
h.buildHeap(l)
8578
h.heapSort()

Diff for: data_structures/linked_list/doubly_linked_list.py

+13-14
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44
- Each link references the next link and the previous one.
55
- 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.
66
- Advantages over SLL - IT can be traversed in both forward and backward direction.,Delete operation is more efficent'''
7-
from __future__ import print_function
87

98

109
class LinkedList: #making main class named linked list
1110
def __init__(self):
1211
self.head = None
1312
self.tail = None
14-
13+
1514
def insertHead(self, x):
1615
newLink = Link(x) #Create a new link with a value attached to it
1716
if(self.isEmpty() == True): #Set the first element added to be the tail
@@ -20,52 +19,52 @@ def insertHead(self, x):
2019
self.head.previous = newLink # newLink <-- currenthead(head)
2120
newLink.next = self.head # newLink <--> currenthead(head)
2221
self.head = newLink # newLink(head) <--> oldhead
23-
22+
2423
def deleteHead(self):
2524
temp = self.head
26-
self.head = self.head.next # oldHead <--> 2ndElement(head)
25+
self.head = self.head.next # oldHead <--> 2ndElement(head)
2726
self.head.previous = None # oldHead --> 2ndElement(head) nothing pointing at it so the old head will be removed
2827
if(self.head is None):
2928
self.tail = None #if empty linked list
3029
return temp
31-
30+
3231
def insertTail(self, x):
3332
newLink = Link(x)
3433
newLink.next = None # currentTail(tail) newLink -->
3534
self.tail.next = newLink # currentTail(tail) --> newLink -->
3635
newLink.previous = self.tail #currentTail(tail) <--> newLink -->
3736
self.tail = newLink # oldTail <--> newLink(tail) -->
38-
37+
3938
def deleteTail(self):
4039
temp = self.tail
4140
self.tail = self.tail.previous # 2ndLast(tail) <--> oldTail --> None
4241
self.tail.next = None # 2ndlast(tail) --> None
4342
return temp
44-
43+
4544
def delete(self, x):
4645
current = self.head
47-
46+
4847
while(current.value != x): # Find the position to delete
4948
current = current.next
50-
49+
5150
if(current == self.head):
5251
self.deleteHead()
53-
52+
5453
elif(current == self.tail):
5554
self.deleteTail()
56-
55+
5756
else: #Before: 1 <--> 2(current) <--> 3
5857
current.previous.next = current.next # 1 --> 3
5958
current.next.previous = current.previous # 1 <--> 3
60-
59+
6160
def isEmpty(self): #Will return True if the list is empty
6261
return(self.head is None)
63-
62+
6463
def display(self): #Prints contents of the list
6564
current = self.head
6665
while(current != None):
6766
current.displayLink()
68-
current = current.next
67+
current = current.next
6968
print()
7069

7170
class Link:

0 commit comments

Comments
 (0)