Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

need help how to format numbers specifically according to how the problem wants me to format them, any methods i can use for this problem?

Your code so far

def arithmetic_arranger(problems, show_answers=False):
    newproblems = []
    for Item in problems:
        if Item.isalpha():
            return print("Error: must only contain numbers")
        
        subitems = Item.split()
        
        if len(subitems[0])>4 or len(subitems[2])>4:
            return print("Error: numbers cannot be more than 4 digits")
        
        if subitems[1] == "+":
          result = int(subitems[0])+int(subitems[2])
          if len(subitems[2]) > len(subitems[0]):
              temp1 = subitems[0]
              temp2 = subitems[2]
              subitems[2] = temp1
              subitems[0] = temp2
          #subitems.insert(0," ")
          #subitems.insert(3," ")
          #subitems.extend(["------",str(result)])
          #newitem = ''.join(subitems)
          #rightaligneditem = "{:>10}".format(newitem)
          #newproblems.append(rightaligneditem)

        if subitems[1] == "-":
          result = int(subitems[0])-int(subitems[2])
          if len(subitems[2]) > len(subitems[0]):
              temp1 = subitems[0]
              temp2 = subitems[2]
              subitems[2] = temp1
              subitems[0] = temp2
          #subitems.insert(0," ")
          #subitems.insert(3," ")
          #subitems.extend([ "------",str(result)])
          #newitem = ''.join(subitems)
          #rightaligneditem = "{:>10}".format(newitem)
          #newproblems.append(rightaligneditem)
        
        #if subitems[1] == "*" or subitems[1]== "/":
        #    return print("Error: Operator must be '+' or '-'")
        
    #if show_answers == True:
    #    return print(newproblems)
    #else:
    #    return newproblems


print(f'\n{arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])}')

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:137.0) Gecko/20100101 Firefox/137.0

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

1 Like

There’s no specific method for formatting numbers here. Just think of lines being printed like a dot matrix printer (is this reference still useable?).

You’ll need to break up the problem’s parts and re-assemble them as lines.

I think i came to the same conclusion but i am unsure how to carry it out. I am thinking about putting my first numbers on one line of a list and my second numbers on a second list and printing one then the other but i got stuck at the part with the operators as in how i would account for different operators.

1 Like

Wouldn’t you just put them with the second numbers? They go in the same line

note you need to return a string, not print the numbers

thank you, however i am still unsure how to add the spaces, backspaces, and line
in the correct places while in a for loop or would i need to add them using one of the methods that were described

When I solved this for the first time, I did it with loops

before thinking of what code to use, try to write the series of steps needed on paper, make sure you understand what output to get from which input

1 Like

Sadly i still cant understand how to progress. What should i do when i am completely stuck?

You want to add backspaces to a string?

Please post your current code and we can try to provide some guidance

def arithmetic_arranger(problems, show_answers=False):
    firstN = []  # List to hold the first numbers
    secondN = []  # List to hold the second numbers
    operators = []  # List to hold the operators ('+' or '-')
    lines = []  # List to hold the formatted lines for output
    results = []  # List to hold the results of operations

    # Check if there are more than 5 problems
    if len(problems) > 5:
        return "Error: Too many problems."

    # Loop through each problem
    for item in problems:
        subitems = item.split()  # Split the problem into components
        
        # Check if the numbers contain only digits
        if not subitems[0].isdigit() or not subitems[2].isdigit():
            return "Error: Numbers must only contain digits."
        
        # Check if numbers are more than four digits
        if len(subitems[0]) > 4 or len(subitems[2]) > 4:
            return "Error: Numbers cannot be more than four digits."

        # Check if the operator is valid
        if subitems[1] not in ['+', '-']:
            return "Error: Operator must be '+' or '-'."
        
        # Store the first and second numbers, as well as the operator
        firstN.append(subitems[0])
        secondN.append(subitems[2])
        operators.append(subitems[1])
        
        # Compute results for addition or subtraction
        result = int(subitems[0]) + int(subitems[2]) if subitems[1] == '+' else int(subitems[0]) - int(subitems[2])
        results.append(result)  # Store the result

    
        return arranged_problems  # Return the final formatted output

what i am not sure about is how to make a loop that will add the necessary spaces between the numbers, combining two lists together for the case of the operators and second numbers lists to that they are in one list and additionally how to dynamically create lines that will grow and shrink depending on the length of the largest number.

Break the problem down into smaller problems and solve them one at a time.

Would you be able to find the length of the longest number in this list?

list = [1, 2, 3, 888]

ok so i am looking at this method called rfind().
so i suppose:

list.rfind()

I’m sorry that’s not even close.

Why did you choose that method?

Did you try to find out what it does or how it works?

Search for it.

i am looking at at table of python string methods i have at hand.

Why are you looking at string methods?

Put this into your preferred search engine “python how to find the largest number in a list”

because i am not sure i can make a solution that is efficient, id be long winded and unnecessary.

but if i did i suppose id make a loop that will compare the first number on the list with the next, if the first number is larger id store that number in a variable then id move on the the next, the the second number is larger id store that one in a variable then more on to the next comparison untill the list ends.

Show me. Don’t worry about “efficient” just make something that works.

Now write that code and we’ll see if it works?

If you want to search for a method that will help, go ahead. We’re working with a list though, not a string

Ok this is what i came up with, it will not work for all use cases but for a small list i think it can work well enough


list = [1,2,3,888]

largestnum = max(list)
lengthlargestnum = len(str(largestnum))

Print(lengthlargestnum)

Did you test this out?

Does it work?

Don’t just guess

Can you also take that list and print it out like this?

1    3
2    888