mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
actions/checkout@v2 (#1643)
* actions/checkout@v2 https://github.com/actions/checkout/releases * fixup! Format Python code with psf/black push
This commit is contained in:
parent
1b3985837f
commit
34c808b375
2
.github/workflows/directory_writer.yml
vendored
2
.github/workflows/directory_writer.yml
vendored
|
@ -6,7 +6,7 @@ jobs:
|
|||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/setup-python@v1
|
||||
with:
|
||||
python-version: 3.x
|
||||
|
|
|
@ -8,7 +8,7 @@ from math import * # noqa: F401, F403
|
|||
from sympy import diff
|
||||
|
||||
|
||||
def newton_raphson(func: str, a: int, precision: int=10 ** -10) -> float:
|
||||
def newton_raphson(func: str, a: int, precision: int = 10 ** -10) -> float:
|
||||
""" Finds root from the point 'a' onwards by Newton-Raphson method
|
||||
>>> newton_raphson("sin(x)", 2)
|
||||
3.1415926536808043
|
||||
|
|
|
@ -3,8 +3,10 @@ import sys
|
|||
|
||||
import cryptomath_module as cryptomath
|
||||
|
||||
SYMBOLS = (r""" !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`"""
|
||||
r"""abcdefghijklmnopqrstuvwxyz{|}~""")
|
||||
SYMBOLS = (
|
||||
r""" !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`"""
|
||||
r"""abcdefghijklmnopqrstuvwxyz{|}~"""
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
'''
|
||||
"""
|
||||
A binary search Tree
|
||||
'''
|
||||
"""
|
||||
|
||||
|
||||
class Node:
|
||||
def __init__(self, value, parent):
|
||||
self.value = value
|
||||
|
@ -13,16 +15,11 @@ class Node:
|
|||
|
||||
if self.left is None and self.right is None:
|
||||
return str(self.value)
|
||||
return pformat(
|
||||
{
|
||||
"%s"
|
||||
% (self.value): (self.left, self.right)
|
||||
},
|
||||
indent=1,
|
||||
)
|
||||
return pformat({"%s" % (self.value): (self.left, self.right)}, indent=1,)
|
||||
|
||||
|
||||
class BinarySearchTree:
|
||||
def __init__(self, root = None):
|
||||
def __init__(self, root=None):
|
||||
self.root = root
|
||||
|
||||
def __str__(self):
|
||||
|
@ -32,10 +29,10 @@ class BinarySearchTree:
|
|||
return str(self.root)
|
||||
|
||||
def __reassign_nodes(self, node, newChildren):
|
||||
if(newChildren is not None): # reset its kids
|
||||
if newChildren is not None: # reset its kids
|
||||
newChildren.parent = node.parent
|
||||
if(node.parent is not None): # reset its parent
|
||||
if(self.is_right(node)): # If it is the right children
|
||||
if node.parent is not None: # reset its parent
|
||||
if self.is_right(node): # If it is the right children
|
||||
node.parent.right = newChildren
|
||||
else:
|
||||
node.parent.left = newChildren
|
||||
|
@ -55,10 +52,10 @@ class BinarySearchTree:
|
|||
new_node = Node(value, None) # create a new Node
|
||||
if self.empty(): # if Tree is empty
|
||||
self.root = new_node # set its root
|
||||
else: # Tree is not empty
|
||||
parent_node = self.root # from root
|
||||
else: # Tree is not empty
|
||||
parent_node = self.root # from root
|
||||
while True: # While we don't get to a leaf
|
||||
if value < parent_node.value: # We go left
|
||||
if value < parent_node.value: # We go left
|
||||
if parent_node.left == None:
|
||||
parent_node.left = new_node # We insert the new node in a leaf
|
||||
break
|
||||
|
@ -87,60 +84,65 @@ class BinarySearchTree:
|
|||
node = node.left if value < node.value else node.right
|
||||
return node
|
||||
|
||||
def get_max(self, node = None):
|
||||
def get_max(self, node=None):
|
||||
"""
|
||||
We go deep on the right branch
|
||||
"""
|
||||
if node is None:
|
||||
node = self.root
|
||||
if not self.empty():
|
||||
while(node.right is not None):
|
||||
while node.right is not None:
|
||||
node = node.right
|
||||
return node
|
||||
|
||||
def get_min(self, node = None):
|
||||
def get_min(self, node=None):
|
||||
"""
|
||||
We go deep on the left branch
|
||||
"""
|
||||
if(node is None):
|
||||
if node is None:
|
||||
node = self.root
|
||||
if(not self.empty()):
|
||||
if not self.empty():
|
||||
node = self.root
|
||||
while(node.left is not None):
|
||||
while node.left is not None:
|
||||
node = node.left
|
||||
return node
|
||||
|
||||
def remove(self, value):
|
||||
node = self.search(value) # Look for the node with that label
|
||||
if(node is not None):
|
||||
if(node.left is None and node.right is None): # If it has no children
|
||||
node = self.search(value) # Look for the node with that label
|
||||
if node is not None:
|
||||
if node.left is None and node.right is None: # If it has no children
|
||||
self.__reassign_nodes(node, None)
|
||||
node = None
|
||||
elif(node.left is None): # Has only right children
|
||||
elif node.left is None: # Has only right children
|
||||
self.__reassign_nodes(node, node.right)
|
||||
elif(node.right is None): # Has only left children
|
||||
elif node.right is None: # Has only left children
|
||||
self.__reassign_nodes(node, node.left)
|
||||
else:
|
||||
tmpNode = self.get_max(node.left) # Gets the max value of the left branch
|
||||
tmpNode = self.get_max(
|
||||
node.left
|
||||
) # Gets the max value of the left branch
|
||||
self.remove(tmpNode.value)
|
||||
node.value = tmpNode.value # Assigns the value to the node to delete and keesp tree structure
|
||||
|
||||
node.value = (
|
||||
tmpNode.value
|
||||
) # Assigns the value to the node to delete and keesp tree structure
|
||||
|
||||
def preorder_traverse(self, node):
|
||||
if node is not None:
|
||||
yield node # Preorder Traversal
|
||||
yield from self.preorder_traverse(node.left)
|
||||
yield from self.preorder_traverse(node.right)
|
||||
|
||||
def traversal_tree(self, traversalFunction = None):
|
||||
def traversal_tree(self, traversalFunction=None):
|
||||
"""
|
||||
This function traversal the tree.
|
||||
You can pass a function to traversal the tree as needed by client code
|
||||
"""
|
||||
if(traversalFunction is None):
|
||||
if traversalFunction is None:
|
||||
return self.preorder_traverse(self.root)
|
||||
else:
|
||||
return traversalFunction(self.root)
|
||||
|
||||
|
||||
def postorder(curr_node):
|
||||
"""
|
||||
postOrder (left, right, self)
|
||||
|
@ -150,8 +152,9 @@ def postorder(curr_node):
|
|||
nodeList = postorder(curr_node.left) + postorder(curr_node.right) + [curr_node]
|
||||
return nodeList
|
||||
|
||||
|
||||
def binary_search_tree():
|
||||
r'''
|
||||
r"""
|
||||
Example
|
||||
8
|
||||
/ \
|
||||
|
@ -170,7 +173,7 @@ def binary_search_tree():
|
|||
Traceback (most recent call last):
|
||||
...
|
||||
IndexError: Warning: Tree is empty! please use another.
|
||||
'''
|
||||
"""
|
||||
testlist = (8, 3, 6, 1, 10, 14, 13, 4, 7)
|
||||
t = BinarySearchTree()
|
||||
for i in testlist:
|
||||
|
@ -178,18 +181,18 @@ def binary_search_tree():
|
|||
|
||||
# Prints all the elements of the list in order traversal
|
||||
print(t)
|
||||
|
||||
if(t.search(6) is not None):
|
||||
|
||||
if t.search(6) is not None:
|
||||
print("The value 6 exists")
|
||||
else:
|
||||
print("The value 6 doesn't exist")
|
||||
|
||||
if(t.search(-1) is not None):
|
||||
if t.search(-1) is not None:
|
||||
print("The value -1 exists")
|
||||
else:
|
||||
print("The value -1 doesn't exist")
|
||||
|
||||
if(not t.empty()):
|
||||
if not t.empty():
|
||||
print("Max Value: ", t.get_max().value)
|
||||
print("Min Value: ", t.get_min().value)
|
||||
|
||||
|
@ -197,9 +200,11 @@ def binary_search_tree():
|
|||
t.remove(i)
|
||||
print(t)
|
||||
|
||||
|
||||
二叉搜索树 = binary_search_tree
|
||||
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
|
||||
doctest.testmod()
|
||||
binary_search_tree()
|
||||
|
|
|
@ -1,12 +1,14 @@
|
|||
# A complete working Python program to demonstrate all
|
||||
# stack operations using a doubly linked list
|
||||
|
||||
class Node:
|
||||
def __init__(self, data):
|
||||
self.data = data # Assign data
|
||||
self.next = None # Initialize next as null
|
||||
self.prev = None # Initialize prev as null
|
||||
|
||||
# A complete working Python program to demonstrate all
|
||||
# stack operations using a doubly linked list
|
||||
|
||||
|
||||
class Node:
|
||||
def __init__(self, data):
|
||||
self.data = data # Assign data
|
||||
self.next = None # Initialize next as null
|
||||
self.prev = None # Initialize prev as null
|
||||
|
||||
|
||||
class Stack:
|
||||
"""
|
||||
>>> stack = Stack()
|
||||
|
@ -32,89 +34,90 @@ class Stack:
|
|||
stack elements are:
|
||||
2->1->0->
|
||||
"""
|
||||
def __init__(self):
|
||||
|
||||
def __init__(self):
|
||||
self.head = None
|
||||
|
||||
|
||||
def push(self, data):
|
||||
"""add a Node to the stack"""
|
||||
if self.head is None:
|
||||
self.head = Node(data)
|
||||
else:
|
||||
new_node = Node(data)
|
||||
self.head.prev = new_node
|
||||
new_node.next = self.head
|
||||
if self.head is None:
|
||||
self.head = Node(data)
|
||||
else:
|
||||
new_node = Node(data)
|
||||
self.head.prev = new_node
|
||||
new_node.next = self.head
|
||||
new_node.prev = None
|
||||
self.head = new_node
|
||||
|
||||
self.head = new_node
|
||||
|
||||
def pop(self):
|
||||
"""pop the top element off the stack"""
|
||||
if self.head is None:
|
||||
if self.head is None:
|
||||
return None
|
||||
else:
|
||||
temp = self.head.data
|
||||
else:
|
||||
temp = self.head.data
|
||||
self.head = self.head.next
|
||||
self.head.prev = None
|
||||
return temp
|
||||
|
||||
return temp
|
||||
|
||||
def top(self):
|
||||
"""return the top element of the stack"""
|
||||
return self.head.data
|
||||
|
||||
def __len__(self):
|
||||
temp = self.head
|
||||
def __len__(self):
|
||||
temp = self.head
|
||||
count = 0
|
||||
while temp is not None:
|
||||
while temp is not None:
|
||||
count += 1
|
||||
temp = temp.next
|
||||
return count
|
||||
return count
|
||||
|
||||
def is_empty(self):
|
||||
return self.head is None
|
||||
|
||||
def print_stack(self):
|
||||
print("stack elements are:")
|
||||
temp = self.head
|
||||
while temp is not None:
|
||||
print(temp.data, end ="->")
|
||||
temp = temp.next
|
||||
|
||||
|
||||
# Code execution starts here
|
||||
if __name__=='__main__':
|
||||
|
||||
# Start with the empty stack
|
||||
stack = Stack()
|
||||
|
||||
# Insert 4 at the beginning. So stack becomes 4->None
|
||||
print("Stack operations using Doubly LinkedList")
|
||||
stack.push(4)
|
||||
|
||||
# Insert 5 at the beginning. So stack becomes 4->5->None
|
||||
stack.push(5)
|
||||
|
||||
# Insert 6 at the beginning. So stack becomes 4->5->6->None
|
||||
stack.push(6)
|
||||
|
||||
# Insert 7 at the beginning. So stack becomes 4->5->6->7->None
|
||||
stack.push(7)
|
||||
|
||||
# Print the stack
|
||||
stack.print_stack()
|
||||
|
||||
# Print the top element
|
||||
print("\nTop element is ", stack.top())
|
||||
|
||||
# Print the stack size
|
||||
print("Size of the stack is ", len(stack))
|
||||
|
||||
# pop the top element
|
||||
stack.pop()
|
||||
|
||||
# pop the top element
|
||||
stack.pop()
|
||||
|
||||
def print_stack(self):
|
||||
print("stack elements are:")
|
||||
temp = self.head
|
||||
while temp is not None:
|
||||
print(temp.data, end="->")
|
||||
temp = temp.next
|
||||
|
||||
|
||||
# Code execution starts here
|
||||
if __name__ == "__main__":
|
||||
|
||||
# Start with the empty stack
|
||||
stack = Stack()
|
||||
|
||||
# Insert 4 at the beginning. So stack becomes 4->None
|
||||
print("Stack operations using Doubly LinkedList")
|
||||
stack.push(4)
|
||||
|
||||
# Insert 5 at the beginning. So stack becomes 4->5->None
|
||||
stack.push(5)
|
||||
|
||||
# Insert 6 at the beginning. So stack becomes 4->5->6->None
|
||||
stack.push(6)
|
||||
|
||||
# Insert 7 at the beginning. So stack becomes 4->5->6->7->None
|
||||
stack.push(7)
|
||||
|
||||
# Print the stack
|
||||
stack.print_stack()
|
||||
|
||||
# Print the top element
|
||||
print("\nTop element is ", stack.top())
|
||||
|
||||
# Print the stack size
|
||||
print("Size of the stack is ", len(stack))
|
||||
|
||||
# pop the top element
|
||||
stack.pop()
|
||||
|
||||
# pop the top element
|
||||
stack.pop()
|
||||
|
||||
# two elements have now been popped off
|
||||
stack.print_stack()
|
||||
|
||||
# Print True if the stack is empty else False
|
||||
print("\nstack is empty:", stack.is_empty())
|
||||
stack.print_stack()
|
||||
|
||||
# Print True if the stack is empty else False
|
||||
print("\nstack is empty:", stack.is_empty())
|
||||
|
|
|
@ -36,6 +36,7 @@ def dp_count(S, m, n):
|
|||
|
||||
return table[n]
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
|
||||
|
|
|
@ -111,7 +111,7 @@ def simpson_integration(function, a: float, b: float, precision: int = 4) -> flo
|
|||
|
||||
for i in range(1, N_STEPS):
|
||||
a1 = a + h * i
|
||||
result += function(a1) * (4 if i%2 else 2)
|
||||
result += function(a1) * (4 if i % 2 else 2)
|
||||
|
||||
result *= h / 3
|
||||
return round(result, precision)
|
||||
|
|
Loading…
Reference in New Issue
Block a user