Skip to content

Commit 89acf5d

Browse files
cclaussharshildarji
authored andcommitted
print() is a function just like every other function (TheAlgorithms#1101)
* print() is a function just like every other function
1 parent 6654e1e commit 89acf5d

13 files changed

+133
-133
lines changed

arithmetic_analysis/newton_raphson_method.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,25 @@ def NewtonRaphson(func, a):
88
''' Finds root from the point 'a' onwards by Newton-Raphson method '''
99
while True:
1010
c = Decimal(a) - ( Decimal(eval(func)) / Decimal(eval(str(diff(func)))) )
11-
11+
1212
a = c
1313

1414
# This number dictates the accuracy of the answer
1515
if abs(eval(func)) < 10**-15:
1616
return c
17-
17+
1818

1919
# Let's Execute
2020
if __name__ == '__main__':
2121
# Find root of trigonometric function
2222
# Find value of pi
23-
print ('sin(x) = 0', NewtonRaphson('sin(x)', 2))
24-
23+
print('sin(x) = 0', NewtonRaphson('sin(x)', 2))
24+
2525
# Find root of polynomial
26-
print ('x**2 - 5*x +2 = 0', NewtonRaphson('x**2 - 5*x +2', 0.4))
27-
26+
print('x**2 - 5*x +2 = 0', NewtonRaphson('x**2 - 5*x +2', 0.4))
27+
2828
# Find Square Root of 5
29-
print ('x**2 - 5 = 0', NewtonRaphson('x**2 - 5', 0.1))
29+
print('x**2 - 5 = 0', NewtonRaphson('x**2 - 5', 0.1))
3030

3131
# Exponential Roots
32-
print ('exp(x) - 1 = 0', NewtonRaphson('exp(x) - 1', 0))
32+
print('exp(x) - 1 = 0', NewtonRaphson('exp(x) - 1', 0))

ciphers/caesar_cipher.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ def main():
4141
print("4.Quit")
4242
choice = input("What would you like to do?: ")
4343
if choice not in ['1', '2', '3', '4']:
44-
print ("Invalid choice, please enter a valid choice")
44+
print("Invalid choice, please enter a valid choice")
4545
elif choice == '1':
4646
strng = input("Please enter the string to be encrypted: ")
4747
key = int(input("Please enter off-set between 1-94: "))
4848
if key in range(1, 95):
49-
print (encrypt(strng.lower(), key))
49+
print(encrypt(strng.lower(), key))
5050
elif choice == '2':
5151
strng = input("Please enter the string to be decrypted: ")
5252
key = int(input("Please enter off-set between 1-94: "))
@@ -57,7 +57,7 @@ def main():
5757
brute_force(strng)
5858
main()
5959
elif choice == '4':
60-
print ("Goodbye.")
60+
print("Goodbye.")
6161
break
6262

6363

ciphers/morse_code_implementation.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ def decrypt(message):
7171
def main():
7272
message = "Morse code here"
7373
result = encrypt(message.upper())
74-
print (result)
74+
print(result)
7575

7676
message = result
7777
result = decrypt(message)
78-
print (result)
78+
print(result)
7979

8080

8181
if __name__ == '__main__':

ciphers/trafid_cipher.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
def __encryptPart(messagePart, character2Number):
44
one, two, three = "", "", ""
55
tmp = []
6-
6+
77
for character in messagePart:
88
tmp.append(character2Number[character])
99

1010
for each in tmp:
1111
one += each[0]
1212
two += each[1]
1313
three += each[2]
14-
14+
1515
return one+two+three
1616

1717
def __decryptPart(messagePart, character2Number):
@@ -25,7 +25,7 @@ def __decryptPart(messagePart, character2Number):
2525
tmp += digit
2626
if len(tmp) == len(messagePart):
2727
result.append(tmp)
28-
tmp = ""
28+
tmp = ""
2929

3030
return result[0], result[1], result[2]
3131

@@ -48,7 +48,7 @@ def __prepare(message, alphabet):
4848
for letter, number in zip(alphabet, numbers):
4949
character2Number[letter] = number
5050
number2Character[number] = letter
51-
51+
5252
return message, alphabet, character2Number, number2Character
5353

5454
def encryptMessage(message, alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.", period=5):
@@ -57,7 +57,7 @@ def encryptMessage(message, alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.", period=5):
5757

5858
for i in range(0, len(message)+1, period):
5959
encrypted_numeric += __encryptPart(message[i:i+period], character2Number)
60-
60+
6161
for i in range(0, len(encrypted_numeric), 3):
6262
encrypted += number2Character[encrypted_numeric[i:i+3]]
6363

@@ -70,7 +70,7 @@ def decryptMessage(message, alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.", period=5):
7070

7171
for i in range(0, len(message)+1, period):
7272
a,b,c = __decryptPart(message[i:i+period], character2Number)
73-
73+
7474
for j in range(0, len(a)):
7575
decrypted_numeric.append(a[j]+b[j]+c[j])
7676

@@ -83,4 +83,4 @@ def decryptMessage(message, alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.", period=5):
8383
msg = "DEFEND THE EAST WALL OF THE CASTLE."
8484
encrypted = encryptMessage(msg,"EPSDUCVWYM.ZLKXNBTFGORIJHAQ")
8585
decrypted = decryptMessage(encrypted, "EPSDUCVWYM.ZLKXNBTFGORIJHAQ")
86-
print ("Encrypted: {}\nDecrypted: {}".format(encrypted, decrypted))
86+
print("Encrypted: {}\nDecrypted: {}".format(encrypted, decrypted))

ciphers/xor_cipher.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def decrypt_string(self,content,key = 0):
122122

123123
# This will be returned
124124
ans = ""
125-
125+
126126
for ch in content:
127127
ans += chr(ord(ch) ^ key)
128128

@@ -188,22 +188,22 @@ def decrypt_file(self,file, key):
188188
# key = 67
189189

190190
# # test enrcypt
191-
# print crypt.encrypt("hallo welt",key)
191+
# print(crypt.encrypt("hallo welt",key))
192192
# # test decrypt
193-
# print crypt.decrypt(crypt.encrypt("hallo welt",key), key)
193+
# print(crypt.decrypt(crypt.encrypt("hallo welt",key), key))
194194

195195
# # test encrypt_string
196-
# print crypt.encrypt_string("hallo welt",key)
196+
# print(crypt.encrypt_string("hallo welt",key))
197197

198198
# # test decrypt_string
199-
# print crypt.decrypt_string(crypt.encrypt_string("hallo welt",key),key)
199+
# print(crypt.decrypt_string(crypt.encrypt_string("hallo welt",key),key))
200200

201201
# if (crypt.encrypt_file("test.txt",key)):
202-
# print "encrypt successful"
202+
# print("encrypt successful")
203203
# else:
204-
# print "encrypt unsuccessful"
204+
# print("encrypt unsuccessful")
205205

206206
# if (crypt.decrypt_file("encrypt.out",key)):
207-
# print "decrypt successful"
207+
# print("decrypt successful")
208208
# else:
209-
# print "decrypt unsuccessful"
209+
# print("decrypt unsuccessful")

data_structures/binary_tree/fenwick_tree.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ def query(self, i): # query cumulative data from index 0 to i in O(lg N)
1616
ret += self.ft[i]
1717
i -= i & (-i)
1818
return ret
19-
19+
2020
if __name__ == '__main__':
2121
f = FenwickTree(100)
2222
f.update(1,20)
2323
f.update(4,4)
24-
print (f.query(1))
25-
print (f.query(3))
26-
print (f.query(4))
24+
print(f.query(1))
25+
print(f.query(3))
26+
print(f.query(4))
2727
f.update(2,-5)
28-
print (f.query(1))
29-
print (f.query(3))
28+
print(f.query(1))
29+
print(f.query(3))

data_structures/binary_tree/lazy_segment_tree.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
import math
33

44
class SegmentTree:
5-
5+
66
def __init__(self, N):
77
self.N = N
88
self.st = [0 for i in range(0,4*N)] # approximate the overall size of segment tree with array N
99
self.lazy = [0 for i in range(0,4*N)] # create array to store lazy update
1010
self.flag = [0 for i in range(0,4*N)] # flag for lazy update
11-
11+
1212
def left(self, idx):
1313
return idx*2
1414

@@ -34,7 +34,7 @@ def update(self, idx, l, r, a, b, val): # update(1, 1, N, a, b, v) for update va
3434
self.lazy[self.right(idx)] = self.lazy[idx]
3535
self.flag[self.left(idx)] = True
3636
self.flag[self.right(idx)] = True
37-
37+
3838
if r < a or l > b:
3939
return True
4040
if l >= a and r <= b :
@@ -74,18 +74,18 @@ def showData(self):
7474
showList = []
7575
for i in range(1,N+1):
7676
showList += [self.query(1, 1, self.N, i, i)]
77-
print (showList)
78-
77+
print(showList)
78+
7979

8080
if __name__ == '__main__':
8181
A = [1,2,-4,7,3,-5,6,11,-20,9,14,15,5,2,-8]
8282
N = 15
8383
segt = SegmentTree(N)
8484
segt.build(1,1,N,A)
85-
print (segt.query(1,1,N,4,6))
86-
print (segt.query(1,1,N,7,11))
87-
print (segt.query(1,1,N,7,12))
85+
print(segt.query(1,1,N,4,6))
86+
print(segt.query(1,1,N,7,11))
87+
print(segt.query(1,1,N,7,12))
8888
segt.update(1,1,N,1,3,111)
89-
print (segt.query(1,1,N,1,15))
89+
print(segt.query(1,1,N,1,15))
9090
segt.update(1,1,N,7,8,235)
9191
segt.showData()

data_structures/binary_tree/segment_tree.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
import math
33

44
class SegmentTree:
5-
5+
66
def __init__(self, A):
77
self.N = len(A)
88
self.st = [0] * (4 * self.N) # approximate the overall size of segment tree with array N
99
self.build(1, 0, self.N - 1)
10-
10+
1111
def left(self, idx):
1212
return idx * 2
1313

@@ -22,10 +22,10 @@ def build(self, idx, l, r):
2222
self.build(self.left(idx), l, mid)
2323
self.build(self.right(idx), mid + 1, r)
2424
self.st[idx] = max(self.st[self.left(idx)] , self.st[self.right(idx)])
25-
25+
2626
def update(self, a, b, val):
2727
return self.update_recursive(1, 0, self.N - 1, a - 1, b - 1, val)
28-
28+
2929
def update_recursive(self, idx, l, r, a, b, val): # update(1, 1, N, a, b, v) for update val v to [a,b]
3030
if r < a or l > b:
3131
return True
@@ -55,17 +55,17 @@ def showData(self):
5555
showList = []
5656
for i in range(1,N+1):
5757
showList += [self.query(i, i)]
58-
print (showList)
59-
58+
print(showList)
59+
6060

6161
if __name__ == '__main__':
6262
A = [1,2,-4,7,3,-5,6,11,-20,9,14,15,5,2,-8]
6363
N = 15
6464
segt = SegmentTree(A)
65-
print (segt.query(4, 6))
66-
print (segt.query(7, 11))
67-
print (segt.query(7, 12))
65+
print(segt.query(4, 6))
66+
print(segt.query(7, 11))
67+
print(segt.query(7, 12))
6868
segt.update(1,3,111)
69-
print (segt.query(1, 15))
69+
print(segt.query(1, 15))
7070
segt.update(7,8,235)
7171
segt.showData()
+21-21
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
11
from __future__ import print_function
2-
# Python code to demonstrate working of
2+
# Python code to demonstrate working of
33
# extend(), extendleft(), rotate(), reverse()
4-
4+
55
# importing "collections" for deque operations
66
import collections
7-
7+
88
# initializing deque
99
de = collections.deque([1, 2, 3,])
10-
11-
# using extend() to add numbers to right end
10+
11+
# using extend() to add numbers to right end
1212
# adds 4,5,6 to right end
1313
de.extend([4,5,6])
14-
14+
1515
# printing modified deque
16-
print ("The deque after extending deque at end is : ")
17-
print (de)
18-
19-
# using extendleft() to add numbers to left end
16+
print("The deque after extending deque at end is : ")
17+
print(de)
18+
19+
# using extendleft() to add numbers to left end
2020
# adds 7,8,9 to right end
2121
de.extendleft([7,8,9])
22-
22+
2323
# printing modified deque
24-
print ("The deque after extending deque at beginning is : ")
25-
print (de)
26-
24+
print("The deque after extending deque at beginning is : ")
25+
print(de)
26+
2727
# using rotate() to rotate the deque
2828
# rotates by 3 to left
2929
de.rotate(-3)
30-
30+
3131
# printing modified deque
32-
print ("The deque after rotating deque is : ")
33-
print (de)
34-
32+
print("The deque after rotating deque is : ")
33+
print(de)
34+
3535
# using reverse() to reverse the deque
3636
de.reverse()
37-
37+
3838
# printing modified deque
39-
print ("The deque after reversing deque is : ")
40-
print (de)
39+
print("The deque after reversing deque is : ")
40+
print(de)

0 commit comments

Comments
 (0)