What does this AssertionError mean in the context of my code? FAIL: test_arrangement (test_module.UnitTests)

Bingo.

1 Like

That is helpful, thank you.

I think it’s FINALLY working!!! :smiley:

With my function definition written this way:
def arithmetic_arranger(problems, results = False):

Calling the function with results = False as such:
print(arithmetic_arranger(["32 + 698", "3801 - 2", "50 + 50", "123 - 49", "12 + 3600"], results = False))
Passes all tests!

Calling the function with results = True as such:
print(arithmetic_arranger(["32 + 698", "3801 - 2", "50 + 50", "123 - 49", "12 + 3600"], results = True))
Passes all tests!

Thank you @sanity and @JeremyLT I really appreciate the help!

Everything is working so I’m posting the final version of the code here. From the arithmetic_arranger file:

def arithmetic_arranger(problems, results = False):
  if len(problems) > 5:
    return 'Error: Too many problems.'
  
  top = []
  bottom = []
  dashes = []
  spaces = '    '
  answers = []

# ["32 + 698", "3801 - 2", "50 + 50", "123 - 49", "12 + 3600"]

  for problem in problems:
    first = '  ' + problem.split()[0]
    second = problem.split(' ', 1)[1]
    difference_first = len(first) - len(second)
    difference_second = len(second) - len(first)

    width_first = len(problem.split()[0])
    width_second = len(problem.split()[2])

    if width_first > 4 or width_second > 4:
      return "Error: Numbers cannot be more than four digits."
      
    if second[0] != '+' and second[0] != '-':
      return "Error: Operator must be '+' or '-'."
    
    if first.lstrip().isdigit() != True or second[2:].isdigit() != True:
      return 'Error: Numbers must only contain digits.'
    
    if difference_first > 0:
      second = second[0] + ' ' * (difference_first + 1) + second[2:]
    elif difference_second > 0:
      first = ' ' * difference_second + first

    # Optional condition to display answers under problems:
    if results:
      if second[0] == '+':
        answer = int(first) + int(second[2:])
      else:
        answer = int(first) - int(second[2:])

      answer_to_string = str(answer)
      length_result = len(answer_to_string)
      answer_to_string = '  ' + answer_to_string
      length_maximum = max([len(first.lstrip()), len(second[1:].lstrip())])

      if length_maximum > length_result:
        answer_to_string = ' ' * (length_maximum - length_result) + answer_to_string
      elif length_maximum < length_result:
        answer_to_string = answer_to_string[1:]

      answers.append(answer_to_string)

  # Append variables to lists
    top.append(first)
    bottom.append(second)
    dashes.append('-' * len(second))

  # Set spacing
    spaced_top = spaces.join(top)
    spaced_bottom = spaces.join(bottom)
    spaced_dashes = spaces.join(dashes)
    spaced_answers = spaces.join(answers)
  
  if results:
    arranged_problems = spaced_top + '\n' + spaced_bottom + '\n' + spaced_dashes + '\n' + spaced_answers
  else:
    arranged_problems = spaced_top + '\n' + spaced_bottom + '\n' + spaced_dashes

  return arranged_problems

Well done, this is definitely a tricky one that seems to give a lot of people trouble. For reference, here’s my (slightly over-engineered) solution:

def arithmetic_arranger(problems, display_answers=False):
    if len(problems) > 5:
        return "Error: Too many problems."

    arithmetic_problems = []

    for p in problems:
        operand1, operator, operand2 = p.split()

        operand_len = max(len(operand1), len(operand2))
        if operand_len > 4:
            return "Error: Numbers cannot be more than four digits."

        try:
            if operator == "+":
                answer = int(operand1) + int(operand2)
            elif operator == "-":
                answer = int(operand1) - int(operand2)
            else:
                return "Error: Operator must be '+' or '-'."
        except ValueError:
            return "Error: Numbers must only contain digits."

        problem_width = operand_len + 2
        problem_lines = (
            operand1.rjust(problem_width),
            f"{operator} {operand2.rjust(operand_len)}",
            "-"*problem_width,
            str(answer).rjust(problem_width),
        )
        arithmetic_problems.append(problem_lines)

    arranged_lines = [
        (" "*4).join(sections) for sections in zip(*arithmetic_problems)
    ]
    if not display_answers:
        arranged_lines.pop()

    arranged_problems = "\n".join(arranged_lines)

    return arranged_problems
1 Like

Nicely done! This was challenging for sure.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.