Tighten up psf/black and flake8 (#2024)

* Tighten up psf/black and flake8

* Fix some tests

* Fix some E741

* Fix some E741

* updating DIRECTORY.md

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
Christian Clauss
2020-05-22 08:10:11 +02:00
committed by GitHub
parent 21ed8968c0
commit 1f8a21d727
124 changed files with 583 additions and 495 deletions

View File

@@ -89,8 +89,8 @@ def leftrotation(node):
Bl Br UB Br C
/
UB
UB = unbalanced node
UB = unbalanced node
"""
print("left rotation node:", node.getdata())
ret = node.getleft()
@@ -120,11 +120,11 @@ def rightrotation(node):
def rlrotation(node):
r"""
A A Br
A A Br
/ \ / \ / \
B C RR Br C LR B A
/ \ --> / \ --> / / \
Bl Br B UB Bl UB C
Bl Br B UB Bl UB C
\ /
UB Bl
RR = rightrotation LR = leftrotation
@@ -276,13 +276,13 @@ class AVLtree:
if __name__ == "__main__":
t = AVLtree()
t.traversale()
l = list(range(10))
random.shuffle(l)
for i in l:
lst = list(range(10))
random.shuffle(lst)
for i in lst:
t.insert(i)
t.traversale()
random.shuffle(l)
for i in l:
random.shuffle(lst)
for i in lst:
t.del_node(i)
t.traversale()

View File

@@ -1,4 +1,9 @@
class Node: # This is the Class Node with a constructor that contains data variable to type data and left, right pointers.
class Node:
"""
This is the Class Node with a constructor that contains data variable to type data
and left, right pointers.
"""
def __init__(self, data):
self.data = data
self.left = None

View File

@@ -16,8 +16,8 @@ class SegmentTree:
def right(self, idx):
return idx * 2 + 1
def build(self, idx, l, r, A):
if l == r:
def build(self, idx, l, r, A): # noqa: E741
if l == r: # noqa: E741
self.st[idx] = A[l - 1]
else:
mid = (l + r) // 2
@@ -25,14 +25,16 @@ class SegmentTree:
self.build(self.right(idx), mid + 1, r, A)
self.st[idx] = max(self.st[self.left(idx)], self.st[self.right(idx)])
# update with O(lg N) (Normal segment tree without lazy update will take O(Nlg N) for each update)
def update(
self, idx, l, r, a, b, val
): # update(1, 1, N, a, b, v) for update val v to [a,b]
if self.flag[idx] == True:
# update with O(lg N) (Normal segment tree without lazy update will take O(Nlg N)
# for each update)
def update(self, idx, l, r, a, b, val): # noqa: E741
"""
update(1, 1, N, a, b, v) for update val v to [a,b]
"""
if self.flag[idx] is True:
self.st[idx] = self.lazy[idx]
self.flag[idx] = False
if l != r:
if l != r: # noqa: E741
self.lazy[self.left(idx)] = self.lazy[idx]
self.lazy[self.right(idx)] = self.lazy[idx]
self.flag[self.left(idx)] = True
@@ -40,9 +42,9 @@ class SegmentTree:
if r < a or l > b:
return True
if l >= a and r <= b:
if l >= a and r <= b: # noqa: E741
self.st[idx] = val
if l != r:
if l != r: # noqa: E741
self.lazy[self.left(idx)] = val
self.lazy[self.right(idx)] = val
self.flag[self.left(idx)] = True
@@ -55,18 +57,21 @@ class SegmentTree:
return True
# query with O(lg N)
def query(self, idx, l, r, a, b): # query(1, 1, N, a, b) for query max of [a,b]
if self.flag[idx] == True:
def query(self, idx, l, r, a, b): # noqa: E741
"""
query(1, 1, N, a, b) for query max of [a,b]
"""
if self.flag[idx] is True:
self.st[idx] = self.lazy[idx]
self.flag[idx] = False
if l != r:
if l != r: # noqa: E741
self.lazy[self.left(idx)] = self.lazy[idx]
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 -math.inf
if l >= a and r <= b:
if l >= a and r <= b: # noqa: E741
return self.st[idx]
mid = (l + r) // 2
q1 = self.query(self.left(idx), l, mid, a, b)

View File

@@ -1,6 +1,7 @@
"""
A non-recursive Segment Tree implementation with range query and single element update,
works virtually with any list of the same type of elements with a "commutative" combiner.
works virtually with any list of the same type of elements with a "commutative"
combiner.
Explanation:
https://www.geeksforgeeks.org/iterative-segment-tree-range-minimum-query/
@@ -22,7 +23,8 @@ https://www.geeksforgeeks.org/segment-tree-efficient-implementation/
>>> st.update(4, 1)
>>> st.query(3, 4)
0
>>> st = SegmentTree([[1, 2, 3], [3, 2, 1], [1, 1, 1]], lambda a, b: [a[i] + b[i] for i in range(len(a))])
>>> st = SegmentTree([[1, 2, 3], [3, 2, 1], [1, 1, 1]], lambda a, b: [a[i] + b[i] for i
... in range(len(a))])
>>> st.query(0, 1)
[4, 4, 4]
>>> st.query(1, 2)
@@ -47,7 +49,8 @@ class SegmentTree:
>>> SegmentTree(['a', 'b', 'c'], lambda a, b: '{}{}'.format(a, b)).query(0, 2)
'abc'
>>> SegmentTree([(1, 2), (2, 3), (3, 4)], lambda a, b: (a[0] + b[0], a[1] + b[1])).query(0, 2)
>>> SegmentTree([(1, 2), (2, 3), (3, 4)],
... lambda a, b: (a[0] + b[0], a[1] + b[1])).query(0, 2)
(6, 9)
"""
self.N = len(arr)
@@ -78,7 +81,7 @@ class SegmentTree:
p = p // 2
self.st[p] = self.fn(self.st[p * 2], self.st[p * 2 + 1])
def query(self, l: int, r: int) -> T:
def query(self, l: int, r: int) -> T: # noqa: E741
"""
Get range query value in log(N) time
:param l: left element index
@@ -95,9 +98,9 @@ class SegmentTree:
>>> st.query(2, 3)
7
"""
l, r = l + self.N, r + self.N
l, r = l + self.N, r + self.N # noqa: E741
res = None
while l <= r:
while l <= r: # noqa: E741
if l % 2 == 1:
res = self.st[l] if res is None else self.fn(res, self.st[l])
if r % 2 == 0:

View File

@@ -15,8 +15,8 @@ class SegmentTree:
def right(self, idx):
return idx * 2 + 1
def build(self, idx, l, r):
if l == r:
def build(self, idx, l, r): # noqa: E741
if l == r: # noqa: E741
self.st[idx] = A[l]
else:
mid = (l + r) // 2
@@ -27,12 +27,13 @@ class SegmentTree:
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]
def update_recursive(self, idx, l, r, a, b, val): # noqa: E741
"""
update(1, 1, N, a, b, v) for update val v to [a,b]
"""
if r < a or l > b:
return True
if l == r:
if l == r: # noqa: E741
self.st[idx] = val
return True
mid = (l + r) // 2
@@ -44,12 +45,13 @@ class SegmentTree:
def query(self, a, b):
return self.query_recursive(1, 0, self.N - 1, a - 1, b - 1)
def query_recursive(
self, idx, l, r, a, b
): # query(1, 1, N, a, b) for query max of [a,b]
def query_recursive(self, idx, l, r, a, b): # noqa: E741
"""
query(1, 1, N, a, b) for query max of [a,b]
"""
if r < a or l > b:
return -math.inf
if l >= a and r <= b:
if l >= a and r <= b: # noqa: E741
return self.st[idx]
mid = (l + r) // 2
q1 = self.query_recursive(self.left(idx), l, mid, a, b)

View File

@@ -1,3 +1,5 @@
# flake8: noqa
from random import random
from typing import Tuple
@@ -161,7 +163,8 @@ def main():
"""After each command, program prints treap"""
root = None
print(
"enter numbers to create a tree, + value to add value into treap, - value to erase all nodes with value. 'q' to quit. "
"enter numbers to create a tree, + value to add value into treap, "
"- value to erase all nodes with value. 'q' to quit. "
)
args = input()

View File

@@ -5,7 +5,7 @@ from hash_table import HashTable
class QuadraticProbing(HashTable):
"""
Basic Hash Table example with open addressing using Quadratic Probing
Basic Hash Table example with open addressing using Quadratic Probing
"""
def __init__(self, *args, **kwargs):

View File

@@ -1,3 +1,5 @@
# flake8: noqa
"""
Binomial Heap
Reference: Advanced Data Structures, Peter Brass

View File

@@ -66,7 +66,7 @@ class MinHeap:
# this is min-heapify method
def sift_down(self, idx, array):
while True:
l = self.get_left_child_idx(idx)
l = self.get_left_child_idx(idx) # noqa: E741
r = self.get_right_child_idx(idx)
smallest = idx
@@ -132,7 +132,7 @@ class MinHeap:
self.sift_up(self.idx_of_element[node])
## USAGE
# USAGE
r = Node("R", -1)
b = Node("B", 6)

View File

@@ -1,5 +1,5 @@
"""
Implementing Deque using DoublyLinkedList ...
Implementing Deque using DoublyLinkedList ...
Operations:
1. insertion in the front -> O(1)
2. insertion in the end -> O(1)
@@ -61,7 +61,7 @@ class _DoublyLinkedBase:
class LinkedDeque(_DoublyLinkedBase):
def first(self):
""" return first element
""" return first element
>>> d = LinkedDeque()
>>> d.add_first('A').first()
'A'
@@ -84,7 +84,7 @@ class LinkedDeque(_DoublyLinkedBase):
raise Exception("List is empty")
return self._trailer._prev._data
### DEque Insert Operations (At the front, At the end) ###
# DEque Insert Operations (At the front, At the end)
def add_first(self, element):
""" insertion in the front
@@ -100,7 +100,7 @@ class LinkedDeque(_DoublyLinkedBase):
"""
return self._insert(self._trailer._prev, element, self._trailer)
### DEqueu Remove Operations (At the front, At the end) ###
# DEqueu Remove Operations (At the front, At the end)
def remove_first(self):
""" removal from the front

View File

@@ -43,7 +43,7 @@ class LinkedList:
-20
>>> link.middle_element()
12
>>>
>>>
"""
slow_pointer = self.head
fast_pointer = self.head

View File

@@ -22,7 +22,7 @@ import operator as op
def Solve(Postfix):
Stack = []
Div = lambda x, y: int(x / y) # integer division operation
Div = lambda x, y: int(x / y) # noqa: E731 integer division operation
Opr = {
"^": op.pow,
"*": op.mul,
@@ -38,29 +38,27 @@ def Solve(Postfix):
for x in Postfix:
if x.isdigit(): # if x in digit
Stack.append(x) # append x to stack
print(
x.rjust(8), ("push(" + x + ")").ljust(12), ",".join(Stack), sep=" | "
) # output in tabular format
# output in tabular format
print(x.rjust(8), ("push(" + x + ")").ljust(12), ",".join(Stack), sep=" | ")
else:
B = Stack.pop() # pop stack
print(
"".rjust(8), ("pop(" + B + ")").ljust(12), ",".join(Stack), sep=" | "
) # output in tabular format
# output in tabular format
print("".rjust(8), ("pop(" + B + ")").ljust(12), ",".join(Stack), sep=" | ")
A = Stack.pop() # pop stack
print(
"".rjust(8), ("pop(" + A + ")").ljust(12), ",".join(Stack), sep=" | "
) # output in tabular format
# output in tabular format
print("".rjust(8), ("pop(" + A + ")").ljust(12), ",".join(Stack), sep=" | ")
Stack.append(
str(Opr[x](int(A), int(B)))
) # evaluate the 2 values popped from stack & push result to stack
# output in tabular format
print(
x.rjust(8),
("push(" + A + x + B + ")").ljust(12),
",".join(Stack),
sep=" | ",
) # output in tabular format
)
return int(Stack[0])

View File

@@ -1,7 +1,7 @@
"""
A Trie/Prefix Tree is a kind of search tree used to provide quick lookup
of words/patterns in a set of words. A basic Trie however has O(n^2) space complexity
making it impractical in practice. It however provides O(max(search_string, length of longest word))
making it impractical in practice. It however provides O(max(search_string, length of longest word))
lookup time making it an optimal approach when space is not an issue.
"""