Skip to content

Commit e1cae68

Browse files
github-actionsgithub-actions
github-actions
authored and
github-actions
committed
fixup! Format Python code with psf/black push
1 parent 2a69c73 commit e1cae68

File tree

6 files changed

+127
-116
lines changed

6 files changed

+127
-116
lines changed

arithmetic_analysis/newton_raphson.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from sympy import diff
99

1010

11-
def newton_raphson(func: str, a: int, precision: int=10 ** -10) -> float:
11+
def newton_raphson(func: str, a: int, precision: int = 10 ** -10) -> float:
1212
""" Finds root from the point 'a' onwards by Newton-Raphson method
1313
>>> newton_raphson("sin(x)", 2)
1414
3.1415926536808043

ciphers/affine_cipher.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33

44
import cryptomath_module as cryptomath
55

6-
SYMBOLS = (r""" !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`"""
7-
r"""abcdefghijklmnopqrstuvwxyz{|}~""")
6+
SYMBOLS = (
7+
r""" !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`"""
8+
r"""abcdefghijklmnopqrstuvwxyz{|}~"""
9+
)
810

911

1012
def main():

data_structures/binary_tree/binary_search_tree.py

+43-38
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
'''
1+
"""
22
A binary search Tree
3-
'''
3+
"""
4+
5+
46
class Node:
57
def __init__(self, value, parent):
68
self.value = value
@@ -13,16 +15,11 @@ def __repr__(self):
1315

1416
if self.left is None and self.right is None:
1517
return str(self.value)
16-
return pformat(
17-
{
18-
"%s"
19-
% (self.value): (self.left, self.right)
20-
},
21-
indent=1,
22-
)
18+
return pformat({"%s" % (self.value): (self.left, self.right)}, indent=1,)
19+
2320

2421
class BinarySearchTree:
25-
def __init__(self, root = None):
22+
def __init__(self, root=None):
2623
self.root = root
2724

2825
def __str__(self):
@@ -32,10 +29,10 @@ def __str__(self):
3229
return str(self.root)
3330

3431
def __reassign_nodes(self, node, newChildren):
35-
if(newChildren is not None): # reset its kids
32+
if newChildren is not None: # reset its kids
3633
newChildren.parent = node.parent
37-
if(node.parent is not None): # reset its parent
38-
if(self.is_right(node)): # If it is the right children
34+
if node.parent is not None: # reset its parent
35+
if self.is_right(node): # If it is the right children
3936
node.parent.right = newChildren
4037
else:
4138
node.parent.left = newChildren
@@ -55,10 +52,10 @@ def __insert(self, value):
5552
new_node = Node(value, None) # create a new Node
5653
if self.empty(): # if Tree is empty
5754
self.root = new_node # set its root
58-
else: # Tree is not empty
59-
parent_node = self.root # from root
55+
else: # Tree is not empty
56+
parent_node = self.root # from root
6057
while True: # While we don't get to a leaf
61-
if value < parent_node.value: # We go left
58+
if value < parent_node.value: # We go left
6259
if parent_node.left == None:
6360
parent_node.left = new_node # We insert the new node in a leaf
6461
break
@@ -87,60 +84,65 @@ def search(self, value):
8784
node = node.left if value < node.value else node.right
8885
return node
8986

90-
def get_max(self, node = None):
87+
def get_max(self, node=None):
9188
"""
9289
We go deep on the right branch
9390
"""
9491
if node is None:
9592
node = self.root
9693
if not self.empty():
97-
while(node.right is not None):
94+
while node.right is not None:
9895
node = node.right
9996
return node
10097

101-
def get_min(self, node = None):
98+
def get_min(self, node=None):
10299
"""
103100
We go deep on the left branch
104101
"""
105-
if(node is None):
102+
if node is None:
106103
node = self.root
107-
if(not self.empty()):
104+
if not self.empty():
108105
node = self.root
109-
while(node.left is not None):
106+
while node.left is not None:
110107
node = node.left
111108
return node
112109

113110
def remove(self, value):
114-
node = self.search(value) # Look for the node with that label
115-
if(node is not None):
116-
if(node.left is None and node.right is None): # If it has no children
111+
node = self.search(value) # Look for the node with that label
112+
if node is not None:
113+
if node.left is None and node.right is None: # If it has no children
117114
self.__reassign_nodes(node, None)
118115
node = None
119-
elif(node.left is None): # Has only right children
116+
elif node.left is None: # Has only right children
120117
self.__reassign_nodes(node, node.right)
121-
elif(node.right is None): # Has only left children
118+
elif node.right is None: # Has only left children
122119
self.__reassign_nodes(node, node.left)
123120
else:
124-
tmpNode = self.get_max(node.left) # Gets the max value of the left branch
121+
tmpNode = self.get_max(
122+
node.left
123+
) # Gets the max value of the left branch
125124
self.remove(tmpNode.value)
126-
node.value = tmpNode.value # Assigns the value to the node to delete and keesp tree structure
127-
125+
node.value = (
126+
tmpNode.value
127+
) # Assigns the value to the node to delete and keesp tree structure
128+
128129
def preorder_traverse(self, node):
129130
if node is not None:
130131
yield node # Preorder Traversal
131132
yield from self.preorder_traverse(node.left)
132133
yield from self.preorder_traverse(node.right)
133134

134-
def traversal_tree(self, traversalFunction = None):
135+
def traversal_tree(self, traversalFunction=None):
135136
"""
136137
This function traversal the tree.
137138
You can pass a function to traversal the tree as needed by client code
138139
"""
139-
if(traversalFunction is None):
140+
if traversalFunction is None:
140141
return self.preorder_traverse(self.root)
141142
else:
142143
return traversalFunction(self.root)
143144

145+
144146
def postorder(curr_node):
145147
"""
146148
postOrder (left, right, self)
@@ -150,8 +152,9 @@ def postorder(curr_node):
150152
nodeList = postorder(curr_node.left) + postorder(curr_node.right) + [curr_node]
151153
return nodeList
152154

155+
153156
def binary_search_tree():
154-
r'''
157+
r"""
155158
Example
156159
8
157160
/ \
@@ -170,36 +173,38 @@ def binary_search_tree():
170173
Traceback (most recent call last):
171174
...
172175
IndexError: Warning: Tree is empty! please use another.
173-
'''
176+
"""
174177
testlist = (8, 3, 6, 1, 10, 14, 13, 4, 7)
175178
t = BinarySearchTree()
176179
for i in testlist:
177180
t.insert(i)
178181

179182
# Prints all the elements of the list in order traversal
180183
print(t)
181-
182-
if(t.search(6) is not None):
184+
185+
if t.search(6) is not None:
183186
print("The value 6 exists")
184187
else:
185188
print("The value 6 doesn't exist")
186189

187-
if(t.search(-1) is not None):
190+
if t.search(-1) is not None:
188191
print("The value -1 exists")
189192
else:
190193
print("The value -1 doesn't exist")
191194

192-
if(not t.empty()):
195+
if not t.empty():
193196
print("Max Value: ", t.get_max().value)
194197
print("Min Value: ", t.get_min().value)
195198

196199
for i in testlist:
197200
t.remove(i)
198201
print(t)
199202

203+
200204
二叉搜索树 = binary_search_tree
201205

202206
if __name__ == "__main__":
203207
import doctest
208+
204209
doctest.testmod()
205210
binary_search_tree()
+77-74
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
# A complete working Python program to demonstrate all
2-
# stack operations using a doubly linked list
3-
4-
class Node:
5-
def __init__(self, data):
6-
self.data = data # Assign data
7-
self.next = None # Initialize next as null
8-
self.prev = None # Initialize prev as null
9-
1+
# A complete working Python program to demonstrate all
2+
# stack operations using a doubly linked list
3+
4+
5+
class Node:
6+
def __init__(self, data):
7+
self.data = data # Assign data
8+
self.next = None # Initialize next as null
9+
self.prev = None # Initialize prev as null
10+
11+
1012
class Stack:
1113
"""
1214
>>> stack = Stack()
@@ -32,89 +34,90 @@ class Stack:
3234
stack elements are:
3335
2->1->0->
3436
"""
35-
def __init__(self):
37+
38+
def __init__(self):
3639
self.head = None
37-
40+
3841
def push(self, data):
3942
"""add a Node to the stack"""
40-
if self.head is None:
41-
self.head = Node(data)
42-
else:
43-
new_node = Node(data)
44-
self.head.prev = new_node
45-
new_node.next = self.head
43+
if self.head is None:
44+
self.head = Node(data)
45+
else:
46+
new_node = Node(data)
47+
self.head.prev = new_node
48+
new_node.next = self.head
4649
new_node.prev = None
47-
self.head = new_node
48-
50+
self.head = new_node
51+
4952
def pop(self):
5053
"""pop the top element off the stack"""
51-
if self.head is None:
54+
if self.head is None:
5255
return None
53-
else:
54-
temp = self.head.data
56+
else:
57+
temp = self.head.data
5558
self.head = self.head.next
5659
self.head.prev = None
57-
return temp
58-
60+
return temp
61+
5962
def top(self):
6063
"""return the top element of the stack"""
6164
return self.head.data
6265

63-
def __len__(self):
64-
temp = self.head
66+
def __len__(self):
67+
temp = self.head
6568
count = 0
66-
while temp is not None:
69+
while temp is not None:
6770
count += 1
6871
temp = temp.next
69-
return count
72+
return count
7073

7174
def is_empty(self):
7275
return self.head is None
7376

74-
def print_stack(self):
75-
print("stack elements are:")
76-
temp = self.head
77-
while temp is not None:
78-
print(temp.data, end ="->")
79-
temp = temp.next
80-
81-
82-
# Code execution starts here
83-
if __name__=='__main__':
84-
85-
# Start with the empty stack
86-
stack = Stack()
87-
88-
# Insert 4 at the beginning. So stack becomes 4->None
89-
print("Stack operations using Doubly LinkedList")
90-
stack.push(4)
91-
92-
# Insert 5 at the beginning. So stack becomes 4->5->None
93-
stack.push(5)
94-
95-
# Insert 6 at the beginning. So stack becomes 4->5->6->None
96-
stack.push(6)
97-
98-
# Insert 7 at the beginning. So stack becomes 4->5->6->7->None
99-
stack.push(7)
100-
101-
# Print the stack
102-
stack.print_stack()
103-
104-
# Print the top element
105-
print("\nTop element is ", stack.top())
106-
107-
# Print the stack size
108-
print("Size of the stack is ", len(stack))
109-
110-
# pop the top element
111-
stack.pop()
112-
113-
# pop the top element
114-
stack.pop()
115-
77+
def print_stack(self):
78+
print("stack elements are:")
79+
temp = self.head
80+
while temp is not None:
81+
print(temp.data, end="->")
82+
temp = temp.next
83+
84+
85+
# Code execution starts here
86+
if __name__ == "__main__":
87+
88+
# Start with the empty stack
89+
stack = Stack()
90+
91+
# Insert 4 at the beginning. So stack becomes 4->None
92+
print("Stack operations using Doubly LinkedList")
93+
stack.push(4)
94+
95+
# Insert 5 at the beginning. So stack becomes 4->5->None
96+
stack.push(5)
97+
98+
# Insert 6 at the beginning. So stack becomes 4->5->6->None
99+
stack.push(6)
100+
101+
# Insert 7 at the beginning. So stack becomes 4->5->6->7->None
102+
stack.push(7)
103+
104+
# Print the stack
105+
stack.print_stack()
106+
107+
# Print the top element
108+
print("\nTop element is ", stack.top())
109+
110+
# Print the stack size
111+
print("Size of the stack is ", len(stack))
112+
113+
# pop the top element
114+
stack.pop()
115+
116+
# pop the top element
117+
stack.pop()
118+
116119
# two elements have now been popped off
117-
stack.print_stack()
118-
119-
# Print True if the stack is empty else False
120-
print("\nstack is empty:", stack.is_empty())
120+
stack.print_stack()
121+
122+
# Print True if the stack is empty else False
123+
print("\nstack is empty:", stack.is_empty())

0 commit comments

Comments
 (0)