Hi all, I am working on a arithmetic arranger problem. I have tested it myself in Visualize Python and it works. However, it could only pass 7 out of 10 test cases. The expected output looks the same as mine.
def arithmetic_arranger(sentences, boolean=False):
if len(sentences)>5:
return "Error: Too many problems."
output1 =""
output2 = ""
output3 = ""
line = ""
for item in sentences:
sign=str()
if item.count('+')==1:
sign='+'
cal=item.split('+')
if (cal[0].strip()).isdigit()==False:
return "Error: Numbers must only contain digits."
elif (cal[1].strip()).isdigit()==False:
return "Error: Numbers must only contain digits."
else:
res=int(cal[0])+int(cal[1])
elif item.count('-')==1:
sign='-'
cal=item.split('-')
if (cal[0].strip()).isdigit()==False:
return "Error: Numbers must only contain digits."
elif (cal[1].strip()).isdigit()==False:
return "Error: Numbers must only contain digits."
else:
res=int(cal[0])-int(cal[1])
else:
return "Error: Operator must be '+' or '-'."
length = max(len(cal[0].strip()),len(cal[1].strip()))+2
if length-2 > 4:
return "Error: Numbers cannot be more than four digits."
else:
pos1=length - len(cal[0].strip())
output1 +=" "*(pos1) +cal[0].strip()+" "
pos2=length - len(cal[1].strip())
output2 +=sign + " "*(pos2-1)+cal[1].strip()+" "
line += "-"*length+" "
if not boolean:
return (output1[:-4]+"\n"+output2[:-4]+"\n"+line[:-4])
else:
return (output1[:-4]+"\n"+output2[:-4]+"\n"+line[:-4]+"\n"+output3[:-4])
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36
Challenge: Arithmetic Formatter
Link to the challenge: