Created — forked from aashish/aashishchittimilla.rb

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View aashishchittimilla.rb
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
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
 
 
 
 
 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.