Hi All,
I was wondering if anyone could point me in the right direction. I have been working on the arithmetic_arranger problem. I have broken it down into bitesized chunks, so haven’t completed it yet, but I decided to test what I had so far. I have only completed where the second argument is False.
The problem I am having is that I have tested it with most of the examples in the test script and the output looks exactly like it should! Could someone tell me what I’m missing?
Thanks
mal
I am sure there are far better ways to achieve this, but here is my code:
def arithmetic_arranger(problems, condition=False):
import re
string = ""
string = ' '.join(problems)
list = string.split()
top_list = []
bottom_list = []
if len(list) > 15:
exit("Error: Too many problems.")
for item in list:
if list.index(item) % 3 == 0:
top_list.append(item)
else:
bottom_list.append(item)
#=====================================================
for item in bottom_list:
if bottom_list.index(item) % 2 == 0:
if (item != "+") and (item != "-"):
exit("Error: Operator must be '+' or '-'.")
#=======================================================
for item in list:
if len(item) > 4:
exit("Error: Numbers cannot be more than four digits.")
#========================================================
check_numbers_check = True
for item in problems:
check_numbers = item.split()
try:
int(check_numbers[0]) + int(check_numbers[2])
except:
check_numbers_check = False
if check_numbers_check == False:
exit("Error: Numbers must only contain digits.")
#=====================================================
top_row_spaced = []
x = 0
i = 1
for item in top_list:
ind_top = x
ind_bottom = ind_top + i
max_ind = max(len(top_list[ind_top]), len(bottom_list[ind_bottom]))
if x < len(top_list) - 1:
if top_row_spaced == []:
top_row_spaced.append(" " * ((max_ind + 2) - len(top_list[ind_top])) + item + " " * 4)
x += 1
i += 1
#print(x, i)
elif True:
top_row_spaced.append(" " * ((max_ind + 2) - len(top_list[ind_top])) + item + " " * 4)
x += 1
i += 1
else:
top_row_spaced.append(" " * ((max_ind + 2) - len(top_list[ind_top])) + item)
#========================================================
bottom_row_spaced = []
line_row_spaced = []
x = 0
i = 0
for item in bottom_list:
if bottom_list.index(item) % 2 != 0:
ind_bottom = bottom_list.index(item)
ind_top = ind_bottom - i
#print(ind_top, ind_bottom)
max_ind = max(len(top_list[ind_top]), len(bottom_list[ind_bottom]))
#print(max_ind)
if x < len(bottom_list) - 1:
if bottom_list.index(item) % 2 == 0:
bottom_row_spaced.append(item)
x += 1
i += 1
else:
bottom_row_spaced.append(" " * ((max_ind + 1) - len(item)) + item + " " * 4)
line_row_spaced.append("-" * (max_ind + 2) + " " * 4)
x += 1
elif len(str(bottom_list.index(item))) == 1:
bottom_row_spaced.append(" " * ((max_ind + 1) - len(item)) + item)
line_row_spaced.append("-" * (max_ind + 2) + " " * 4)
else:
bottom_row_spaced.append(" " * ((max_ind + 2) - len(item) ) + item)
#=============================================================================
#=============================================================================
arranged_items = ""
for element in top_row_spaced:
arranged_items += element
arranged_items += '\n'
for element in bottom_row_spaced:
arranged_items += element
arranged_items += '\n'
for element in line_row_spaced:
arranged_items += element
return(arranged_items)