-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrpn.py
More file actions
70 lines (57 loc) · 1.34 KB
/
rpn.py
File metadata and controls
70 lines (57 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import operator
import readline
from termcolor import colored, cprint
operators = {
'+': operator.add,
'-': operator.sub,
'*': operator.mul,
'/': operator.floordiv,
}
def calculate(arg):
stack = list()
for token in arg.split():
try:
value = int(token)
stack.append(value)
except ValueError:
function = operators[token]
arg2 = stack.pop()
arg1 = stack.pop()
result = function(arg1, arg2)
stack.append(result)
#print(stack)
if len(stack) != 1:
raise TypeError('malformed input')
return stack.pop()
def main():
while True:
result = calculate(input("rpn calc> "))
if result > 0:
cprint(result, 'magenta')
elif result < 0:
cprint(result, 'green')
else:
cprint(result, 'blue')
if __name__ == '__main__':
main()
def greeting():
hello = "Guten Tag!"
print(hello)
def story(arg):
story = "Once upon a time there was"
story += arg1
return story
def linearFunction(x, m, b):
y = m*x
y += b
return y
def slope(rise, run):
slope = rise / run
return slope
def grade(hw, exam, pres):
grade = (hw * 0.2)
grade += (exam * 0.5)
grade += (pres * 0.3)
return grade
def read(input):
return input