Beta

Rational Zeros

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 as x = Fraction(1, 2). (Integer zeros such as x = 5 can be returned either as integers or instances of Fraction.)
  • Each zero must be included as many times as its root multiplicity is. For example, polynomial x3 - 3x - 2 has a rational root x = -1 with multiplicity two, and a rational root x = 2 with multiplicity one, so rational_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:

CreatedDec 21, 2017
PublishedDec 21, 2017
Warriors Trained107
Total Skips20
Total Code Submissions327
Total Times Completed13
Python Completions13
Total Stars4
% of votes with a positive feedback rating90% of 5
Total "Very Satisfied" Votes4
Total "Somewhat Satisfied" Votes1
Total "Not Satisfied" Votes0
Total Rank Assessments5
Average Assessed Rank
4 kyu
Highest Assessed Rank
3 kyu
Lowest Assessed Rank
5 kyu
Ad
Contributors
  • jfellows Avatar
  • uttumuttu Avatar
Ad