Skip to content

Instantly share code, notes, and snippets.

View ravikumar-n's full-sized avatar

ravikumar-n ravikumar-n

View GitHub Profile
class Polynomial
def initialize(coefficients)
raise ArgumentError, "Need at least 2 coefficients" if coefficients.size < 2
@co = coefficients
@powers = Array.new(@co.size - 2) { |i| "x^#{i+2}"}.reverse << 'x' << nil
end
def to_s
return "0" if @co.all? { |c| c.zero? } # not much to do in this case
@co.zip(@powers).map do |el|