-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhomeworkrepl.py
279 lines (206 loc) · 264 KB
/
homeworkrepl.py
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# -*- coding: utf-8 -*-
"""HomeworkREPL.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1AlqZBWbMH58Q_lN1AejVXSg8XAVyUyIG
# **The Homework REPL:**
## An easy to use declarative REPL for quick High-School Homework Caluclations
New to programming? Tired of seeing ERROR every time you try to program? Feel frustrated with your math homework? Want easy yet instantaneous solutions? Have no fear, the Homework REPL is here!
The Homework REPL was designed to introduce students to programming in a beginner-friendly fashion. Learning a new programming language can often be daunting for beginners- new syntax rules, working in a new environment, the list goes on. Using layers of abstraction, we introduce students to the idea of programming by streamlining commands and enabling users to gradually get accustomed to programming and the logic behind it without experiencing through the seemingly endless error messages.
Using Python and the Google CoLab Jupyter Notebook, we created an interface that works very similarly to how an actual REPL would (hence the name Homework REPL). Users can choose from a plethora of key commands and pass in customized arguments in a declarative paradigm. Using regular expressions, the program parses through the user’s input to identify what operation the user wants to perform as well as the measurements passed in. Helper functions are then called to perform the actual computation and the result is displayed back to the user.
## How to Use
Use the play button to run the main function.
Then simply use the commands listed in the Syntax Guide to utilise the many mathematical commands in HomeworkREPL.
## Syntax

# **Use the HomeworkREPL:**
"""
# USE THIS TO ACTIVATE THE REPL. PRESS THE PLAY BUTTON
main()
"""See the Code that was used to make this project below!"""
def main():
# MAIN REPL CODE
import re
run = True
print("REPL Initialized")
print("This is the Homework REPL \n Input statements using the Syntax for calculations to common mathematical problems")
print("//*//*//*//*//*//*//*//*//*//*// \n \n")
while run == True:
x = input('>> ')
if x == "EXIT":
run = False
print("END OF PROGRAM")
break
tokens = re.split("\s", x)
if tokens[0] != "GIVE":
print("INVALID INPUT")
continue
if tokens[1] == "MIDPOINT":
tempA = tokens[3].replace("(", "")
tempB = tempA.replace(")", "")
x_split = re.split(",", tempB)
temp2A = tokens[5].replace("(", "")
temp2B = temp2A.replace(")", "")
y_split = re.split(",", temp2B)
midpoint(float(x_split[0]), float(y_split[0]), float(x_split[1]), float(y_split[1]))
if tokens[1] == "DISTANCE":
tempA = tokens[3].replace("(", "")
tempB = tempA.replace(")", "")
x_split = re.split(",", tempB)
temp2A = tokens[5].replace("(", "")
temp2B = temp2A.replace(")", "")
y_split = re.split(",", temp2B)
distance(float(x_split[0]), float(y_split[0]), float(x_split[1]), float(y_split[1]))
if tokens[1] == "SLOPE":
tempA = tokens[3].replace("(", "")
tempB = tempA.replace(")", "")
x_split = re.split(",", tempB)
temp2A = tokens[5].replace("(", "")
temp2B = temp2A.replace(")", "")
y_split = re.split(",", temp2B)
slope(float(x_split[0]), float(y_split[0]), float(x_split[1]), float(y_split[1]))
if tokens[1] == "SURFACEAREA":
if tokens[3] == "CYLINDER":
radius = tokens[7]
length = tokens[11]
if float(radius) > 0 and float(length) > 0:
surfaceArea_cylinder(float(radius),float(length))
else:
print("INVALID INPUT")
if tokens[3] == "CONE":
radius = tokens[7]
length = tokens[11]
if float(radius) > 0 and float(length) > 0:
surfaceArea_cone(float(radius),float(length))
else:
print("INVALID INPUT")
if tokens[3] == "CUBE":
length = tokens[7]
if float(length) > 0:
surfaceArea_cube(float(length))
else:
print("INVALID INPUT")
if tokens[3] == "SPHERE":
radius = tokens[7]
if float(radius) > 0:
surfaceArea_sphere(float(radius))
else:
print("INVALID INPUT")
if tokens[1] == "AREA":
if tokens[3] == "SQUARE":
length = tokens[7]
if float(length) > 0:
area_of_square(float(length))
else:
print("INVALID INPUT")
if tokens[3] == "CIRCLE":
radius = tokens[7]
if float(radius) > 0:
area_of_circle(float(radius))
else:
print("INVALID INPUT")
if tokens[3] == "RECTANGLE":
length = tokens[7]
width = tokens[11]
if float(length) > 0 and float(width) > 0:
area_of_rectangle(float(length),float(width))
else:
print("INVALID INPUT")
if tokens[3] == "TRIANGLE":
base = tokens[7]
height = tokens[11]
if float(base) > 0 and float(height) > 0:
area_of_traingle(float(base),float(height))
else:
print("INVALID INPUT")
if tokens[3] == "TRAPEZOID":
base1 = tokens[7]
base2 = tokens[11]
height = tokens[15]
if float(base1) > 0 and float(base2) > 0 and float(height) > 0:
area_of_trapezoid(float(base1),float(base2),float(height))
else:
print("INVALID INPUT")
if tokens[1] == "VOLUME":
if tokens[3] == "CUBE":
if (float(tokens[7])) > 0:
cube_Volume((float(tokens[7])))
else:
print("INVALID INPUT")
if tokens[3] == "CONE":
if (float(tokens[7]) > 0 and (float(tokens[11])) > 0):
cone_Volume((float(tokens[7])), (float(tokens[11])))
else:
print("INVALID INPUT")
if tokens[3] == "CYLINDER":
if (float(tokens[7]) > 0 and (float(tokens[11])) > 0):
cylinder_Volume((float(tokens[7])), (float(tokens[11])))
else:
print("INVALID INPUT")
if tokens[3] == "SPHERE":
if (float(tokens[7])) > 0:
sphere_Volume((float(tokens[7])))
else:
print("INVALID INPUT")
"""Helper Functions"""
import math
# midpoint volume
def midpoint(x1, x2, y1, y2):
x = (x1+x2)/2
y = (y1+y2)/2
return print("Midpoint is: ("+str(x)+","+str(y)+")")
# surface area cube
def surfaceArea_cube(s):
return print("Surface Area of Cube is: " + str(6*s*s))
# surface area cylinder
def surfaceArea_cylinder(r,h):
return print("Surface Area of Cube is: " + str(2*math.pi*r*h + (2*math.pi*r*r)))
#surface area cone
def surfaceArea_cone(r,l):
return print("Surface Area of Cone is: " + str((1/3) * math.pi*r*r*l))
# surface area sphere
def surfaceArea_sphere(r):
return print("Surface Area of Sphere is: " + str((4/3) * math.pi*r*r*r))
# distance formula
def distance(x1, y1, x2, y2):
return print("Distance is " + str(math.sqrt((x1-x2)**2+(y1-y2)**2)))
# volume cube
def cube_Volume(side):
return print("Volume of Cube: " + str(math.pow(side,3)))
# volume cylinder
def cylinder_Volume(r, h):
return print("Volume of Cylinder: " + str((math.pi *(r**2)*h)))
# cone volume
def cone_Volume(r, h):
return print("Volume of Cone: " + str(math.pi * (r**2) * (h/3)))
# sphere volume
def sphere_Volume(r):
return print("Volume of sphere: " + str((4/3)*math.pi*(math.pow(r,3))))
# slope formula
def slope(x1,y1,x2,y2):
return print("The slope is: " + str((y2-y1)/(x2-x1)))
# area square
def area_of_square(s) -> float:
"""Return the area of a square."""
area = s**2
return print("Area is " + str(area))
# area rectangle
def area_of_rectangle(l, w) -> float:
"""Return the area of a rectangle."""
area = l * w
return print("Area is " + str(area))
# area circle
def area_of_circle(r) -> float:
"""Return the area of a circle."""
area = math.pi * r ** 2
return print("Area is " + str(area))
# area triangle
def area_of_triangle(b, h) -> float:
"""Return the area of a triangle."""
area = 0.5 * b * h
return print("Area is " + str(area))
# area trapezoid
def area_of_trapezoid(b_1, b_2, h) -> float:
"""Return the area of a trapezoid."""
area = 0.5 * h * (b_1 + b_2)
return print("Area is " + str(area))