Skip to content

@RLGGHC /aashishchittimilla.rb secret forked from aashish/aashishchittimilla.rb
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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.