Untitled

 avatar
unknown
plain_text
a year ago
1.8 kB
13
Indexable
def func(arr):
    output = []
    if 'x' not in arr:
        output.append("{:.5f}".format(0))
        return output
    arr = "".join(arr.split())
    arr = arr.replace('-', '+-')
    elements = arr.split('+')
    elements = [x for x in elements if x]
    dict={}
    print(elements)
    for e in elements:                
        if '-' not in e:
            if len(e)==1 and 'x' not in e:
                coefficient = float(e)
                exponent = 0
            if 'x^' in e:
                coefficient = e[0]
                if e[0]=='x':
                    coefficient = 1
                exponent = int(e.split('x^')[1])
            if '^' not in e and 'x' in e:
                exponent=1
                if len(e)==1:
                    coefficient=1
                else:
                    coefficient=e[0]
        if '-' in e:
            newe = e.split('-')
            e = newe[1]
            if len(e)==1 and 'x' not in e:
                coefficient = -float(e)
                exponent = 0
            if 'x^' in e:
                if e[0]=='x':
                    coefficient = -1
                else: 
                    coefficient = -float(e[0])
                exponent = int(e.split('x^')[1])
            if '^' not in e and 'x' in e:
                exponent=1
                if len(e)==1:
                    coefficient=-1
                else:
                    coefficient=-float(e[0])
            
        dict[exponent] = coefficient
    max_exp = max(dict.keys())
    output = []
    for i in range(0, max_exp+1):
        if i in dict.keys():
            output.append("{:.5f}".format(float(dict[i])))
        else:
            dict[i] = 0
            output.append("{:.5f}".format(float(dict[i])))
            
        
    return output
Editor is loading...
Leave a Comment