| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 6654e1e commit 89acf5d
13 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,25 +8,25 @@ def NewtonRaphson(func, a): | |||
| 8 | 8 | ''' Finds root from the point 'a' onwards by Newton-Raphson method ''' | |
| 9 | 9 | while True: | |
| 10 | 10 | c = Decimal(a) - ( Decimal(eval(func)) / Decimal(eval(str(diff(func)))) ) | |
| 11 | - | ||
| 11 | + | ||
| 12 | 12 | a = c | |
| 13 | 13 | ||
| 14 | 14 | # This number dictates the accuracy of the answer | |
| 15 | 15 | if abs(eval(func)) < 10**-15: | |
| 16 | 16 | return c | |
| 17 | - | ||
| 17 | + | ||
| 18 | 18 | ||
| 19 | 19 | # Let's Execute | |
| 20 | 20 | if __name__ == '__main__': | |
| 21 | 21 | # Find root of trigonometric function | |
| 22 | 22 | # Find value of pi | |
| 23 | - print ('sin(x) = 0', NewtonRaphson('sin(x)', 2)) | ||
| 24 | - | ||
| 23 | + print('sin(x) = 0', NewtonRaphson('sin(x)', 2)) | ||
| 24 | + | ||
| 25 | 25 | # 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 | + | ||
| 28 | 28 | # 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)) | ||
| 30 | 30 | ||
| 31 | 31 | # Exponential Roots | |
| 32 | - print ('exp(x) - 1 = 0', NewtonRaphson('exp(x) - 1', 0)) | ||
| 32 | + print('exp(x) - 1 = 0', NewtonRaphson('exp(x) - 1', 0)) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -41,12 +41,12 @@ def main(): | |||
| 41 | 41 | print("4.Quit") | |
| 42 | 42 | choice = input("What would you like to do?: ") | |
| 43 | 43 | 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") | ||
| 45 | 45 | elif choice == '1': | |
| 46 | 46 | strng = input("Please enter the string to be encrypted: ") | |
| 47 | 47 | key = int(input("Please enter off-set between 1-94: ")) | |
| 48 | 48 | if key in range(1, 95): | |
| 49 | - print (encrypt(strng.lower(), key)) | ||
| 49 | + print(encrypt(strng.lower(), key)) | ||
| 50 | 50 | elif choice == '2': | |
| 51 | 51 | strng = input("Please enter the string to be decrypted: ") | |
| 52 | 52 | key = int(input("Please enter off-set between 1-94: ")) | |
@@ -57,7 +57,7 @@ def main(): | |||
| 57 | 57 | brute_force(strng) | |
| 58 | 58 | main() | |
| 59 | 59 | elif choice == '4': | |
| 60 | - print ("Goodbye.") | ||
| 60 | + print("Goodbye.") | ||
| 61 | 61 | break | |
| 62 | 62 | ||
| 63 | 63 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -71,11 +71,11 @@ def decrypt(message): | |||
| 71 | 71 | def main(): | |
| 72 | 72 | message = "Morse code here" | |
| 73 | 73 | result = encrypt(message.upper()) | |
| 74 | - print (result) | ||
| 74 | + print(result) | ||
| 75 | 75 | ||
| 76 | 76 | message = result | |
| 77 | 77 | result = decrypt(message) | |
| 78 | - print (result) | ||
| 78 | + print(result) | ||
| 79 | 79 | ||
| 80 | 80 | ||
| 81 | 81 | if __name__ == '__main__': | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,15 +3,15 @@ | |||
| 3 | 3 | def __encryptPart(messagePart, character2Number): | |
| 4 | 4 | one, two, three = "", "", "" | |
| 5 | 5 | tmp = [] | |
| 6 | - | ||
| 6 | + | ||
| 7 | 7 | for character in messagePart: | |
| 8 | 8 | tmp.append(character2Number[character]) | |
| 9 | 9 | ||
| 10 | 10 | for each in tmp: | |
| 11 | 11 | one += each[0] | |
| 12 | 12 | two += each[1] | |
| 13 | 13 | three += each[2] | |
| 14 | - | ||
| 14 | + | ||
| 15 | 15 | return one+two+three | |
| 16 | 16 | ||
| 17 | 17 | def __decryptPart(messagePart, character2Number): | |
@@ -25,7 +25,7 @@ def __decryptPart(messagePart, character2Number): | |||
| 25 | 25 | tmp += digit | |
| 26 | 26 | if len(tmp) == len(messagePart): | |
| 27 | 27 | result.append(tmp) | |
| 28 | - tmp = "" | ||
| 28 | + tmp = "" | ||
| 29 | 29 | ||
| 30 | 30 | return result[0], result[1], result[2] | |
| 31 | 31 | ||
@@ -48,7 +48,7 @@ def __prepare(message, alphabet): | |||
| 48 | 48 | for letter, number in zip(alphabet, numbers): | |
| 49 | 49 | character2Number[letter] = number | |
| 50 | 50 | number2Character[number] = letter | |
| 51 | - | ||
| 51 | + | ||
| 52 | 52 | return message, alphabet, character2Number, number2Character | |
| 53 | 53 | ||
| 54 | 54 | def encryptMessage(message, alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.", period=5): | |
@@ -57,7 +57,7 @@ def encryptMessage(message, alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.", period=5): | |||
| 57 | 57 | ||
| 58 | 58 | for i in range(0, len(message)+1, period): | |
| 59 | 59 | encrypted_numeric += __encryptPart(message[i:i+period], character2Number) | |
| 60 | - | ||
| 60 | + | ||
| 61 | 61 | for i in range(0, len(encrypted_numeric), 3): | |
| 62 | 62 | encrypted += number2Character[encrypted_numeric[i:i+3]] | |
| 63 | 63 | ||
@@ -70,7 +70,7 @@ def decryptMessage(message, alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.", period=5): | |||
| 70 | 70 | ||
| 71 | 71 | for i in range(0, len(message)+1, period): | |
| 72 | 72 | a,b,c = __decryptPart(message[i:i+period], character2Number) | |
| 73 | - | ||
| 73 | + | ||
| 74 | 74 | for j in range(0, len(a)): | |
| 75 | 75 | decrypted_numeric.append(a[j]+b[j]+c[j]) | |
| 76 | 76 | ||
@@ -83,4 +83,4 @@ def decryptMessage(message, alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.", period=5): | |||
| 83 | 83 | msg = "DEFEND THE EAST WALL OF THE CASTLE." | |
| 84 | 84 | encrypted = encryptMessage(msg,"EPSDUCVWYM.ZLKXNBTFGORIJHAQ") | |
| 85 | 85 | decrypted = decryptMessage(encrypted, "EPSDUCVWYM.ZLKXNBTFGORIJHAQ") | |
| 86 | - print ("Encrypted: {}\nDecrypted: {}".format(encrypted, decrypted)) | ||
| 86 | + print("Encrypted: {}\nDecrypted: {}".format(encrypted, decrypted)) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -122,7 +122,7 @@ def decrypt_string(self,content,key = 0): | |||
| 122 | 122 | ||
| 123 | 123 | # This will be returned | |
| 124 | 124 | ans = "" | |
| 125 | - | ||
| 125 | + | ||
| 126 | 126 | for ch in content: | |
| 127 | 127 | ans += chr(ord(ch) ^ key) | |
| 128 | 128 | ||
@@ -188,22 +188,22 @@ def decrypt_file(self,file, key): | |||
| 188 | 188 | # key = 67 | |
| 189 | 189 | ||
| 190 | 190 | # # test enrcypt | |
| 191 | - # print crypt.encrypt("hallo welt",key) | ||
| 191 | + # print(crypt.encrypt("hallo welt",key)) | ||
| 192 | 192 | # # test decrypt | |
| 193 | - # print crypt.decrypt(crypt.encrypt("hallo welt",key), key) | ||
| 193 | + # print(crypt.decrypt(crypt.encrypt("hallo welt",key), key)) | ||
| 194 | 194 | ||
| 195 | 195 | # # test encrypt_string | |
| 196 | - # print crypt.encrypt_string("hallo welt",key) | ||
| 196 | + # print(crypt.encrypt_string("hallo welt",key)) | ||
| 197 | 197 | ||
| 198 | 198 | # # 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)) | ||
| 200 | 200 | ||
| 201 | 201 | # if (crypt.encrypt_file("test.txt",key)): | |
| 202 | - # print "encrypt successful" | ||
| 202 | + # print("encrypt successful") | ||
| 203 | 203 | # else: | |
| 204 | - # print "encrypt unsuccessful" | ||
| 204 | + # print("encrypt unsuccessful") | ||
| 205 | 205 | ||
| 206 | 206 | # if (crypt.decrypt_file("encrypt.out",key)): | |
| 207 | - # print "decrypt successful" | ||
| 207 | + # print("decrypt successful") | ||
| 208 | 208 | # else: | |
| 209 | - # print "decrypt unsuccessful" | ||
| 209 | + # print("decrypt unsuccessful") | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -16,14 +16,14 @@ def query(self, i): # query cumulative data from index 0 to i in O(lg N) | |||
| 16 | 16 | ret += self.ft[i] | |
| 17 | 17 | i -= i & (-i) | |
| 18 | 18 | return ret | |
| 19 | - | ||
| 19 | + | ||
| 20 | 20 | if __name__ == '__main__': | |
| 21 | 21 | f = FenwickTree(100) | |
| 22 | 22 | f.update(1,20) | |
| 23 | 23 | 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)) | ||
| 27 | 27 | f.update(2,-5) | |
| 28 | - print (f.query(1)) | ||
| 29 | - print (f.query(3)) | ||
| 28 | + print(f.query(1)) | ||
| 29 | + print(f.query(3)) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,13 +2,13 @@ | |||
| 2 | 2 | import math | |
| 3 | 3 | ||
| 4 | 4 | class SegmentTree: | |
| 5 | - | ||
| 5 | + | ||
| 6 | 6 | def __init__(self, N): | |
| 7 | 7 | self.N = N | |
| 8 | 8 | self.st = [0 for i in range(0,4*N)] # approximate the overall size of segment tree with array N | |
| 9 | 9 | self.lazy = [0 for i in range(0,4*N)] # create array to store lazy update | |
| 10 | 10 | self.flag = [0 for i in range(0,4*N)] # flag for lazy update | |
| 11 | - | ||
| 11 | + | ||
| 12 | 12 | def left(self, idx): | |
| 13 | 13 | return idx*2 | |
| 14 | 14 | ||
@@ -34,7 +34,7 @@ def update(self, idx, l, r, a, b, val): # update(1, 1, N, a, b, v) for update va | |||
| 34 | 34 | self.lazy[self.right(idx)] = self.lazy[idx] | |
| 35 | 35 | self.flag[self.left(idx)] = True | |
| 36 | 36 | self.flag[self.right(idx)] = True | |
| 37 | - | ||
| 37 | + | ||
| 38 | 38 | if r < a or l > b: | |
| 39 | 39 | return True | |
| 40 | 40 | if l >= a and r <= b : | |
@@ -74,18 +74,18 @@ def showData(self): | |||
| 74 | 74 | showList = [] | |
| 75 | 75 | for i in range(1,N+1): | |
| 76 | 76 | showList += [self.query(1, 1, self.N, i, i)] | |
| 77 | - print (showList) | ||
| 78 | - | ||
| 77 | + print(showList) | ||
| 78 | + | ||
| 79 | 79 | ||
| 80 | 80 | if __name__ == '__main__': | |
| 81 | 81 | A = [1,2,-4,7,3,-5,6,11,-20,9,14,15,5,2,-8] | |
| 82 | 82 | N = 15 | |
| 83 | 83 | segt = SegmentTree(N) | |
| 84 | 84 | 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)) | ||
| 88 | 88 | 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)) | ||
| 90 | 90 | segt.update(1,1,N,7,8,235) | |
| 91 | 91 | segt.showData() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,12 +2,12 @@ | |||
| 2 | 2 | import math | |
| 3 | 3 | ||
| 4 | 4 | class SegmentTree: | |
| 5 | - | ||
| 5 | + | ||
| 6 | 6 | def __init__(self, A): | |
| 7 | 7 | self.N = len(A) | |
| 8 | 8 | self.st = [0] * (4 * self.N) # approximate the overall size of segment tree with array N | |
| 9 | 9 | self.build(1, 0, self.N - 1) | |
| 10 | - | ||
| 10 | + | ||
| 11 | 11 | def left(self, idx): | |
| 12 | 12 | return idx * 2 | |
| 13 | 13 | ||
@@ -22,10 +22,10 @@ def build(self, idx, l, r): | |||
| 22 | 22 | self.build(self.left(idx), l, mid) | |
| 23 | 23 | self.build(self.right(idx), mid + 1, r) | |
| 24 | 24 | self.st[idx] = max(self.st[self.left(idx)] , self.st[self.right(idx)]) | |
| 25 | - | ||
| 25 | + | ||
| 26 | 26 | def update(self, a, b, val): | |
| 27 | 27 | return self.update_recursive(1, 0, self.N - 1, a - 1, b - 1, val) | |
| 28 | - | ||
| 28 | + | ||
| 29 | 29 | def update_recursive(self, idx, l, r, a, b, val): # update(1, 1, N, a, b, v) for update val v to [a,b] | |
| 30 | 30 | if r < a or l > b: | |
| 31 | 31 | return True | |
@@ -55,17 +55,17 @@ def showData(self): | |||
| 55 | 55 | showList = [] | |
| 56 | 56 | for i in range(1,N+1): | |
| 57 | 57 | showList += [self.query(i, i)] | |
| 58 | - print (showList) | ||
| 59 | - | ||
| 58 | + print(showList) | ||
| 59 | + | ||
| 60 | 60 | ||
| 61 | 61 | if __name__ == '__main__': | |
| 62 | 62 | A = [1,2,-4,7,3,-5,6,11,-20,9,14,15,5,2,-8] | |
| 63 | 63 | N = 15 | |
| 64 | 64 | 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)) | ||
| 68 | 68 | segt.update(1,3,111) | |
| 69 | - print (segt.query(1, 15)) | ||
| 69 | + print(segt.query(1, 15)) | ||
| 70 | 70 | segt.update(7,8,235) | |
| 71 | 71 | segt.showData() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,40 +1,40 @@ | |||
| 1 | 1 | from __future__ import print_function | |
| 2 | - # Python code to demonstrate working of | ||
| 2 | + # Python code to demonstrate working of | ||
| 3 | 3 | # extend(), extendleft(), rotate(), reverse() | |
| 4 | - | ||
| 4 | + | ||
| 5 | 5 | # importing "collections" for deque operations | |
| 6 | 6 | import collections | |
| 7 | - | ||
| 7 | + | ||
| 8 | 8 | # initializing deque | |
| 9 | 9 | 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 | ||
| 12 | 12 | # adds 4,5,6 to right end | |
| 13 | 13 | de.extend([4,5,6]) | |
| 14 | - | ||
| 14 | + | ||
| 15 | 15 | # 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 | ||
| 20 | 20 | # adds 7,8,9 to right end | |
| 21 | 21 | de.extendleft([7,8,9]) | |
| 22 | - | ||
| 22 | + | ||
| 23 | 23 | # 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 | + | ||
| 27 | 27 | # using rotate() to rotate the deque | |
| 28 | 28 | # rotates by 3 to left | |
| 29 | 29 | de.rotate(-3) | |
| 30 | - | ||
| 30 | + | ||
| 31 | 31 | # 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 | + | ||
| 35 | 35 | # using reverse() to reverse the deque | |
| 36 | 36 | de.reverse() | |
| 37 | - | ||
| 37 | + | ||
| 38 | 38 | # 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) | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments