Skip to content
the poor coder

Hackerrank Polynomials Solution

Poor Man
Published
Updated
Reading time
2 min read

poly

The poly tool returns the coefficients of a polynomial with the given sequence of roots.

Text
print numpy.poly([-1, 1, 1, 10])        #Output : [  1 -11   9  11 -10]

roots

The roots tool returns the roots of a polynomial with the given coefficients.

Text
print numpy.roots([1, 0, -1])           #Output : [-1.  1.]

polyint

The polyint tool returns an antiderivative (indefinite integral) of a polynomial.

Text
print numpy.polyint([1, 1, 1])          #Output : [ 0.33333333  0.5         1.          0.        ]

polyder

The polyder tool returns the derivative of the specified order of a polynomial.

Text
print numpy.polyder([1, 1, 1, 1])       #Output : [3 2 1]

polyval

The polyval tool evaluates the polynomial at specific value.

Text
print numpy.polyval([1, -2, 0, 2], 4)   #Output : 34

polyfit

The polyfit tool fits a polynomial of a specified order to a set of data using a least-squares approach.

Text
print numpy.polyfit([0,1,-1, 2, -2], [0,1,1, 4, 4], 2)
#Output : [  1.00000000e+00   0.00000000e+00  -3.97205465e-16]

The functions polyadd, polysub, polymul, and polydiv also handle proper addition, subtraction, multiplication, and division of polynomial coefficients, respectively.


Task

You are given the coefficients of a polynomial .
Your task is to find the value of  at point .

Input Format

The first line contains the space separated value of the coefficients in .
The second line contains the value of .

Output Format

Print the desired value.

Sample Input

Text
1.1 2 3
0

Sample Output

Text
3.0

Solution in python3#

Approach 1.

Python
import numpy
print(numpy.polyval(numpy.array(input().split(),float),int(input())))

Approach 2.

Python
import numpy
print(numpy.polyval(tuple(map(float, input().split())), float(input())))

Approach 3.

Python
import numpy
print (numpy.polyval(list(map(float, input().split())), float(input())))

Solution in python#

Approach 1.

Python
import numpy as np
A = map(float,raw_input().split())
print np.polyval(A,input())

Approach 2.

Python
import numpy
print numpy.polyval(map(float,raw_input().split()),float(raw_input()))

Approach 3.

Python
import numpy
print numpy.polyval(map(float, raw_input().split()), float(raw_input()))

Keep going

2020

Hackerrank Mean, Var, and Std Solution

mean The mean tool computes the arithmetic mean along the specified axis. By default, the axis is None. Therefore, it computes the mean of the flattened array. var The var tool computes the arithmetic variance along the…

2020

Hackerrank Linear Algebra Solution

The NumPy module also comes with a number of built-in routines for linear algebra calculations. These can be found in the sub-module linalg. linalg.det The linalg.det tool computes the determinant of an array…

ESC