secret
Created

  • Download Gist
pankajsisodiya.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
class Polynomial
def initialize(*num)
@expression = ""
@howmanynumbers = 0
num.each{@howmanynumbers = @howmanynumbers + 1}
num.each{|n| @howmanynumbers = @howmanynumbers -1; if n ==1 then @expression = @expression + " + "+ "x^" + @howmanynumbers.to_s elsif n < 0 then @expression = @expression + " - " + (n*-1).to_s + "x^" + @howmanynumbers.to_s elsif n==0 then @expression = @expression else @expression = @expression + " + " + n.to_s + "x^" + @howmanynumbers.to_s end }
@expression.sub!('x^0', '')
@counter = -1
@finalexpression = ""
@expression.scan(/./) {|ch| @counter = @counter + 1; if ch == "+" && @counter == 0 then @finalexpression = @finalexpression; @counter = 1 else @finalexpression = @finalexpression + ch.to_s end }
puts @finalexpression
end
end
series = Polynomial.new(-3,1,1,0,6)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.