Skip to content

Commit 157ec80

Browse files
Added some basic Heap functions, created an expression calculation program, updated stack initialization
1 parent b33032e commit 157ec80

File tree

3 files changed

+173
-4
lines changed

3 files changed

+173
-4
lines changed

Heap.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
'''
2+
Overview:
3+
BST with Highest value is always at the top
4+
Complete Tree - Horizontal Line without gaps
5+
Equal values can be below
6+
7+
Min heaps are opposite of Max heaps (described above)
8+
Height is log(n)
9+
10+
Order is not preserved other than a general trend top-bottom
11+
12+
Stored in a List
13+
Able to figure out when one layer ends and next begins because each node
14+
only has two children
15+
Because of that, you can calculate the positions of children IF you start
16+
at index one
17+
18+
left_child = 2 * parent_index
19+
right_child = 2 * parent_index + 1
20+
parent = left_child / 2
21+
parent = right_child // 2
22+
just use // for any node
23+
24+
Insert - Add value to next available node in the last layer to keep
25+
the tree complete, bubble it up to the highest node possible
26+
'''
27+
28+
29+
class MaxHeap:
30+
def __init__(self):
31+
self.heap = []
32+
33+
def _left_child(self, index):
34+
return 2 * index + 1
35+
36+
def _right_child(self, index):
37+
return 2 * index + 2
38+
39+
def _parent(self, index):
40+
return (index - 1) // 2
41+
42+
def _swap(self, index1, index2):
43+
self.heap[index1], self.heap[index2] = self.heap[index2], self.heap[index1]
44+
45+
def insert(self, value):
46+
self.heap.append(value)
47+
current = len(self.heap) - 1
48+
while self.heap[self._parent(current)] < self.heap[current] and current > 0:
49+
self._swap(current, self._parent(current))
50+
current = self._parent(current)
51+
return True
52+
53+
def _sink_down(self, index):
54+
max_index = index
55+
while True:
56+
left_index = self._left_child(max_index)
57+
right_index = self._right_child(max_index)
58+
59+
if left_index < len(self.heap) and self.heap[left_index] > self.heap[max_index]:
60+
max_index = left_index
61+
62+
if right_index < len(self.heap) and self.heap[right_index] > self.heap[max_index]:
63+
max_index = right_index
64+
65+
if max_index != index:
66+
self._swap(index, max_index)
67+
index = max_index
68+
else:
69+
return
70+
71+
def remove(self):
72+
if len(self.heap) == 0:
73+
return None
74+
if len(self.heap) == 1:
75+
return self.heap.pop()
76+
max_value = self.heap[0]
77+
self.heap[0] = self.heap.pop(-1)
78+
self._sink_down(0)
79+
80+
return max_value
81+
82+
83+
mh = MaxHeap()
84+
mh.insert(95)
85+
mh.insert(75)
86+
mh.insert(80)
87+
mh.insert(55)
88+
mh.insert(60)
89+
mh.insert(50)
90+
mh.insert(65)
91+
print(mh.heap)
92+
mh.remove()
93+
print(mh.heap)
94+
mh.remove()
95+
print(mh.heap)

Math.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
from Stack import Stack
2+
import re
3+
OPERATOR = '[+\\-/*%]'
4+
PLUS_MINUS = '[+\\-]'
5+
DIVIDE_MULTIPLY = '[/*]'
6+
NUMBER = '[\\d.]'
7+
PARENTHESES_OPEN = '\\('
8+
PARENTHESES_CLOSED = '\\)'
9+
10+
11+
def _calc(v1, v2, operator): # Switch Statements
12+
if operator == '+':
13+
return v1 + v2
14+
if operator == '-':
15+
return v1 - v2
16+
if operator == '*':
17+
return v1 * v2
18+
if operator == '/':
19+
return v1 / v2
20+
if operator == '%':
21+
return v1 % v2
22+
if operator == '**':
23+
return v1 ** v2
24+
if operator == '//':
25+
return v1 // v2
26+
if operator == '++':
27+
return v1 + 1
28+
if operator == '--':
29+
return v1 + 1
30+
return None
31+
32+
33+
def _calc_expression_stack(expression: Stack) -> int:
34+
running_val = 0
35+
current_operator = None
36+
while expression.height > 0:
37+
last_char = expression.pop().value
38+
if re.search(OPERATOR, last_char) is not None:
39+
current_operator = last_char
40+
if expression.top and re.search(PLUS_MINUS, expression.top.value) is not None:
41+
current_operator += expression.pop().value
42+
running_val = _calc(running_val, None, current_operator)
43+
current_operator = None
44+
elif expression.top and re.search(DIVIDE_MULTIPLY, expression.top.value) is not None:
45+
current_operator += expression.pop().value
46+
elif re.search(NUMBER, last_char) is not None:
47+
current_number = last_char
48+
while expression.top and re.search(NUMBER, expression.top.value) is not None:
49+
current_number += expression.pop().value
50+
current_number = float(current_number)
51+
if current_operator:
52+
running_val = _calc(running_val, current_number, current_operator)
53+
current_operator = None
54+
else:
55+
running_val = current_number
56+
elif re.search(PARENTHESES_OPEN, last_char):
57+
if current_operator:
58+
running_val = _calc(running_val, _calc_expression_stack(expression), current_operator)
59+
else:
60+
running_val = _calc_expression_stack(expression)
61+
elif re.search(PARENTHESES_CLOSED, last_char):
62+
return running_val
63+
64+
return running_val
65+
66+
67+
def calc_expression(expression: str) -> int:
68+
char_stack = Stack()
69+
for i in range(len(expression)):
70+
if expression[-(i + 1)] != ' ': # last -> first so calculated left -> right
71+
char_stack.push(expression[-(i + 1)])
72+
return _calc_expression_stack(char_stack)
73+
74+
75+
print(calc_expression(input('')))

Stack.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ def __init__(self, value):
2828

2929

3030
class Stack:
31-
def __init__(self, value):
32-
new_node = Node(value)
33-
self.top = new_node
34-
self.height = 1
31+
def __init__(self):
32+
self.top = None
33+
self.height = 0
3534

3635
def print_stack(self):
3736
temp = self.top

0 commit comments

Comments
 (0)