Answer to Question #233852 in Python for Lovely

Question #233852

Please send corrected answer please for thisPolynomial


Given polynomial, write a program that prints polynomial in Cix^Pi + Ci-1x^Pi-1+...+ C1x + CO format.


Input


The first line contains a single integer N. Next N lines contain two integers Pi, Ci separated with space, where Pi denotes power and Ci denotes coefficient of Pi.



1
Expert's answer
2021-09-06T07:05:16-0400
def poly(co):
    s = []
    first = True
    p = ' + '
    m = ' - '
    for Pi in range(len(co)-1, -1, -1):
        Ci = co[Pi]
        if 0 == Ci:
            if 0 == Pi and first:
                s.append('0')
            continue
        if Ci < 0:
            sign = m
            Ci = -Ci
        else:
            sign = p
        if first:
            first = False
            sign = '-' if m == sign else ''
        s.append(sign)
        if 1 != Ci or 0 == Pi:
            s.append(str(Ci))
        if 0 < Pi:
            s.append('x')
            if 1 < Pi:
                s.append('^')
                s.append(str(Pi))
    print(''.join(s))


def main():
    N = int(input())
    C = [0 for _ in range(N)]
    for i in range(N):
        Pi, Ci = list(map(int, input('Pi, Ci: ').split()))
        C[Pi] = Ci
    poly(C)


if __name__ == '__main__':
    import doctest
    doctest.testmod()
    main()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS