Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.5 kB
2
Indexable
Never

def turn_to_list(a):

    # Find all matches of the pattern in the input string

    pattern = r'(\d+|[-+*/])'

    tokens = re.findall(pattern, a)

    return [int(token) if token.isdigit() else token for token in tokens]



def devide_and_concer(arr):

    # this function do the divesion and multiplication


    #make a list of every string 
    t = turn_to_list(arr)
    
    i = 0
    while i  < len(t): 
        if str(t[i]) == '*':
            s = t[i-1] * t[i+1]
            t.pop(i-1)
            t.pop(i)
            t.pop(i-1)
            t.insert(i-1,s)
        elif str(t[i]) == '/':
            s = t[i-1] / t[i+1]
            t.pop(i-1)
            t.pop(i)
            t.pop(i-1)
            t.insert(i-1,int(s))
        else : i+=1
        
    return ''.join([str(a) for a in t])      



def sum_and_sub(x): 
    '''
    this function return the sum of string number input
    '''
    s = x.replace(" ", "")
    o = []
    j = 0 
    for i in range(len(s)): 
        if not s[i].isdigit(): 
            o.append(int(s[j:i]))
            j = i 
        if i == len(s)-1: 
            i+=2
            o.append(int(s[j:i]))

    return sum(o)


def calculate_all(x):
    # to do the multiplicatin and sum by order 
    t = devide_and_concer(x)
    s = sum_and_sub(t)
    return s


# program main function to execute 
def main(): 

    x = input('Enter numbers to calculate: ')
    d = calculate_all(x)
    print(d)




if __name__ == '__main__': 
     main()
Leave a Comment