Newby looking for some assistance with arithmetic_arranger problem

Hi All,

I was wondering if anyone could point me in the right direction. I have been working on the arithmetic_arranger problem. I have broken it down into bitesized chunks, so haven’t completed it yet, but I decided to test what I had so far. I have only completed where the second argument is False.

The problem I am having is that I have tested it with most of the examples in the test script and the output looks exactly like it should! Could someone tell me what I’m missing?

Thanks

mal

I am sure there are far better ways to achieve this, but here is my code:

 def arithmetic_arranger(problems, condition=False):
    import re
    
    string = ""
    string = ' '.join(problems)
    list = string.split()
    top_list = []
    bottom_list = []
    
    if len(list) > 15:
        exit("Error: Too many problems.")
    
    
    for item in list:
        if list.index(item) % 3 == 0:
            top_list.append(item)
        else:
            bottom_list.append(item)
    
    #=====================================================
    for item in bottom_list:
        if bottom_list.index(item)  % 2 == 0:
            if (item != "+") and (item != "-"):
                exit("Error: Operator must be '+' or '-'.")
              
    #=======================================================
    
    for item in list:
        if len(item) > 4:
            exit("Error: Numbers cannot be more than four digits.")
    #========================================================
    
    check_numbers_check = True
    
    for item in problems:
        check_numbers = item.split()
        
        try:
            int(check_numbers[0]) + int(check_numbers[2])
        except:
            check_numbers_check = False

    if check_numbers_check == False:
        exit("Error: Numbers must only contain digits.")   
    #=====================================================
    top_row_spaced = []
    
    x = 0
    i = 1
    for item in top_list:
        
        ind_top = x
        ind_bottom = ind_top + i
       
        max_ind = max(len(top_list[ind_top]), len(bottom_list[ind_bottom]))
     
        if x < len(top_list) - 1:
            if top_row_spaced == []:
                top_row_spaced.append(" " * ((max_ind + 2) - len(top_list[ind_top])) + item + " " * 4)
                x += 1
                i += 1
                #print(x, i)
                

            elif True:
                top_row_spaced.append(" " * ((max_ind + 2) - len(top_list[ind_top])) + item + " " * 4)
                x += 1
                i += 1
        

        else:
            
            top_row_spaced.append(" " * ((max_ind + 2) - len(top_list[ind_top])) + item)
            
            
    
    #========================================================
    bottom_row_spaced = []
    line_row_spaced = []
    
    x = 0
    i = 0
    for item in bottom_list:
        if bottom_list.index(item) % 2 != 0:
            ind_bottom = bottom_list.index(item)
            ind_top = ind_bottom - i
            #print(ind_top, ind_bottom)
            max_ind = max(len(top_list[ind_top]), len(bottom_list[ind_bottom]))
            #print(max_ind)
           
        if x < len(bottom_list) - 1:
            if bottom_list.index(item) % 2 == 0:
                bottom_row_spaced.append(item)
                x += 1
                i += 1
                
                


            else:
                bottom_row_spaced.append(" " * ((max_ind + 1) - len(item)) + item + " " * 4)
                line_row_spaced.append("-" * (max_ind + 2) + " " * 4)
                
                x += 1
        elif len(str(bottom_list.index(item))) == 1:
            bottom_row_spaced.append(" " * ((max_ind + 1) - len(item)) + item)
            line_row_spaced.append("-" * (max_ind + 2) + " " * 4)

        else:
            bottom_row_spaced.append(" " * ((max_ind + 2) - len(item) ) + item)

  
    #=============================================================================
    

   
#=============================================================================

    arranged_items = ""
    for element in top_row_spaced:
        arranged_items += element
    arranged_items += '\n'
    for element in bottom_row_spaced:
        arranged_items += element
    arranged_items += '\n'
    for element in line_row_spaced:
        arranged_items += element
    return(arranged_items)

Could you write what errors exactly are you getting and/or link to your code on the replit?

1 Like

Hi,

Thanks for posting.
Does this help?

boilerplate-arithmetic-formatter - Replit

When I run it in VS Code, it looks correct, but I must be misunderstanding something

While I don’t really understand what kind of transformation you are doing at the start, a couple of things:
First: “list” is a Python keyword and should never be overwritten, use another variable name.
Second: The error messages say that your output ends with 4 spaces, which shoudn’t be there.

- ---    ------
+ ---    ------    
?              ++++

Third: You are supposed to “error(message)” not “exit(message)

1 Like

Hi,

Thanks for your feedback, I shall look into those and make the changes you suggested.

mal

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Hi Jagaya,

I’ve done it, thanks for your help.

mal

1 Like

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