question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Arithmetic Arranger test suite diff output can be improved

See original GitHub issue

The test suite for the Arithmetic Arranger project doesn’t always have a well formatted diff

example: image

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:closed
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ieahleencommented, Jul 28, 2021

pytest seems much more clear

0reactions
naomi-lgbtcommented, Aug 19, 2021

I 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found