mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-12 23:52:17 +08:00
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:
@ -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()
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
@ -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:
|
||||
|
@ -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)
|
||||
|
@ -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()
|
||||
|
Reference in New Issue
Block a user