secret
Last active — forked from amrnt/amrtamimi.rb

  • Download Gist
amrtamimi.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 32 33 34 35 36 37 38 39 40
class Polynomial
attr_reader :vars
def initialize(vars)
raise ArgumentError, 'Need at least 2 coefficients' if vars.size <2
 
number_of_terms ||= vars.size - 1
final_output = []
 
i=0
vars.each do |var|
if var == 0
final_output << ("")
else
if var == 1 || var == -1
final_output << ("+" if var == 1) unless i == 0
final_output << ("-" if var == -1)
final_output << "x"
final_output << "^#{number_of_terms}" unless number_of_terms == 1
else
final_output << ("+" if var > 0) unless i == 0
final_output << ("-" if var < 0)
final_output << (var.to_i * -1 if var < 0)
final_output << (var.to_i if var > 0)
final_output << "x" unless number_of_terms == 0
final_output << "^#{number_of_terms}" unless number_of_terms == 0 || number_of_terms == 1
end
end
i=i+1
number_of_terms = number_of_terms.to_i - 1
end
@final_output = final_output.to_s
@final_output = "0" if final_output.to_s.empty?
end
 
def to_s
@final_output
end
 
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.