Beta
Rational Zeros
13jfellows
Description:
You must write a function that finds the rational zeros of a given polynomial function.
The polynomial will be passed as a string, such as x2 + x - 6
, with the power after the variable and spaces between each term. The variable will always be x
. The polynomial will always consist only of integer coefficients and be simplified as much as possible already (e.g. x2 + 3x2
will always be 4x2
).
To use the above polynomial as an example, x2 + x - 6
= 0
when x = -3
and x = 2
. Therefore, your function should return [-3, 2]
.
A few notes:
- Your function should return the zeros in ascending order.
- Rational zeros must be returned as instances of class
Fraction
, e.g.,x = .5
asx = Fraction(1, 2)
. (Integer zeros such asx = 5
can be returned either as integers or instances ofFraction
.) - Each zero must be included as many times as its root multiplicity is. For example, polynomial
x3 - 3x - 2
has a rational rootx = -1
with multiplicity two, and a rational rootx = 2
with multiplicity one, sorational_zeros('x3 - 3x - 2')
should return[-1, -1, 2]
. - If the polynomial has no rational zeros, return an empty list (
[]
). - Don't worry about if the polynomial has any other irrational or imaginary roots - just return the rational roots that exist.
If you're stuck, check out https://en.wikipedia.org/wiki/Rational_root_theorem.
Algorithms
Similar Kata:
Stats:
Created | Dec 21, 2017 |
Published | Dec 21, 2017 |
Warriors Trained | 107 |
Total Skips | 20 |
Total Code Submissions | 327 |
Total Times Completed | 13 |
Python Completions | 13 |
Total Stars | 4 |
% of votes with a positive feedback rating | 90% of 5 |
Total "Very Satisfied" Votes | 4 |
Total "Somewhat Satisfied" Votes | 1 |
Total "Not Satisfied" Votes | 0 |
Total Rank Assessments | 5 |
Average Assessed Rank | 4 kyu |
Highest Assessed Rank | 3 kyu |
Lowest Assessed Rank | 5 kyu |