Tell us what’s happening:
Describe your issue in detail here.
I have done all the things that the challenge is asking and the only thing left is the 4 spaces between each problem which I am not able to figure out how to do. In my current code, I have used rjust() function to align it which I think is why there is the problem because it does not take in account the number of digits there are (in my code that is). I have also added some spaces at the end just so it looks closer to the correct result, but those spaces are not practical. So I would request you to help me in figuring out how to keep those 4 spaces between each problem.
Your code so far
import re
def arithmetic_arranger(problems, showAns = False):
ans = ""
result = ""
arr = []
leng = []
sign = []
if len(problems) > 5:
return "Error: Too many problems."
# Extracting the numbers
for i in problems:
if len(re.findall("[A-Z,a-z]+", i)) > 0:
return "Error: Numbers must only contain digits."
arr = arr + re.findall("[0-9]+", i)
# Printing top row
for i in range(0, len(arr), 2):
if len(arr[i]) > 4:
return "Error: Numbers cannot be more than four digits."
result = result + arr[i].rjust(6) + " "
result = result.rstrip()
result = result + "\n"
# Extracting the signs
for i in problems:
sign = sign + re.findall("[+,-]", i)
if len(sign) != len(problems):
return "Error: Operator must be '+' or '-'."
# Preparing middle row with signs
for i in range(1, len(arr), 2):
if len(arr[i]) > 4:
return "Error: Numbers cannot be more than four digits."
# Keeping appropriate signs
for j in range(len(sign)):
if i != (j + j + 1):
continue
else:
s = sign[j]
# Answers to questions if showAns is true
if showAns:
if sign[j] == "+":
sd = int(arr[i]) + int(arr[i - 1])
ans = ans + (str(sd)).rjust(6) + " "
elif sign[j] == "-":
sd = int(arr[i - 1]) - int(arr[i])
ans = ans + (str(sd)).rjust(6) + " "
# Length of spaces between signs and the middle row number
if len(arr[i - 1]) - len(arr[i]) <= 0:
spaces = 1
else:
spaces = (len(arr[i - 1]) - len(arr[i])) + 1
sp = ""
while spaces != 0:
sp = sp + " "
spaces = spaces - 1
l = s + sp + arr[i]
leng.append(l)
# Printing the middle row
result = result + l.rjust(6) + " "
result = result.rstrip()
result = result + "\n"
# Printing the bottom row with appropraite number of dashes (-)
for i in leng:
d = len(i)
dash = ""
while d != 0:
dash = dash + "-"
d = d - 1
result = result + dash.rjust(6) + " "
result = result.rstrip()
# Outputting result
if not showAns:
return result
else:
result = result + "\n"
result = result + ans
return result
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; rv:88.0) Gecko/20100101 Firefox/88.0
Challenge: Arithmetic Formatter
Link to the challenge: