Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions maths/polynomial_evaluation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
def evaluate_poly(poly, x):
"""
Objective: Computes the polynomial function for a given value x.
Returns that value.
Input Prams:
poly: tuple of numbers - value of cofficients
x: value for x in f(x)
Return: value of f(x)

>>> evaluate_poly((0.0, 0.0, 5.0, 9.3, 7.0), 10)
79800.0
"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a doctest:

    >>> evaluate_poly((0.0, 0.0, 5.0, 9.3, 7.0), 10)
    79800.0

Then makes sure it passes locally with: python3 -m doctest -v maths/polynomial_evaluation.py

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cclauss thanks for the suggestion!
will do the changes.


return sum(c*(x**i) for i, c in enumerate(poly))


if __name__ == "__main__":
"""
Example: poly = (0.0, 0.0, 5.0, 9.3, 7.0) # f(x) = 7.0x^4 + 9.3x^3 + 5.0x^2
x = -13
print (evaluate_poly(poly, x)) # f(-13) = 7.0(-13)^4 + 9.3(-13)^3 + 5.0(-13)^2 = 180339.9
"""
poly = (0.0, 0.0, 5.0, 9.3, 7.0)
x = 10
print(evaluate_poly(poly, x))