Hello,
I’m currently on the arithmetic arranger problem described here https://repl.it/github/freeCodeCamp/boilerplate-arithmetic-formatter
My code seems very close to the expected result, BUT on some iterations there is a random
“F”
or a random
“.”
that is printed right before
“This is arranged_problems”
(which I use to visualize what my function returns).
It’s visible on loops 3 to 7. I don’t seem to have any capital F in my code, and I’ve also double-checked all the “.” and can’t understand where they come from.
Many thanks ahead for your help!
My code:
def arithmetic_arranger(problems, calc = False):
#initialize all values that are needed outside of the loop
pbl_firstsplit = []
pbl_secondsplit = []
pbl_secondsplit_padded = []
pbl_line1 = []
pbl_line2 = []
pbl_line3 = []
pbl_line4 = []
pbl_line1_joined = []
pbl_line2_joined = []
pbl_line3_joined = []
pbl_line4_joined = []
return_iffalse = []
return_iftrue = []
arranged_problems = []
# 1. count number of problems
count_problems_elements = len(problems)
# 2. identified non-authorised operators
substring_multiply = "*"
substring_divide = "/"
count_wrongoperators = str(problems).count(substring_divide) + str(problems).count(substring_multiply)
# 3. regex to check on nb of digits
import re
regex_maxfourdigits = '\d{5,}'
str_problems = str(problems)
match_maxfourdigits = re.search(regex_maxfourdigits, str_problems)
match_findnondigits = re.search('[a-zA-Z]', str_problems)
# make problems_str a string so it can be split
problems_str = str(problems)
#loop on the list of problems
i = 0
while i < len(problems):
pbl = problems[i]
# split each element within each problem - will be used to compute a result later on
pbl_split1 = pbl.split()
pbl_firstsplit.append(pbl_split1)
calc1 = pbl_firstsplit[i][0]
operator = pbl_firstsplit[i][1]
calc2 = pbl_firstsplit[i][2]
#now I have to re-split the pbl string to have "element one", "operator space element 2"
pbl_split2 = pbl.split(" ",1)
pbl_secondsplit.append(pbl_split2)
# now I will pad the elements using rjust
pbl_split2_str = str(pbl_split2)
longest_inpbl = max(pbl_split2, key=len)
len_longest_inpbl = len(longest_inpbl)
padded = [str(i).rjust(len_longest_inpbl, ' ') for i in pbl_split2]
pbl_secondsplit_padded.append(padded)
#now appending the formatted elements to each list
pbl_line1.append(pbl_secondsplit_padded[i][0] + " " * 4)
pbl_line1_joined = ''.join(pbl_line1)
pbl_line2.append(pbl_secondsplit_padded[i][1] + " " * 4)
pbl_line2_joined = ''.join(pbl_line2)
pbl_line3.append("-"* len(pbl_secondsplit_padded[i][0]) + " " * 4)
pbl_line3_joined = ''.join(pbl_line3)
# doing the computation only if a True argument is passed
if calc == True:
if operator == '+':
result = int(calc1) + int(calc2)
else:
result = int(calc1) - int(calc2)
result_padded = str(result).rjust(len_longest_inpbl, ' ')
pbl_line4.append(result_padded)
pbl_line4.append(" " * 4)
pbl_line4_joined = ''.join([str(i) for i in pbl_line4])
return_iftrue = "\n" +str(pbl_line1_joined) + "\n" + str(pbl_line2_joined) + "\n" + str(pbl_line3_joined) + "\n" + str(pbl_line4_joined)
else:
return_iffalse = "\n" + str(pbl_line1_joined) + "\n" + str(pbl_line2_joined) + "\n" + str(pbl_line3_joined)
i+=1
# error in nb of problems
if int(count_problems_elements) > 4:
arranged_problems ="Error: Too many problems."
# error in operators, ie if string contains * or /
elif count_wrongoperators > 0:
arranged_problems ="Error: Operator must be '+' or '-'."
# error if any number is more than 4 digit long
elif match_maxfourdigits is not None:
arranged_problems = "Error: Numbers cannot be more than four digits."
elif match_findnondigits is not None:
arranged_problems = "Error: Numbers must only contain digits."
# if all else ok, convert
elif calc == True:
arranged_problems = return_iftrue
else:
arranged_problems = return_iffalse
print("***This is arranged_problems: *** "+arranged_problems)
return arranged_problems
This is my output:
***This is arranged_problems: ***
32 3801 45 123
+ 698 - 2 + 43 + 49
----- ---- ---- ----
32 3801 45 123
+ 698 - 2 + 43 + 49
----- ---- ---- ----
***This is arranged_problems: ***
3 3801 45 123
+ 855 - 2 + 43 + 49
----- ---- ---- ----
F***This is arranged_problems: *** Error: Operator must be '+' or '-'.
.***This is arranged_problems: *** Error: Numbers must only contain digits.
.***This is arranged_problems: ***
32 1 45 123
- 698 - 3801 + 43 + 49
----- ------ ---- ----
-666 -3800 88 172
F***This is arranged_problems: *** Error: Numbers cannot be more than four digits.
.***This is arranged_problems: *** Error: Too many problems.
.