-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharithmetic_arranger.py
More file actions
60 lines (48 loc) · 1.87 KB
/
Copy patharithmetic_arranger.py
File metadata and controls
60 lines (48 loc) · 1.87 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
def arithmetic_arranger(problems, display_answers=False):
if len(problems) > 5:
return "Error: Too many problems."
top_row = ""
bottom_row = ""
lines = ""
results_row = ""
for i, problem in enumerate(problems):
# Split the problem into its parts
parts = problem.split()
if len(parts) != 3:
return "Error: Invalid problem format."
num1, operator, num2 = parts
# Check if the operator is valid
if operator not in ['+', '-']:
return "Error: Operator must be '+' or '-'."
# Check if both numbers are digits
if not (num1.isdigit() and num2.isdigit()):
return "Error: Numbers must only contain digits."
# Check if the numbers have more than four digits
if len(num1) > 4 or len(num2) > 4:
return "Error: Numbers cannot be more than four digits."
# Calculate the result if required
if display_answers:
if operator == '+':
result = str(int(num1) + int(num2))
else:
result = str(int(num1) - int(num2))
# Determine the width of the problem
width = max(len(num1), len(num2)) + 2
# Create the rows
if i > 0:
top_row += " "
bottom_row += " "
lines += " "
results_row += " "
top_row += num1.rjust(width)
bottom_row += operator + num2.rjust(width - 1)
lines += "-" * width
if display_answers:
results_row += result.rjust(width)
if display_answers:
return f"{top_row}\n{bottom_row}\n{lines}\n{results_row}"
else:
return f"{top_row}\n{bottom_row}\n{lines}"
# Example usage
print(arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"]))
print(arithmetic_arranger(["32 + 8", "1 - 3801", "9999 + 9999", "523 - 49"], True))