-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPROJECT-arithmetic-formatter.py
76 lines (60 loc) · 2.83 KB
/
PROJECT-arithmetic-formatter.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
def arithmetic_arranger(problems, show_answers=False):
SPACING = ' '
OPERATOR_OFFSET = 2
OPERATOR_INPUT_OFFSET = 3
MAX_INPUT_DIGITS = 4
MAX_PROBLEMS_AMOUNT = 5
# Expressions validation and parsing
expressions = []
for problem in problems:
if len(expressions) == MAX_PROBLEMS_AMOUNT:
return 'Error: Too many problems.'
first_space_index = problem.find(" ")
expressions.append({
'top': problem[:first_space_index],
'bottom': problem[first_space_index+OPERATOR_INPUT_OFFSET:].strip(),
'op': problem[first_space_index:first_space_index+OPERATOR_INPUT_OFFSET].strip()
})
if expressions[-1]['op'] not in ('+', '-'):
return 'Error: Operator must be \'+\' or \'-\'.'
elif not expressions[-1]['top'].isnumeric() or not expressions[-1]['bottom'].isnumeric():
return 'Error: Numbers must only contain digits.'
LAST_INDEX = len(expressions) - 1
lines = ['', '', '']
if show_answers == True:
lines.append('')
# Create output lines
for index, expr in enumerate(expressions):
if max(len(expr['top']), len(expr['bottom'])) > MAX_INPUT_DIGITS:
return 'Error: Numbers cannot be more than four digits.'
expr['width'] = max(len(expr['top']), len(expr['bottom'])) + OPERATOR_OFFSET
lines[0] += f'{expr["top"]:>{expr["width"]}}' \
+ (SPACING if index < LAST_INDEX else '')
lines[1] += f'{expr["op"]}{expr["bottom"]:>{expr["width"]-1}}' \
+ (SPACING if index < LAST_INDEX else '')
lines[2] += '-' * expr["width"] \
+ (SPACING if index < LAST_INDEX else '')
if show_answers == True:
result = calculate_result(expr)
lines[3] += f'{str(result):>{expr["width"]}}' \
+ (SPACING if index < LAST_INDEX else '')
return "\n".join(lines).strip('\n')
def calculate_result(expression):
first = int(expression['top'])
second = int(expression['bottom'])
op = expression['op']
if op == '+':
return first + second
elif op == '-':
return first - second
else:
return None
# Tests
print(f'\n{arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])}')
print(f'\n{arithmetic_arranger(["3801 - 2", "123 + 49"])}')
print(f'\n{arithmetic_arranger(["44 + 815", "909 - 2", "45 + 43", "123 + 49", "888 + 40", "653 + 87"])}')
print(f'\n{arithmetic_arranger(["3 / 855", "3801 - 2", "45 + 43", "123 + 49"])}')
print(f'\n{arithmetic_arranger(["24 + 85215", "3801 - 2", "45 + 43", "123 + 49"])}')
print(f'\n{arithmetic_arranger(["98 + 3g5", "3801 - 2", "45 + 43", "123 + 49"])}')
print(f'\n{arithmetic_arranger(["3 + 855", "988 + 40"], True)}')
print(f'\n{arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True)}')