secret
Last active — forked from aashish/aashishchittimilla.rb

  • Download Gist
aashishchittimilla.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
class Polynomial
def Polynomial.new(args)
@expression = ""
raise ArgumentError, 'Need at least 2 coefficients.' unless args.length > 2
for i in 1..args.length
 
if (args[i-1].<=>0) == 1 && i > 1
@expression = @expression + "+"
end
if (args[i-1].<=> 0) == 0
next
end
if args.length-i == 0
@expression = @expression + "#{args[i-1]}"
elsif args[i-1] == 1
@expression = @expression + "x^" + "#{args.length-i}"
else
@expression = @expression + "#{args[i-1]}" + "x^" + "#{args.length-i}"
end
end
@expression
end
end
#y = Polynomial.new([-3,-4,1,0,6]) # => -3x^4-4x^3+x^2+6
#puts y
 
#puts Polynomial.new([1,0,2]) # => x^2+2
#puts Polynomial.new([1]) # => Need at least 2 coefficients. (ArgumentError)
 
# from D:/aashishkiran.rb:35

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.