Skip to content

psf/black code formatting #1421

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

Merged
merged 3 commits into from
Oct 22, 2019
Merged
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
2 changes: 1 addition & 1 deletion ciphers/base64_cipher.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def encode_base64(text):
i = 0
while i < len(s):
if i > 0 and ((i / 3 * 4) % 76) == 0:
r = r + "\r\n" # for unix newline, put "\n"
r = r + "\r\n" # for unix newline, put "\n"

n = (s[i] << 16) + (s[i + 1] << 8) + s[i + 2]

Expand Down
12 changes: 6 additions & 6 deletions ciphers/caesar_cipher.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
def encrypt(input_string: str, key: int) -> str:
result = ''
result = ""
for x in input_string:
if not x.isalpha():
result += x
Expand All @@ -11,7 +11,7 @@ def encrypt(input_string: str, key: int) -> str:


def decrypt(input_string: str, key: int) -> str:
result = ''
result = ""
for x in input_string:
if not x.isalpha():
result += x
Expand All @@ -24,23 +24,23 @@ def decrypt(input_string: str, key: int) -> str:

def brute_force(input_string: str) -> None:
key = 1
result = ''
result = ""
while key <= 94:
for x in input_string:
indx = (ord(x) - key) % 256
if indx < 32:
indx = indx + 95
result = result + chr(indx)
print(f'Key: {key}\t| Message: {result}')
result = ''
print(f"Key: {key}\t| Message: {result}")
result = ""
key += 1
return None


def main():
while True:
print(f'{"-" * 10}\n Menu\n{"-", * 10}')
print(*["1.Encrpyt", "2.Decrypt", "3.BruteForce", "4.Quit"], sep='\n')
print(*["1.Encrpyt", "2.Decrypt", "3.BruteForce", "4.Quit"], sep="\n")
choice = input("What would you like to do?: ")
if choice not in ["1", "2", "3", "4"]:
print("Invalid choice, please enter a valid choice")
Expand Down
26 changes: 17 additions & 9 deletions data_structures/binary_tree/treap.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Node(object):
Treap's node
Treap is a binary tree by value and heap by priority
"""

def __init__(self, value: int = None):
self.value = value
self.prior = random()
Expand All @@ -20,10 +21,7 @@ def __repr__(self):
return "'%s: %.5s'" % (self.value, self.prior)
else:
return pformat(
{
"%s: %.5s"
% (self.value, self.prior): (self.left, self.right)
},
{"%s: %.5s" % (self.value, self.prior): (self.left, self.right)},
indent=1,
)

Expand All @@ -33,6 +31,7 @@ def __str__(self):
right = str(self.right or "")
return value + left + right


def split(root: Node, value: int) -> Tuple[Node, Node]:
"""
We split current tree into 2 trees with value:
Expand Down Expand Up @@ -61,12 +60,13 @@ def split(root: Node, value: int) -> Tuple[Node, Node]:
root.right, right = split(root.right, value)
return (root, right)


def merge(left: Node, right: Node) -> Node:
"""
We merge 2 trees into one.
Note: all left tree's values must be less than all right tree's
"""
if (not left) or (not right): # If one node is None, return the other
if (not left) or (not right): # If one node is None, return the other
return left or right
elif left.prior < right.prior:
"""
Expand All @@ -82,6 +82,7 @@ def merge(left: Node, right: Node) -> Node:
right.left = merge(left, right.left)
return right


def insert(root: Node, value: int) -> Node:
"""
Insert element
Expand All @@ -94,6 +95,7 @@ def insert(root: Node, value: int) -> Node:
left, right = split(root, value)
return merge(merge(left, node), right)


def erase(root: Node, value: int) -> Node:
"""
Erase element
Expand All @@ -102,15 +104,16 @@ def erase(root: Node, value: int) -> Node:
Split all nodes with values greater into right.
Merge left, right
"""
left, right = split(root, value-1)
left, right = split(root, value - 1)
_, right = split(right, value)
return merge(left, right)


def inorder(root: Node):
"""
Just recursive print of a tree
"""
if not root: # None
if not root: # None
return
else:
inorder(root.left)
Expand Down Expand Up @@ -154,21 +157,26 @@ def interactTreap(root, args):

return root


def main():
"""After each command, program prints treap"""
root = None
print("enter numbers to creat a tree, + value to add value into treap, - value to erase all nodes with value. 'q' to quit. ")
print(
"enter numbers to creat a tree, + value to add value into treap, - value to erase all nodes with value. 'q' to quit. "
)

args = input()
while args != 'q':
while args != "q":
root = interactTreap(root, args)
print(root)
args = input()

print("good by!")
pass


if __name__ == "__main__":
import doctest

doctest.testmod()
main()
67 changes: 35 additions & 32 deletions divide_and_conquer/mergesort.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,48 @@
def merge(a,b,m,e):
l=a[b:m+1]
r=a[m+1:e+1]
k=b
i=0
j=0
while i<len(l) and j<len(r):
#change sign for Descending order
if l[i]<r[j]:
a[k]=l[i]
i+=1
def merge(a, b, m, e):
l = a[b : m + 1]
r = a[m + 1 : e + 1]
k = b
i = 0
j = 0
while i < len(l) and j < len(r):
# change sign for Descending order
if l[i] < r[j]:
a[k] = l[i]
i += 1
else:
a[k]=r[j]
j+=1
k+=1
while i<len(l):
a[k]=l[i]
i+=1
k+=1
while j<len(r):
a[k]=r[j]
j+=1
k+=1
a[k] = r[j]
j += 1
k += 1
while i < len(l):
a[k] = l[i]
i += 1
k += 1
while j < len(r):
a[k] = r[j]
j += 1
k += 1
return a

def mergesort(a,b,e):


def mergesort(a, b, e):
"""
>>> mergesort([3,2,1],0,2)
[1, 2, 3]
>>> mergesort([3,2,1,0,1,2,3,5,4],0,8)
[0, 1, 1, 2, 2, 3, 3, 4, 5]
"""
if b<e:
m = (b+e)//2
#print("ms1",a,b,m)
mergesort(a,b,m)
#print("ms2",a,m+1,e)
mergesort(a,m+1,e)
#print("m",a,b,m,e)
merge(a,b,m,e)
if b < e:
m = (b + e) // 2
# print("ms1",a,b,m)
mergesort(a, b, m)
# print("ms2",a,m+1,e)
mergesort(a, m + 1, e)
# print("m",a,b,m,e)
merge(a, b, m, e)
return a


if __name__ == "__main__":
import doctest

doctest.testmod()
1 change: 1 addition & 0 deletions dynamic_programming/abbreviation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
daBcd -> capitalize a and c(dABCd) -> remove d (ABC)
"""


def abbr(a, b):
"""
>>> abbr("daBcd", "ABC")
Expand Down
2 changes: 1 addition & 1 deletion dynamic_programming/fibonacci.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def get(self, sequence_no=None):
print("\nInvalid input, please try again.")
except NameError:
print("\n********* Invalid input, good bye!! ************\n")

import doctest

doctest.testmod()
1 change: 1 addition & 0 deletions dynamic_programming/fractional_knapsack.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def fracKnapsack(vl, wt, W, n):
else sum(vl[:k])
)


if __name__ == "__main__":
import doctest

Expand Down
2 changes: 1 addition & 1 deletion dynamic_programming/longest_common_subsequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def longest_common_subsequence(x: str, y: str):
expected_subseq = "GTAB"

ln, subseq = longest_common_subsequence(a, b)
## print("len =", ln, ", sub-sequence =", subseq)
## print("len =", ln, ", sub-sequence =", subseq)
import doctest

doctest.testmod()
1 change: 1 addition & 0 deletions dynamic_programming/sum_of_subset.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def isSumSubset(arr, arrLen, requiredSum):
# print(subset[i])
print(subset[arrLen][requiredSum])


if __name__ == "__main__":
import doctest

Expand Down
Loading