secret
anonymous / williamyanez.rb
Created

  • Download Gist
williamyanez.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
=begin
RPCFN: Ruby**Fun (#4)
William Yanez (wyanez@gmail.com)
26/11/2009
=end
 
class Polynomial
 
def initialize(arr_coeficients)
raise ArgumentError,"Need at least 2 coefficients" if arr_coeficients.size<2
@arr_coeficients=arr_coeficients
process
end
def to_s
@poly
end
private
def process
degree=@arr_coeficients.size-1
@poly=""
@arr_coeficients.each do |coef|
unless coef.zero?
@poly<<"+" if coef>0 and !@poly.empty?
@poly<<"-" if coef<0
@poly<<coef.abs.to_s if coef.abs !=1 or degree.zero?
if degree>0
@poly<<"x"
@poly<<"^"<<degree.to_s if degree>1
end
end
degree=degree-1
end
@poly="0=0" if @poly.empty?
end
end
end
end
degree=degree-1
end
@poly="0=0" if @poly.empty?
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.