Skip to content

Revert PR #1101 #1103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions arithmetic_analysis/newton_raphson_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@ def NewtonRaphson(func, a):
''' Finds root from the point 'a' onwards by Newton-Raphson method '''
while True:
c = Decimal(a) - ( Decimal(eval(func)) / Decimal(eval(str(diff(func)))) )

a = c

# This number dictates the accuracy of the answer
if abs(eval(func)) < 10**-15:
return c


# Let's Execute
if __name__ == '__main__':
# Find root of trigonometric function
# Find value of pi
print('sin(x) = 0', NewtonRaphson('sin(x)', 2))

print ('sin(x) = 0', NewtonRaphson('sin(x)', 2))
# Find root of polynomial
print('x**2 - 5*x +2 = 0', NewtonRaphson('x**2 - 5*x +2', 0.4))

print ('x**2 - 5*x +2 = 0', NewtonRaphson('x**2 - 5*x +2', 0.4))
# Find Square Root of 5
print('x**2 - 5 = 0', NewtonRaphson('x**2 - 5', 0.1))
print ('x**2 - 5 = 0', NewtonRaphson('x**2 - 5', 0.1))

# Exponential Roots
print('exp(x) - 1 = 0', NewtonRaphson('exp(x) - 1', 0))
print ('exp(x) - 1 = 0', NewtonRaphson('exp(x) - 1', 0))
6 changes: 3 additions & 3 deletions ciphers/caesar_cipher.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ def main():
print("4.Quit")
choice = input("What would you like to do?: ")
if choice not in ['1', '2', '3', '4']:
print("Invalid choice, please enter a valid choice")
print ("Invalid choice, please enter a valid choice")
elif choice == '1':
strng = input("Please enter the string to be encrypted: ")
key = int(input("Please enter off-set between 1-94: "))
if key in range(1, 95):
print(encrypt(strng.lower(), key))
print (encrypt(strng.lower(), key))
elif choice == '2':
strng = input("Please enter the string to be decrypted: ")
key = int(input("Please enter off-set between 1-94: "))
Expand All @@ -57,7 +57,7 @@ def main():
brute_force(strng)
main()
elif choice == '4':
print("Goodbye.")
print ("Goodbye.")
break


Expand Down
4 changes: 2 additions & 2 deletions ciphers/morse_code_implementation.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ def decrypt(message):
def main():
message = "Morse code here"
result = encrypt(message.upper())
print(result)
print (result)

message = result
result = decrypt(message)
print(result)
print (result)


if __name__ == '__main__':
Expand Down
14 changes: 7 additions & 7 deletions ciphers/trafid_cipher.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
def __encryptPart(messagePart, character2Number):
one, two, three = "", "", ""
tmp = []

for character in messagePart:
tmp.append(character2Number[character])

for each in tmp:
one += each[0]
two += each[1]
three += each[2]

return one+two+three

def __decryptPart(messagePart, character2Number):
Expand All @@ -25,7 +25,7 @@ def __decryptPart(messagePart, character2Number):
tmp += digit
if len(tmp) == len(messagePart):
result.append(tmp)
tmp = ""
tmp = ""

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

Expand All @@ -48,7 +48,7 @@ def __prepare(message, alphabet):
for letter, number in zip(alphabet, numbers):
character2Number[letter] = number
number2Character[number] = letter

return message, alphabet, character2Number, number2Character

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

for i in range(0, len(message)+1, period):
encrypted_numeric += __encryptPart(message[i:i+period], character2Number)

for i in range(0, len(encrypted_numeric), 3):
encrypted += number2Character[encrypted_numeric[i:i+3]]

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

for i in range(0, len(message)+1, period):
a,b,c = __decryptPart(message[i:i+period], character2Number)

for j in range(0, len(a)):
decrypted_numeric.append(a[j]+b[j]+c[j])

Expand All @@ -83,4 +83,4 @@ def decryptMessage(message, alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.", period=5):
msg = "DEFEND THE EAST WALL OF THE CASTLE."
encrypted = encryptMessage(msg,"EPSDUCVWYM.ZLKXNBTFGORIJHAQ")
decrypted = decryptMessage(encrypted, "EPSDUCVWYM.ZLKXNBTFGORIJHAQ")
print("Encrypted: {}\nDecrypted: {}".format(encrypted, decrypted))
print ("Encrypted: {}\nDecrypted: {}".format(encrypted, decrypted))
18 changes: 9 additions & 9 deletions ciphers/xor_cipher.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def decrypt_string(self,content,key = 0):

# This will be returned
ans = ""

for ch in content:
ans += chr(ord(ch) ^ key)

Expand Down Expand Up @@ -188,22 +188,22 @@ def decrypt_file(self,file, key):
# key = 67

# # test enrcypt
# print(crypt.encrypt("hallo welt",key))
# print crypt.encrypt("hallo welt",key)
# # test decrypt
# print(crypt.decrypt(crypt.encrypt("hallo welt",key), key))
# print crypt.decrypt(crypt.encrypt("hallo welt",key), key)

# # test encrypt_string
# print(crypt.encrypt_string("hallo welt",key))
# print crypt.encrypt_string("hallo welt",key)

# # test decrypt_string
# print(crypt.decrypt_string(crypt.encrypt_string("hallo welt",key),key))
# print crypt.decrypt_string(crypt.encrypt_string("hallo welt",key),key)

# if (crypt.encrypt_file("test.txt",key)):
# print("encrypt successful")
# print "encrypt successful"
# else:
# print("encrypt unsuccessful")
# print "encrypt unsuccessful"

# if (crypt.decrypt_file("encrypt.out",key)):
# print("decrypt successful")
# print "decrypt successful"
# else:
# print("decrypt unsuccessful")
# print "decrypt unsuccessful"
12 changes: 6 additions & 6 deletions data_structures/binary_tree/fenwick_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ def query(self, i): # query cumulative data from index 0 to i in O(lg N)
ret += self.ft[i]
i -= i & (-i)
return ret

if __name__ == '__main__':
f = FenwickTree(100)
f.update(1,20)
f.update(4,4)
print(f.query(1))
print(f.query(3))
print(f.query(4))
print (f.query(1))
print (f.query(3))
print (f.query(4))
f.update(2,-5)
print(f.query(1))
print(f.query(3))
print (f.query(1))
print (f.query(3))
18 changes: 9 additions & 9 deletions data_structures/binary_tree/lazy_segment_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
import math

class SegmentTree:

def __init__(self, N):
self.N = N
self.st = [0 for i in range(0,4*N)] # approximate the overall size of segment tree with array N
self.lazy = [0 for i in range(0,4*N)] # create array to store lazy update
self.flag = [0 for i in range(0,4*N)] # flag for lazy update

def left(self, idx):
return idx*2

Expand All @@ -34,7 +34,7 @@ def update(self, idx, l, r, a, b, val): # update(1, 1, N, a, b, v) for update va
self.lazy[self.right(idx)] = self.lazy[idx]
self.flag[self.left(idx)] = True
self.flag[self.right(idx)] = True

if r < a or l > b:
return True
if l >= a and r <= b :
Expand Down Expand Up @@ -74,18 +74,18 @@ def showData(self):
showList = []
for i in range(1,N+1):
showList += [self.query(1, 1, self.N, i, i)]
print(showList)

print (showList)

if __name__ == '__main__':
A = [1,2,-4,7,3,-5,6,11,-20,9,14,15,5,2,-8]
N = 15
segt = SegmentTree(N)
segt.build(1,1,N,A)
print(segt.query(1,1,N,4,6))
print(segt.query(1,1,N,7,11))
print(segt.query(1,1,N,7,12))
print (segt.query(1,1,N,4,6))
print (segt.query(1,1,N,7,11))
print (segt.query(1,1,N,7,12))
segt.update(1,1,N,1,3,111)
print(segt.query(1,1,N,1,15))
print (segt.query(1,1,N,1,15))
segt.update(1,1,N,7,8,235)
segt.showData()
20 changes: 10 additions & 10 deletions data_structures/binary_tree/segment_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
import math

class SegmentTree:

def __init__(self, A):
self.N = len(A)
self.st = [0] * (4 * self.N) # approximate the overall size of segment tree with array N
self.build(1, 0, self.N - 1)

def left(self, idx):
return idx * 2

Expand All @@ -22,10 +22,10 @@ def build(self, idx, l, r):
self.build(self.left(idx), l, mid)
self.build(self.right(idx), mid + 1, r)
self.st[idx] = max(self.st[self.left(idx)] , self.st[self.right(idx)])

def update(self, a, b, val):
return self.update_recursive(1, 0, self.N - 1, a - 1, b - 1, val)

def update_recursive(self, idx, l, r, a, b, val): # update(1, 1, N, a, b, v) for update val v to [a,b]
if r < a or l > b:
return True
Expand Down Expand Up @@ -55,17 +55,17 @@ def showData(self):
showList = []
for i in range(1,N+1):
showList += [self.query(i, i)]
print(showList)

print (showList)

if __name__ == '__main__':
A = [1,2,-4,7,3,-5,6,11,-20,9,14,15,5,2,-8]
N = 15
segt = SegmentTree(A)
print(segt.query(4, 6))
print(segt.query(7, 11))
print(segt.query(7, 12))
print (segt.query(4, 6))
print (segt.query(7, 11))
print (segt.query(7, 12))
segt.update(1,3,111)
print(segt.query(1, 15))
print (segt.query(1, 15))
segt.update(7,8,235)
segt.showData()
42 changes: 21 additions & 21 deletions data_structures/queue/double_ended_queue.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
from __future__ import print_function
# Python code to demonstrate working of
# Python code to demonstrate working of
# extend(), extendleft(), rotate(), reverse()

# importing "collections" for deque operations
import collections

# initializing deque
de = collections.deque([1, 2, 3,])

# using extend() to add numbers to right end
# using extend() to add numbers to right end
# adds 4,5,6 to right end
de.extend([4,5,6])

# printing modified deque
print("The deque after extending deque at end is : ")
print(de)

# using extendleft() to add numbers to left end
print ("The deque after extending deque at end is : ")
print (de)
# using extendleft() to add numbers to left end
# adds 7,8,9 to right end
de.extendleft([7,8,9])

# printing modified deque
print("The deque after extending deque at beginning is : ")
print(de)

print ("The deque after extending deque at beginning is : ")
print (de)
# using rotate() to rotate the deque
# rotates by 3 to left
de.rotate(-3)

# printing modified deque
print("The deque after rotating deque is : ")
print(de)

print ("The deque after rotating deque is : ")
print (de)
# using reverse() to reverse the deque
de.reverse()

# printing modified deque
print("The deque after reversing deque is : ")
print(de)
print ("The deque after reversing deque is : ")
print (de)
Loading