-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy patharithmetic_arranger.py
52 lines (45 loc) · 1.85 KB
/
arithmetic_arranger.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
def arithmetic_arranger(problems, val=False):
arranged_problems = ''
if len(problems) > 5:
arranged_problems = "Error: Too many problems."
return arranged_problems
# list of all operations in str format
operations = list(map(lambda x: x.split()[1], problems))
if set(operations) != {'+', '-'} and len(set(operations)) != 2:
arranged_problems = "Error: Operator must be '+' or '-'."
return arranged_problems
numbers = [] # list of all operands in str format
for i in problems:
p = i.split()
numbers.extend([p[0], p[2]])
if not all(map(lambda x: x.isdigit(), numbers)):
arranged_problems = "Error: Numbers must only contain digits."
return arranged_problems
if not all(map(lambda x: len(x) < 5, numbers)):
arranged_problems = "Error: Numbers cannot be more than four digits."
return arranged_problems
top_row = ''
dashes = ''
values = list(map(lambda x: eval(x), problems))
solutions = ''
for i in range(0, len(numbers), 2):
space_width = max(len(numbers[i]), len(numbers[i+1])) + 2
top_row += numbers[i].rjust(space_width)
dashes += '-' * space_width
solutions += str(values[i // 2]).rjust(space_width)
if i != len(numbers) - 2:
top_row += ' ' * 4
dashes += ' ' * 4
solutions += ' ' * 4
bottom_row = ''
for i in range(1, len(numbers), 2):
space_width = max(len(numbers[i - 1]), len(numbers[i])) + 1
bottom_row += operations[i // 2]
bottom_row += numbers[i].rjust(space_width)
if i != len(numbers) - 1:
bottom_row += ' ' * 4
if val:
arranged_problems = '\n'.join((top_row, bottom_row, dashes, solutions))
else:
arranged_problems = '\n'.join((top_row, bottom_row, dashes))
return arranged_problems