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.
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()
Comments
Leave a comment