Polynomial Evaluate
Programming Interview
Medium
8 views
Problem Description
Given {x}, Create Polynomial class eval(x). Output value.
Input Format
Line1 n. Line2 n+1 coefficients. Line3 x.
Output Format
One integer value.
Official Solution
import sys
lines=sys.stdin.read().splitlines()
if len(lines)<3: sys.exit(0)
n=int(lines[0].strip())
coef=list(map(int,lines[1].split()[:n+1]))
x=int(lines[2].strip())
class Polynomial:
def __init__(self,coef):
self.coef=coef
def eval(self,x):
val=0
for c in self.coef:
val=val*x+c
return val
print(Polynomial(coef).eval(x))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!