-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArithmetic Formatter code
71 lines (57 loc) · 2.22 KB
/
Arithmetic Formatter code
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
def arithmetic_arranger(problems: str, answers: bool=False) -> str:
""" Returns the problems arranged vertically and side-by-side. """
# Error handling
if len(problems) > 5:
return "Error: Too many problems."
for problem in problems:
problem = problem.split()
if problem[1] not in ["+", "-"]:
return "Error: Operator must be '+' or '-'."
elif not problem[0].isnumeric() or not problem[2].isnumeric():
return "Error: Numbers must only contain digits."
elif len(problem[0]) > 4 or len(problem[2]) > 4:
return "Error: Numbers cannot be more than four digits."
# Rearrange functionality
arranged_problems = ""
line_one = []
line_two = []
line_three = []
problem_answers = []
between = " " * 4
for problem in problems:
problem = problem.split()
# Determine what's the largest digit
largest = ""
if len(problem[0]) > len(problem[2]):
largest = problem[0]
elif len(problem[0]) < len(problem[2]):
largest = problem[2]
else:
largest = problem[0]
dashes = "-" * (len(largest) + 2)
# "Space" refers to blank spaces
spaces_one = ""
spaces_two = ""
if largest == problem[0]:
spaces_one = " " * 2
spaces_two = " " * (len(largest) - len(problem[2]))
elif largest != problem[0]:
spaces_one = " " * ((len(largest) + 2) - len(problem[0]))
# Line formatting
line_one.append(f"{spaces_one}{problem[0]}")
line_two.append(f"{problem[1]} {spaces_two}{problem[2]}")
line_three.append(f"{dashes}")
result = eval(f"{problem[0]}{problem[1]}{problem[2]}")
spaces_three = " " * (len(dashes) - len(str(result)))
problem_answers.append(f"{spaces_three}{result}")
arranged_problems = (
f"{between.join(line_one)}\n"
f"{between.join(line_two)}\n"
f"{between.join(line_three)}"
)
# If `answers` is True, print the answers; otherwise delete the variable
if answers:
arranged_problems += f"\n{between.join(problem_answers)}"
else:
del problem_answers
return arranged_problems