-
-
Save RLGGHC/244711 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Polynomial | |
def initialize array | |
if array.length <= 1 | |
raise ArgumentError.new('Need at least 2 coefficients') | |
exit | |
else | |
@return_string = '' | |
@iterations = array.length - 1 | |
array.each do |item| | |
if @iterations > 1 | |
@this_iteration = "^#{@iterations}" | |
else | |
@this_iteration = '' | |
end | |
if item.abs > 0 | |
if item > 1 | |
if @return_string.empty? | |
@item_string = "#{item}" | |
else | |
@item_string = "+#{item}" | |
end | |
elsif item < -1 | |
@item_string = "#{item}" | |
elsif (item == 1) | |
if @return_string.empty? | |
@item_string = '' | |
else | |
@item_string = '+' | |
end | |
elsif item == -1 | |
@item_string = '-' | |
end | |
if @iterations > 0 | |
@item_string = "#{@item_string}x" | |
end | |
@return_string << "#{@item_string}#{@this_iteration}" | |
end | |
@iterations = @iterations - 1 | |
end | |
if @return_string.empty? | |
@return_string = '0' | |
end | |
end | |
end | |
def to_s | |
@return_string | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment