Scientific Computing with Python Projects - Arithmetic Formatter - rPW3RJt60un0GLkqS6Jh7

Tell us what’s happening:
Describe your issue in detail here.
https://replit.com/@StephenChan1/boilerplate-arithmetic-formatter#arithmetic_arranger.py

I ran the test module and passed,.

Q1. Is the link above in its entirety supposed to go into the solution link box?

Q2. Is my mess “readable” and did I over complicated anything?

Your code so far


def arithmetic_arranger(list1, default=False):
  # error 1
  if len(list1) > 5:
    return "Error: Too many problems."
  # error 2
  for i in list1:
    if i.split()[1] in "+-":
      continue
    else:
      return "Error: Operator must be '+' or '-'."
  # error 3
  list2 = []
  for i in list1:
    list2.append(i.split()[0])
    list2.append(i.split()[2])
  for i in list2:
    if not i.isdigit():
      return "Error: Numbers must only contain digits."
  # error 4
    for j in i.split():
      if len(j) <= 4:
        continue
      else:
        return "Error: Numbers cannot be more than four digits."
        
  
  # prepare operand and operator list
  operands_top = []
  operators = []
  operands_bot = []
  dashes = []
  results = []
  
  # seperating operands and operators from the given list of strings
  # numbers are right-aligned
  # a single space between the operator and the longest of the two operands
  # dashes run along the entire length of each problem individually
  for i in list1:      
    length = max(len(i.split()[0]), len(i.split()[2])) + 2
    operands_top.append(i.split()[0].rjust(length, " "))
    operators.append(i.split()[1])
    operands_bot.append(i.split()[2].rjust(length - 1, " "))
    dashes.append("-" * length)

  # print(operands_top)
  # print(operators)
  # print(operands_bot)
  # print(dashes)
  
  # calculate the resutls
  # right-aligned
  for i in range(len(list1)):
    length = len(dashes[i])
    top = operands_top[i]
    bot = operands_bot[i]
    if operators[i] == "+":
      results.append(str(int(top) + int(bot)).rjust(length, " "))
    else:
      results.append(str(int(top) - int(bot)).rjust(length, " "))
    
  # print(results)
  # operator on the same line as the second operand
  second_row = []
  for i in range(len(list1)):
    second_row.append(operators[i] + operands_bot[i])

  # print(second_row)
  # four spaces between each problem
  # both operands are in the same order as provided
  first_row = "    ".join(operands_top)
  second_row1 = "    ".join(second_row)
  third_row = "    ".join(dashes)
  forth_row = "    ".join(results)
  # print(first_row)
  # print(second_row1)
  # print(third_row)
  # print(forth_row)
  if default:
    arranged_problems = "\n".join([first_row, second_row1, third_row, forth_row])
  else:
    arranged_problems = "\n".join([first_row, second_row1, third_row])

    
  return arranged_problems

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36

Challenge: Scientific Computing with Python Projects - Arithmetic Formatter

Link to the challenge:

you can submit that, or also just this, without the link to the specific file

for reasability, you can choose more descriptive variable names

1 Like

Fun side note, to prevent all this commenting in and out, I use helpers like

debug = true

def print_debug(*args):
    if debug:
      print(args)

.....

    print_debug(first_row)
1 Like

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