Skip to content

Instantly share code, notes, and snippets.

@RLGGHC
Forked from aurelienbottazini/aurelienbottazzini.rb
Created December 1, 2009 01:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RLGGHC/363400bbb74423c054f1 to your computer and use it in GitHub Desktop.
Save RLGGHC/363400bbb74423c054f1 to your computer and use it in GitHub Desktop.
=begin rdoc
For RPCFN: Ruby**Fun (#4)
The Original Problem can be found here:
http://rubylearning.com/blog/2009/11/26/rpcfn-rubyfun-4/
Here is an excerpt:
Write a class that pretty-prints polynomials, following some simple rules:
* if a coefficient is 1, it doesn't get printed
* if a coefficient is negative, you have to display something like "- 2x^3", not "+ -2x^3"
* if a coefficient is 0, nothing gets added to the output
* for x^1 the ^1 part gets omitted
* x^0 == 1, so we don't need to display it
Here's a couple of usage examples:
puts Polynomial.new([-3,-4,1,0,6]) # => -3x^4-4x^3+x^2+6
puts Polynomial.new([1,0,2]) # => x^2+2
Don't concern yourself too much with error handling, but if somebody tries to create a
polynomial with less than 2 elements, your program has to raise an ArgumentError with
the message "Need at least 2 coefficients."
=================================================
Author:: Aurelien Bottazini
=end
class Polynomial
@coefficients
def initialize(coefficients)
raise ArgumentError, "Need at least 2 coefficients." unless coefficients.count > 1
@coefficients = coefficients
end
# Pretty print
def to_s
string = String.new
all_zero = true
polynomial_degree = @coefficients.size
@coefficients.each_with_index do | coef, index |
if coef != 0
all_zero = false
# displaying coefficient sign
string << sign(coef) unless index == 0 && coef > 0
# displaying coefficient when needed
string << "#{coef.abs unless coef == 1 || coef == -1}"
# computing degree for current coefficient
string << "#{x(polynomial_degree - (index + 1))}"
end
end
all_zero ? "0=0" : string
end
private
def x(degree)
case
when degree == 1
"x"
when degree > 1
"x^#{degree}"
else
String.new
end
end
def sign(coef)
coef > 0 ? "+" : "-"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment