-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharithmetic_arranger.py
131 lines (99 loc) · 4.52 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
def arithmetic_arranger(problems, display=False):
operators = []
longest_operands = []
len_diffs = []
num_spaces = []
print_list = []
print_list_2 = []
print_list_3 = []
if len(problems) > 5:
return "Error: Too many problems."
for problem in range(len(problems)):
if "+" in problems[problem]:
problems[problem] = problems[problem].split(" + ")
operators.append("+")
elif "-" in problems[problem]:
problems[problem] = problems[problem].split(" - ")
operators.append("-")
else:
return "Error: Operator must be '+' or '-'."
for problem in range(len(problems)):
if problems[problem][0].isdigit() == False:
return "Error: Numbers must only contain digits."
elif problems[problem][1].isdigit() == False:
return "Error: Numbers must only contain digits."
for problem in range(len(problems)):
if len(problems[problem][0]) > 4:
return "Error: Numbers cannot be more than four digits."
elif len(problems[problem][1]) > 4:
return "Error: Numbers cannot be more than four digits."
for problem in range(len(problems)):
if len(problems[problem][0]) < len(problems[problem][1]):
longest_operands.append("1")
len_diffs.append(1 + (len(problems[problem][1])-len(problems[problem][0])))
else:
longest_operands.append("-1")
len_diffs.append(-1 - (len(problems[problem][0])-len(problems[problem][1])))
#---------------------------------
for lens in range(len(len_diffs)):
if len_diffs[lens] > 0:
for num_times_print in range(len_diffs[lens]):
print_list.append(" ")
print_list.append(" ")
print_list.append(problems[lens][0])
if lens != (len(problems) - 1):
print_list.append(" ")
else:
print_list.append(" ")
print_list.append(problems[lens][0])
if lens != (len(problems) - 1):
print_list.append(" ")
for lens in range(len(len_diffs)):
print_list_2.append(operators[lens])
if len_diffs[lens] < 0:
for spaces in range(abs(len_diffs[lens])):
print_list_2.append(" ")
print_list_2.append(problems[lens][1])
if lens != (len(problems) - 1):
print_list_2.append(" ")
else:
print_list_2.append(" ")
print_list_2.append(problems[lens][1])
if lens != (len(problems) - 1):
print_list_2.append(" ")
for operands in range(len(longest_operands)):
if longest_operands[operands] == "-1":
for number_of_dashes in range(len(problems[operands][0])+2):
print_list_3.append("-")
else:
for number_of_dashes in range(len(problems[operands][1])+2):
print_list_3.append("-")
if operands != (len(problems) - 1):
print_list_3.append(" ")
return_statement = ''.join(print_list) + "\n" + ''.join(print_list_2) + "\n" + ''.join(print_list_3)
if display == True:
solutions = []
print_list_4 = []
for problem in range(len(problems)):
if operators[problem] == "+":
solutions.append(int(problems[problem][0]) + int(problems[problem][1]))
else:
solutions.append(int(problems[problem][0]) - int(problems[problem][1]))
for problem in range(len(problems)):
if len(problems[problem][0]) > len(problems[problem][1]):
num_spaces.append(len(str(problems[problem][0])) - len(str(solutions[problem])))
else:
num_spaces.append(len(str(problems[problem][1])) - len(str(solutions[problem])))
for spaces in range(len(num_spaces)):
for i in range(2 + int(num_spaces[spaces])):
print_list_4.append(" ")
print_list_4.append(solutions[spaces])
if spaces != (len(problems) - 1):
print_list_4.append(" ")
for i in range(len(print_list_4)):
if isinstance(print_list_4[i], int):
print_list_4[i] = str(print_list_4[i])
return_statement = ''.join(print_list) + "\n" + ''.join(print_list_2) + "\n" + ''.join(print_list_3) + "\n" + ''.join(print_list_4)
return return_statement
else:
return return_statement