Skip to content

Commit fe4938d

Browse files
authored
Add files via upload
1 parent b297b80 commit fe4938d

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

Diff for: binary_tree.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Creating binary tree from given list
2+
3+
from binarytree import build
4+
5+
# List of nodes
6+
7+
# nodes = [1, 3, 2, 8, 1, 1, 1, 4, 3, 3, 5, 5, 8, 3, 5, 5, 3, 3, 2, 2, 3]
8+
nodes = [9, 2, 7, 2, 9, 9, 1, 2, 6, 6, 8, 8, 6]
9+
10+
# Building the binary tree
11+
12+
binary_tree = build(nodes)
13+
print('Binary tree from list :\n', binary_tree)
14+
15+
# Getting list of nodes from binarytree
16+
17+
print('\nList from binary tree :', binary_tree.values)
18+
19+
# Python program for recursive implementation of max sum problem in a triangle
20+
21+
N = 4
22+
23+
24+
# Function for finding maximum sum
25+
26+
def max_path_sum(tri, x, y, row, col):
27+
if y == col:
28+
return 0
29+
30+
if x == row - 1:
31+
return tri[x][y]
32+
33+
return tri[x][y] + max(max_path_sum(tri, x + 1, y, row, col),
34+
max_path_sum(tri, x + 1, y + 1, row, col))
35+
36+
37+
# Driver program to test above functions
38+
tri = [[4], [4, 7], [5, 8, 8], [9, 2, 7, 7], [4, 7, 4, 8, 2], [9, 5, 6, 9, 9, 9], [6, 9, 5, 3, 2, 1, 3],
39+
[8, 8, 1, 8, 2, 3, 6, 4], [1, 6, 5, 4, 7, 9, 8, 8, 9], [9, 3, 6, 8, 2, 5, 1, 4, 1, 2],
40+
[2, 3, 5, 3, 2, 9, 9, 4, 7, 7, 5], [7, 7, 2, 7, 3, 1, 5, 5, 8, 6, 5, 3], [3, 6, 9, 3, 1, 8, 7, 2, 7, 6, 9, 2, 5],
41+
[8, 3, 9, 9, 6, 7, 4, 6, 1, 2, 5, 1, 5, 9], [4, 8, 5, 7, 8, 5, 1, 7, 1, 6, 3, 4, 5, 6, 8]]
42+
43+
print(max_path_sum(tri, 0, 0, 15, 15))

Diff for: calculator.py

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Simple calculator
2+
3+
def addition(x, y):
4+
sum = float(x) + float(y)
5+
print(sum)
6+
7+
8+
def subtraction(x, y):
9+
operation = float(x) - float(y)
10+
print(operation)
11+
12+
13+
def multiplication(x, y):
14+
multiply = float(x) * float(y)
15+
print(multiply)
16+
17+
18+
def division(x, y):
19+
divide = float(x) / float(y)
20+
print(divide)
21+
22+
23+
def exponentiation(x, y):
24+
exp = float(x) ** float(y)
25+
print(exp)
26+
27+
28+
def modulo(x, y):
29+
modul = float(x) % float(y)
30+
print(modul)
31+
32+
33+
def get_numbers():
34+
x, y = (input("Enter two numbers separated by a space: ").split())
35+
36+
while x.isalpha():
37+
print("Incorrect format, enter a number")
38+
x = input("Enter the first number: ")
39+
40+
while y.isalpha():
41+
print("Incorrect format, enter a number")
42+
y = input("Enter the second number: ")
43+
44+
return x, y
45+
46+
47+
def calculator():
48+
x, y = get_numbers()
49+
print(f"x= {x}")
50+
print(f"y= {y}")
51+
52+
while True:
53+
arithmetic = input("What math operation do you want to perform?: ").lower()
54+
55+
if arithmetic == 'add':
56+
addition(x, y)
57+
58+
elif arithmetic == 'subtract':
59+
subtraction(x, y)
60+
61+
elif arithmetic == 'multiply':
62+
multiplication(x, y)
63+
64+
elif arithmetic == 'divide':
65+
division(x, y)
66+
67+
elif arithmetic == 'exp':
68+
exponentiation(x, y)
69+
70+
elif arithmetic == 'modulo':
71+
modulo(x, y)
72+
73+
elif arithmetic == 'quit':
74+
quit()
75+
76+
else:
77+
print("Enter the correct math operation: (add/subtract/multiply/divide/exp/modulo/quit)")
78+
79+
80+
calculator()

0 commit comments

Comments
 (0)