Arithmetic Arranger test suite diff output can be improved
See original GitHub issueThe test suite for the Arithmetic Arranger project doesn’t always have a well formatted diff
example:
I would expect to see it as
AssertionError: ' [23 chars] 123 \n+ 855 - 2 + 43 + 49 [73 chars] ' != ' [23 chars] 123\n+ 855 - 2 + 43 + 49\n-----[23 chars]----'
- 3 3801 45 123
? ----
+ 3 3801 45 123
- + 855 - 2 + 43 + 49
? ----
+ + 855 - 2 + 43 + 49
- ----- ------ ---- -----
? -----
+ ----- ------ ---- -----
- 858 3799 88 172
(note the last line)
I see this happen in various situations. In this case the code that causes this in the diff is below:
def arithmetic_arranger(problems, solutions=bool):
operand1 = []
operator = []
operand2 = []
results = []
op_length = []
# check the number of problems
if len(problems) > 5:
return 'Error: Too many problems.'
# split the problems into lists of operators and first and second operands
for i in range(len(problems)):
operand1.append(problems[i].split()[0])
operator.append(problems[i].split()[1])
operand2.append(problems[i].split()[2])
# check for invalid operators
for i in operator:
if not i == '+':
if not i == '-':
return "Error: Operator must be '+' or '-'."
# check for invalid operands
for i in operand1:
if not i.isnumeric():
return "Error: Numbers must only contain digits."
for i in operand2:
if not i.isnumeric():
return "Error: Numbers must only contain digits."
# check size of numbers
for i in operand1:
if len(i) > 4:
return "Error: Numbers cannot be more than four digits."
for i in operand2:
if len(i) > 4:
return "Error: Numbers cannot be more than four digits."
# compare length of individual operands
for i in range(len(problems)):
if len(operand1[i]) > len(operand2[i]):
op_length.append(len(operand1[i]))
else:
op_length.append(len(operand2[i]))
# arrange the output
space = ' '
first_line = ''
second_line = ''
third_line = ''
fourth_line = ''
for i in range(len(problems)):
first_line = first_line + space * (2 + op_length[i] - len(operand1[i])) + operand1[i] + space * 4
second_line = second_line + operator[i] + space * (1 + op_length[i] - len(operand2[i])) + operand2[
i] + space * 4
third_line = third_line + '-' * (2 + op_length[i]) + space * 4
arranged_problems = first_line + '\n' + second_line + '\n' + third_line
# calculate the results of the problems
if solutions:
for i in range(len(problems)):
if operator[i] == '+':
results.append(int(operand1[i]) + int(operand2[i]))
else:
results.append(int(operand1[i]) - int(operand2[i]))
for i in range(len(problems)):
fourth_line = fourth_line + space*(2 + op_length[i] - len(str(results[i]))) + str(results[i]) + space*4
arranged_problems = arranged_problems + '\n' + fourth_line
return arranged_problems
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
freeCodeCamp Python Project: Arithmetic Formatter - YouTube
Hey guys! This is a Twitch coding stream I've been working on. FreeCodeCamp has some great challenges for us to practice our coding...
Read more >Arithmetic arranger test error - The freeCodeCamp Forum
i just finished the arithmetic arranger project of scientific computing with python project . Every rule (test condition) was working fine ...
Read more >Currently taking the freecodecamp python and need help ...
Here is the arithmetic arranger question click here to access the question ... I'm not sure if there is a better approach I...
Read more >Python for everybody challenge 1 fcc arithmetic arranger
Create a function that receives a list of strings that are arithmetic problems and returns the problems arranged vertically and side-by-side.
Read more >python - Solution to arithmetic arranger from freecodecamp
Situations that will return an error: If there are too many problems supplied to the function. The limit is five, anything more will...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
pytest
seems much more clearI do think we want to keep one test with four problems, just to test the input is accepted, but cleaning up the rest would be a good idea.