mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-05 17:34:49 +08:00
Add pep8-naming to pre-commit hooks and fixes incorrect naming conventions (#7062)
* ci(pre-commit): Add pep8-naming to `pre-commit` hooks (#7038) * refactor: Fix naming conventions (#7038) * Update arithmetic_analysis/lu_decomposition.py Co-authored-by: Christian Clauss <cclauss@me.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * refactor(lu_decomposition): Replace `NDArray` with `ArrayLike` (#7038) * chore: Fix naming conventions in doctests (#7038) * fix: Temporarily disable project euler problem 104 (#7069) * chore: Fix naming conventions in doctests (#7038) Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
@ -12,7 +12,7 @@ import random
|
||||
from typing import Any
|
||||
|
||||
|
||||
class my_queue:
|
||||
class MyQueue:
|
||||
def __init__(self) -> None:
|
||||
self.data: list[Any] = []
|
||||
self.head: int = 0
|
||||
@ -39,20 +39,20 @@ class my_queue:
|
||||
print(self.data[self.head : self.tail])
|
||||
|
||||
|
||||
class my_node:
|
||||
class MyNode:
|
||||
def __init__(self, data: Any) -> None:
|
||||
self.data = data
|
||||
self.left: my_node | None = None
|
||||
self.right: my_node | None = None
|
||||
self.left: MyNode | None = None
|
||||
self.right: MyNode | None = None
|
||||
self.height: int = 1
|
||||
|
||||
def get_data(self) -> Any:
|
||||
return self.data
|
||||
|
||||
def get_left(self) -> my_node | None:
|
||||
def get_left(self) -> MyNode | None:
|
||||
return self.left
|
||||
|
||||
def get_right(self) -> my_node | None:
|
||||
def get_right(self) -> MyNode | None:
|
||||
return self.right
|
||||
|
||||
def get_height(self) -> int:
|
||||
@ -62,11 +62,11 @@ class my_node:
|
||||
self.data = data
|
||||
return
|
||||
|
||||
def set_left(self, node: my_node | None) -> None:
|
||||
def set_left(self, node: MyNode | None) -> None:
|
||||
self.left = node
|
||||
return
|
||||
|
||||
def set_right(self, node: my_node | None) -> None:
|
||||
def set_right(self, node: MyNode | None) -> None:
|
||||
self.right = node
|
||||
return
|
||||
|
||||
@ -75,7 +75,7 @@ class my_node:
|
||||
return
|
||||
|
||||
|
||||
def get_height(node: my_node | None) -> int:
|
||||
def get_height(node: MyNode | None) -> int:
|
||||
if node is None:
|
||||
return 0
|
||||
return node.get_height()
|
||||
@ -87,7 +87,7 @@ def my_max(a: int, b: int) -> int:
|
||||
return b
|
||||
|
||||
|
||||
def right_rotation(node: my_node) -> my_node:
|
||||
def right_rotation(node: MyNode) -> MyNode:
|
||||
r"""
|
||||
A B
|
||||
/ \ / \
|
||||
@ -110,7 +110,7 @@ def right_rotation(node: my_node) -> my_node:
|
||||
return ret
|
||||
|
||||
|
||||
def left_rotation(node: my_node) -> my_node:
|
||||
def left_rotation(node: MyNode) -> MyNode:
|
||||
"""
|
||||
a mirror symmetry rotation of the left_rotation
|
||||
"""
|
||||
@ -126,7 +126,7 @@ def left_rotation(node: my_node) -> my_node:
|
||||
return ret
|
||||
|
||||
|
||||
def lr_rotation(node: my_node) -> my_node:
|
||||
def lr_rotation(node: MyNode) -> MyNode:
|
||||
r"""
|
||||
A A Br
|
||||
/ \ / \ / \
|
||||
@ -143,16 +143,16 @@ def lr_rotation(node: my_node) -> my_node:
|
||||
return right_rotation(node)
|
||||
|
||||
|
||||
def rl_rotation(node: my_node) -> my_node:
|
||||
def rl_rotation(node: MyNode) -> MyNode:
|
||||
right_child = node.get_right()
|
||||
assert right_child is not None
|
||||
node.set_right(right_rotation(right_child))
|
||||
return left_rotation(node)
|
||||
|
||||
|
||||
def insert_node(node: my_node | None, data: Any) -> my_node | None:
|
||||
def insert_node(node: MyNode | None, data: Any) -> MyNode | None:
|
||||
if node is None:
|
||||
return my_node(data)
|
||||
return MyNode(data)
|
||||
if data < node.get_data():
|
||||
node.set_left(insert_node(node.get_left(), data))
|
||||
if (
|
||||
@ -180,7 +180,7 @@ def insert_node(node: my_node | None, data: Any) -> my_node | None:
|
||||
return node
|
||||
|
||||
|
||||
def get_rightMost(root: my_node) -> Any:
|
||||
def get_right_most(root: MyNode) -> Any:
|
||||
while True:
|
||||
right_child = root.get_right()
|
||||
if right_child is None:
|
||||
@ -189,7 +189,7 @@ def get_rightMost(root: my_node) -> Any:
|
||||
return root.get_data()
|
||||
|
||||
|
||||
def get_leftMost(root: my_node) -> Any:
|
||||
def get_left_most(root: MyNode) -> Any:
|
||||
while True:
|
||||
left_child = root.get_left()
|
||||
if left_child is None:
|
||||
@ -198,12 +198,12 @@ def get_leftMost(root: my_node) -> Any:
|
||||
return root.get_data()
|
||||
|
||||
|
||||
def del_node(root: my_node, data: Any) -> my_node | None:
|
||||
def del_node(root: MyNode, data: Any) -> MyNode | None:
|
||||
left_child = root.get_left()
|
||||
right_child = root.get_right()
|
||||
if root.get_data() == data:
|
||||
if left_child is not None and right_child is not None:
|
||||
temp_data = get_leftMost(right_child)
|
||||
temp_data = get_left_most(right_child)
|
||||
root.set_data(temp_data)
|
||||
root.set_right(del_node(right_child, temp_data))
|
||||
elif left_child is not None:
|
||||
@ -276,7 +276,7 @@ class AVLtree:
|
||||
"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.root: my_node | None = None
|
||||
self.root: MyNode | None = None
|
||||
|
||||
def get_height(self) -> int:
|
||||
return get_height(self.root)
|
||||
@ -296,7 +296,7 @@ class AVLtree:
|
||||
self,
|
||||
) -> str: # a level traversale, gives a more intuitive look on the tree
|
||||
output = ""
|
||||
q = my_queue()
|
||||
q = MyQueue()
|
||||
q.push(self.root)
|
||||
layer = self.get_height()
|
||||
if layer == 0:
|
||||
|
@ -37,14 +37,14 @@ class SegmentTree:
|
||||
return idx * 2 + 1
|
||||
|
||||
def build(
|
||||
self, idx: int, left_element: int, right_element: int, A: list[int]
|
||||
self, idx: int, left_element: int, right_element: int, a: list[int]
|
||||
) -> None:
|
||||
if left_element == right_element:
|
||||
self.segment_tree[idx] = A[left_element - 1]
|
||||
self.segment_tree[idx] = a[left_element - 1]
|
||||
else:
|
||||
mid = (left_element + right_element) // 2
|
||||
self.build(self.left(idx), left_element, mid, A)
|
||||
self.build(self.right(idx), mid + 1, right_element, A)
|
||||
self.build(self.left(idx), left_element, mid, a)
|
||||
self.build(self.right(idx), mid + 1, right_element, a)
|
||||
self.segment_tree[idx] = max(
|
||||
self.segment_tree[self.left(idx)], self.segment_tree[self.right(idx)]
|
||||
)
|
||||
|
@ -2,8 +2,8 @@ import math
|
||||
|
||||
|
||||
class SegmentTree:
|
||||
def __init__(self, A):
|
||||
self.N = len(A)
|
||||
def __init__(self, a):
|
||||
self.N = len(a)
|
||||
self.st = [0] * (
|
||||
4 * self.N
|
||||
) # approximate the overall size of segment tree with array N
|
||||
@ -58,11 +58,11 @@ class SegmentTree:
|
||||
q2 = self.query_recursive(self.right(idx), mid + 1, r, a, b)
|
||||
return max(q1, q2)
|
||||
|
||||
def showData(self):
|
||||
showList = []
|
||||
def show_data(self):
|
||||
show_list = []
|
||||
for i in range(1, N + 1):
|
||||
showList += [self.query(i, i)]
|
||||
print(showList)
|
||||
show_list += [self.query(i, i)]
|
||||
print(show_list)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
@ -75,4 +75,4 @@ if __name__ == "__main__":
|
||||
segt.update(1, 3, 111)
|
||||
print(segt.query(1, 15))
|
||||
segt.update(7, 8, 235)
|
||||
segt.showData()
|
||||
segt.show_data()
|
||||
|
@ -121,28 +121,28 @@ def inorder(root: Node | None) -> None:
|
||||
inorder(root.right)
|
||||
|
||||
|
||||
def interactTreap(root: Node | None, args: str) -> Node | None:
|
||||
def interact_treap(root: Node | None, args: str) -> Node | None:
|
||||
"""
|
||||
Commands:
|
||||
+ value to add value into treap
|
||||
- value to erase all nodes with value
|
||||
|
||||
>>> root = interactTreap(None, "+1")
|
||||
>>> root = interact_treap(None, "+1")
|
||||
>>> inorder(root)
|
||||
1,
|
||||
>>> root = interactTreap(root, "+3 +5 +17 +19 +2 +16 +4 +0")
|
||||
>>> root = interact_treap(root, "+3 +5 +17 +19 +2 +16 +4 +0")
|
||||
>>> inorder(root)
|
||||
0,1,2,3,4,5,16,17,19,
|
||||
>>> root = interactTreap(root, "+4 +4 +4")
|
||||
>>> root = interact_treap(root, "+4 +4 +4")
|
||||
>>> inorder(root)
|
||||
0,1,2,3,4,4,4,4,5,16,17,19,
|
||||
>>> root = interactTreap(root, "-0")
|
||||
>>> root = interact_treap(root, "-0")
|
||||
>>> inorder(root)
|
||||
1,2,3,4,4,4,4,5,16,17,19,
|
||||
>>> root = interactTreap(root, "-4")
|
||||
>>> root = interact_treap(root, "-4")
|
||||
>>> inorder(root)
|
||||
1,2,3,5,16,17,19,
|
||||
>>> root = interactTreap(root, "=0")
|
||||
>>> root = interact_treap(root, "=0")
|
||||
Unknown command
|
||||
"""
|
||||
for arg in args.split():
|
||||
@ -168,7 +168,7 @@ def main() -> None:
|
||||
|
||||
args = input()
|
||||
while args != "q":
|
||||
root = interactTreap(root, args)
|
||||
root = interact_treap(root, args)
|
||||
print(root)
|
||||
args = input()
|
||||
|
||||
|
@ -52,14 +52,14 @@ class MinHeap:
|
||||
return self.heap_dict[key]
|
||||
|
||||
def build_heap(self, array):
|
||||
lastIdx = len(array) - 1
|
||||
startFrom = self.get_parent_idx(lastIdx)
|
||||
last_idx = len(array) - 1
|
||||
start_from = self.get_parent_idx(last_idx)
|
||||
|
||||
for idx, i in enumerate(array):
|
||||
self.idx_of_element[i] = idx
|
||||
self.heap_dict[i.name] = i.val
|
||||
|
||||
for i in range(startFrom, -1, -1):
|
||||
for i in range(start_from, -1, -1):
|
||||
self.sift_down(i, array)
|
||||
return array
|
||||
|
||||
@ -123,12 +123,12 @@ class MinHeap:
|
||||
def is_empty(self):
|
||||
return True if len(self.heap) == 0 else False
|
||||
|
||||
def decrease_key(self, node, newValue):
|
||||
def decrease_key(self, node, new_value):
|
||||
assert (
|
||||
self.heap[self.idx_of_element[node]].val > newValue
|
||||
self.heap[self.idx_of_element[node]].val > new_value
|
||||
), "newValue must be less that current value"
|
||||
node.val = newValue
|
||||
self.heap_dict[node.name] = newValue
|
||||
node.val = new_value
|
||||
self.heap_dict[node.name] = new_value
|
||||
self.sift_up(self.idx_of_element[node])
|
||||
|
||||
|
||||
@ -143,7 +143,7 @@ e = Node("E", 4)
|
||||
# Use one of these two ways to generate Min-Heap
|
||||
|
||||
# Generating Min-Heap from array
|
||||
myMinHeap = MinHeap([r, b, a, x, e])
|
||||
my_min_heap = MinHeap([r, b, a, x, e])
|
||||
|
||||
# Generating Min-Heap by Insert method
|
||||
# myMinHeap.insert(a)
|
||||
@ -154,14 +154,14 @@ myMinHeap = MinHeap([r, b, a, x, e])
|
||||
|
||||
# Before
|
||||
print("Min Heap - before decrease key")
|
||||
for i in myMinHeap.heap:
|
||||
for i in my_min_heap.heap:
|
||||
print(i)
|
||||
|
||||
print("Min Heap - After decrease key of node [B -> -17]")
|
||||
myMinHeap.decrease_key(b, -17)
|
||||
my_min_heap.decrease_key(b, -17)
|
||||
|
||||
# After
|
||||
for i in myMinHeap.heap:
|
||||
for i in my_min_heap.heap:
|
||||
print(i)
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
@ -15,9 +15,9 @@ Enter an Infix Equation = a + b ^c
|
||||
"""
|
||||
|
||||
|
||||
def infix_2_postfix(Infix):
|
||||
Stack = []
|
||||
Postfix = []
|
||||
def infix_2_postfix(infix):
|
||||
stack = []
|
||||
post_fix = []
|
||||
priority = {
|
||||
"^": 3,
|
||||
"*": 2,
|
||||
@ -26,7 +26,7 @@ def infix_2_postfix(Infix):
|
||||
"+": 1,
|
||||
"-": 1,
|
||||
} # Priority of each operator
|
||||
print_width = len(Infix) if (len(Infix) > 7) else 7
|
||||
print_width = len(infix) if (len(infix) > 7) else 7
|
||||
|
||||
# Print table header for output
|
||||
print(
|
||||
@ -37,52 +37,52 @@ def infix_2_postfix(Infix):
|
||||
)
|
||||
print("-" * (print_width * 3 + 7))
|
||||
|
||||
for x in Infix:
|
||||
for x in infix:
|
||||
if x.isalpha() or x.isdigit():
|
||||
Postfix.append(x) # if x is Alphabet / Digit, add it to Postfix
|
||||
post_fix.append(x) # if x is Alphabet / Digit, add it to Postfix
|
||||
elif x == "(":
|
||||
Stack.append(x) # if x is "(" push to Stack
|
||||
stack.append(x) # if x is "(" push to Stack
|
||||
elif x == ")": # if x is ")" pop stack until "(" is encountered
|
||||
while Stack[-1] != "(":
|
||||
Postfix.append(Stack.pop()) # Pop stack & add the content to Postfix
|
||||
Stack.pop()
|
||||
while stack[-1] != "(":
|
||||
post_fix.append(stack.pop()) # Pop stack & add the content to Postfix
|
||||
stack.pop()
|
||||
else:
|
||||
if len(Stack) == 0:
|
||||
Stack.append(x) # If stack is empty, push x to stack
|
||||
if len(stack) == 0:
|
||||
stack.append(x) # If stack is empty, push x to stack
|
||||
else: # while priority of x is not > priority of element in the stack
|
||||
while len(Stack) > 0 and priority[x] <= priority[Stack[-1]]:
|
||||
Postfix.append(Stack.pop()) # pop stack & add to Postfix
|
||||
Stack.append(x) # push x to stack
|
||||
while len(stack) > 0 and priority[x] <= priority[stack[-1]]:
|
||||
post_fix.append(stack.pop()) # pop stack & add to Postfix
|
||||
stack.append(x) # push x to stack
|
||||
|
||||
print(
|
||||
x.center(8),
|
||||
("".join(Stack)).ljust(print_width),
|
||||
("".join(Postfix)).ljust(print_width),
|
||||
("".join(stack)).ljust(print_width),
|
||||
("".join(post_fix)).ljust(print_width),
|
||||
sep=" | ",
|
||||
) # Output in tabular format
|
||||
|
||||
while len(Stack) > 0: # while stack is not empty
|
||||
Postfix.append(Stack.pop()) # pop stack & add to Postfix
|
||||
while len(stack) > 0: # while stack is not empty
|
||||
post_fix.append(stack.pop()) # pop stack & add to Postfix
|
||||
print(
|
||||
" ".center(8),
|
||||
("".join(Stack)).ljust(print_width),
|
||||
("".join(Postfix)).ljust(print_width),
|
||||
("".join(stack)).ljust(print_width),
|
||||
("".join(post_fix)).ljust(print_width),
|
||||
sep=" | ",
|
||||
) # Output in tabular format
|
||||
|
||||
return "".join(Postfix) # return Postfix as str
|
||||
return "".join(post_fix) # return Postfix as str
|
||||
|
||||
|
||||
def infix_2_prefix(Infix):
|
||||
Infix = list(Infix[::-1]) # reverse the infix equation
|
||||
def infix_2_prefix(infix):
|
||||
infix = list(infix[::-1]) # reverse the infix equation
|
||||
|
||||
for i in range(len(Infix)):
|
||||
if Infix[i] == "(":
|
||||
Infix[i] = ")" # change "(" to ")"
|
||||
elif Infix[i] == ")":
|
||||
Infix[i] = "(" # change ")" to "("
|
||||
for i in range(len(infix)):
|
||||
if infix[i] == "(":
|
||||
infix[i] = ")" # change "(" to ")"
|
||||
elif infix[i] == ")":
|
||||
infix[i] = "(" # change ")" to "("
|
||||
|
||||
return (infix_2_postfix("".join(Infix)))[
|
||||
return (infix_2_postfix("".join(infix)))[
|
||||
::-1
|
||||
] # call infix_2_postfix on Infix, return reverse of Postfix
|
||||
|
||||
|
@ -20,49 +20,49 @@ Enter a Postfix Equation (space separated) = 5 6 9 * +
|
||||
import operator as op
|
||||
|
||||
|
||||
def Solve(Postfix):
|
||||
Stack = []
|
||||
Div = lambda x, y: int(x / y) # noqa: E731 integer division operation
|
||||
Opr = {
|
||||
def solve(post_fix):
|
||||
stack = []
|
||||
div = lambda x, y: int(x / y) # noqa: E731 integer division operation
|
||||
opr = {
|
||||
"^": op.pow,
|
||||
"*": op.mul,
|
||||
"/": Div,
|
||||
"/": div,
|
||||
"+": op.add,
|
||||
"-": op.sub,
|
||||
} # operators & their respective operation
|
||||
|
||||
# print table header
|
||||
print("Symbol".center(8), "Action".center(12), "Stack", sep=" | ")
|
||||
print("-" * (30 + len(Postfix)))
|
||||
print("-" * (30 + len(post_fix)))
|
||||
|
||||
for x in Postfix:
|
||||
for x in post_fix:
|
||||
if x.isdigit(): # if x in digit
|
||||
Stack.append(x) # append x to stack
|
||||
stack.append(x) # append x to stack
|
||||
# output in tabular format
|
||||
print(x.rjust(8), ("push(" + x + ")").ljust(12), ",".join(Stack), sep=" | ")
|
||||
print(x.rjust(8), ("push(" + x + ")").ljust(12), ",".join(stack), sep=" | ")
|
||||
else:
|
||||
B = Stack.pop() # pop stack
|
||||
b = stack.pop() # pop stack
|
||||
# output in tabular format
|
||||
print("".rjust(8), ("pop(" + B + ")").ljust(12), ",".join(Stack), sep=" | ")
|
||||
print("".rjust(8), ("pop(" + b + ")").ljust(12), ",".join(stack), sep=" | ")
|
||||
|
||||
A = Stack.pop() # pop stack
|
||||
a = stack.pop() # pop stack
|
||||
# output in tabular format
|
||||
print("".rjust(8), ("pop(" + A + ")").ljust(12), ",".join(Stack), sep=" | ")
|
||||
print("".rjust(8), ("pop(" + a + ")").ljust(12), ",".join(stack), sep=" | ")
|
||||
|
||||
Stack.append(
|
||||
str(Opr[x](int(A), int(B)))
|
||||
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),
|
||||
("push(" + a + x + b + ")").ljust(12),
|
||||
",".join(stack),
|
||||
sep=" | ",
|
||||
)
|
||||
|
||||
return int(Stack[0])
|
||||
return int(stack[0])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
Postfix = input("\n\nEnter a Postfix Equation (space separated) = ").split(" ")
|
||||
print("\n\tResult = ", Solve(Postfix))
|
||||
print("\n\tResult = ", solve(Postfix))
|
||||
|
@ -8,7 +8,7 @@ on the current day is less than or equal to its price on the given day.
|
||||
"""
|
||||
|
||||
|
||||
def calculateSpan(price, S):
|
||||
def calculation_span(price, s):
|
||||
|
||||
n = len(price)
|
||||
# Create a stack and push index of fist element to it
|
||||
@ -16,7 +16,7 @@ def calculateSpan(price, S):
|
||||
st.append(0)
|
||||
|
||||
# Span value of first element is always 1
|
||||
S[0] = 1
|
||||
s[0] = 1
|
||||
|
||||
# Calculate span values for rest of the elements
|
||||
for i in range(1, n):
|
||||
@ -30,14 +30,14 @@ def calculateSpan(price, S):
|
||||
# than all elements on left of it, i.e. price[0],
|
||||
# price[1], ..price[i-1]. Else the price[i] is
|
||||
# greater than elements after top of stack
|
||||
S[i] = i + 1 if len(st) <= 0 else (i - st[0])
|
||||
s[i] = i + 1 if len(st) <= 0 else (i - st[0])
|
||||
|
||||
# Push this element to stack
|
||||
st.append(i)
|
||||
|
||||
|
||||
# A utility function to print elements of array
|
||||
def printArray(arr, n):
|
||||
def print_array(arr, n):
|
||||
for i in range(0, n):
|
||||
print(arr[i], end=" ")
|
||||
|
||||
@ -47,7 +47,7 @@ price = [10, 4, 5, 90, 120, 80]
|
||||
S = [0 for i in range(len(price) + 1)]
|
||||
|
||||
# Fill the span values in array S[]
|
||||
calculateSpan(price, S)
|
||||
calculation_span(price, S)
|
||||
|
||||
# Print the calculated span values
|
||||
printArray(S, len(price))
|
||||
print_array(S, len(price))
|
||||
|
Reference in New Issue
Block a user