Skip to content

Instantly share code, notes, and snippets.

@RLGGHC
Created December 9, 2009 00:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RLGGHC/c519fccdc1e6b2f7a3eb to your computer and use it in GitHub Desktop.
Save RLGGHC/c519fccdc1e6b2f7a3eb to your computer and use it in GitHub Desktop.
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment