-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharithmetic_arranger.py
More file actions
56 lines (47 loc) · 2.46 KB
/
arithmetic_arranger.py
File metadata and controls
56 lines (47 loc) · 2.46 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
def arithmetic_arranger(problems, show_answers=False):
if len(problems) > 5:
return 'Error: Too many problems.'
operators, numbers, answers = [], [], []
first_line, second_line, dashes, answer_row = '', '', '', ''
# Process problems
for problem in problems:
parts = problem.split()
if parts[1] not in ['+', '-']:
return "Error: Operator must be '+' or '-'."
if not parts[0].isdigit() or not parts[2].isdigit():
return 'Error: Numbers must only contain digits.'
if len(parts[0]) > 4 or len(parts[2]) > 4:
return 'Error: Numbers cannot be more than four digits.'
operators.append(parts[1])
numbers.append(parts[0])
numbers.append(parts[2])
# Calculate answers
if parts[1] == '+':
answers.append(int(parts[0]) + int(parts[2]))
else:
answers.append(int(parts[0]) - int(parts[2]))
# Arrange problems
for i in range(0, len(numbers), 2):
space_width = max(len(numbers[i]), len(numbers[i + 1])) + 2
first_number = numbers[i].rjust(space_width)
second_number = operators[i // 2] + ' ' + numbers[i + 1].rjust(space_width - 2)
first_line += first_number + ' '
second_line += second_number + ' '
dashes += '-' * space_width + ' '
if show_answers:
answer_row += str(answers[i // 2]).rjust(space_width) + ' '
# Return result
if show_answers:
return '\n'.join([first_line.rstrip(), second_line.rstrip(), dashes.rstrip(), answer_row.rstrip()])
else:
return '\n'.join([first_line.rstrip(), second_line.rstrip(), dashes.rstrip()])
print(f'\n{arithmetic_arranger(["3801 - 2", "123 + 49"])}')
print(f'\n{arithmetic_arranger(["1 + 2", "1 - 9380"])}')
print(f'\n{arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"])}')
print(f'\n{arithmetic_arranger(["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"])}')
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)}')