-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharithmetic_arranger.py
154 lines (121 loc) · 4.76 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
def arithmetic_arranger(problems, evaluate=False):
arranged_problems = None
if len(problems) == 0:
return "Error: List is empty"
if len(problems) > 5:
return "Error: Too many problems."
if not is_operator_valid(problems):
return "Error: Operator must be '+' or '-'."
operands_in_exp = get_all_operands(problems)
if not is_operand_valid(operands_in_exp):
return "Error: Numbers must only contain digits."
if not has_max_four_digits(operands_in_exp):
return "Error: Numbers cannot be more than four digits."
result = []
if evaluate:
operators = get_all_operators(problems)
result = evaluate_expression(operands_in_exp, operators)
arranged_problems = arrange_expression(problems, result)
return arranged_problems
def is_operator_valid(problems):
size = len(problems)
count = 0
for expression in problems:
if expression.count('+') == 1 or expression.count('-') == 1:
count = count + 1
if count == size:
return True
return False
def get_all_operators(problems):
operators = []
for expression in problems:
if expression.count('+') == 1:
operators.append('+')
if expression.count('-') == 1:
operators.append('-')
return operators
def get_all_operands(problems):
all_operands = []
for expression in problems:
if expression.find('+') != -1:
operands = expression.split('+')
all_operands.append(operands)
elif expression.find('-') != -1:
operands = expression.split('-')
all_operands.append(operands)
return all_operands
def is_operand_valid(operands_in_exp):
count = 0
for operands in operands_in_exp:
if is_number(operands):
count = count + 1
if count == len(operands_in_exp):
return True
return False
def is_number(operands):
if len(operands) > 2:
return False
try:
left = operands[0].strip().isnumeric()
right = operands[1].strip().isnumeric()
return left and right
except:
return False
def has_max_four_digits(operands_in_exp):
count = 0
for operands in operands_in_exp:
is_less_than_four = True
for operand in operands:
if len(operand.strip()) > 4:
is_less_than_four = False
if is_less_than_four:
count = count + 1
if count == len(operands_in_exp):
return True
return False
def evaluate_expression(operands, operators):
result = []
for i in range(len(operands)):
if operators[i] == '+':
result.append(int(operands[i][0]) + int(operands[i][1]))
if operators[i] == '-':
result.append(int(operands[i][0]) - int(operands[i][1]))
return result;
def arrange_expression(problems, result):
exp_list = []
for expression in problems:
exp = expression.split(' ')
exp_list.append(exp)
transpose = [[exp_list[j][i] for j in range(len(exp_list))] for i in range(len(exp_list[0]))]
max_width = [len(max(exp_list[i], key=len)) for i in range(len(exp_list))]
output = ""
index = 0
for element in transpose[0]:
#print('{0:>{width}}{1:4s}'.format(element, '', width=(max_width[index] + 1)), end="")
if (index == len(transpose[0])-1):
output = output + '{0:>{width}}'.format(element, width=(max_width[index] + 2))
else:
output = output + '{0:>{width}}{1:4s}'.format(element, '', width=(max_width[index] + 2))
index = index + 1
output = output + '\n'
for col in range(len(transpose[0])):
#print('{0:}{1:>{width}}{2:4s}'.format(transpose[1][col], transpose[2][col], '', width=max_width[col]), end="")
if (col == len(transpose[0])-1):
output = output + '{0:''^2}{1:>{width}}'.format(transpose[1][col], transpose[2][col], width=max_width[col])
else:
output = output + '{0:''^2}{1:>{width}}{2:4s}'.format(transpose[1][col], transpose[2][col], '', width=max_width[col])
output = output + '\n'
for col in range(len(transpose[0])):
#print('{0:-^{width}}{1:4s}'.format('', '', width=(max_width[col] + 1)), end="")
if (col == len(transpose[0])-1):
output = output + '{0:-^{width}}'.format('', width=(max_width[col] + 2))
else:
output = output + '{0:-^{width}}{1:4s}'.format('', '', width=(max_width[col] + 2))
for col in range(len(result)):
if col == 0: output = output + '\n'
if (col == len(result)-1):
output = output + '{0:>{width}}'.format(result[col], width=(max_width[col] + 2))
else:
output = output + '{0:>{width}}{1:4s}'.format(result[col], '', width=(max_width[col] + 2))
return output
print(arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]))