public
Last active — forked from rohit/polynomial.rb

  • Download Gist
polynomial.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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
# Submission for http://rubylearning.com/blog/2009/11/26/rpcfn-rubyfun-4/
# Author: Rohit Arondekar
# Author-URL: http://rohitarondekar.com
# Author-email: hello@rohitarondekar.com
 
# Notes:
# 1. Storing whether a term is the 1st non zero term as bool in the term
# 2. Added a unit test in polynomial_test.rb
 
class Polynomial
def initialize(coeffs = [])
raise ArgumentError, 'Need at least 2 coefficients' if coeffs.size < 2
@terms = []
# The following is done to identify the first non-zero term so that inputs like [0,0,3,2,1] work
ft_not_set = true
coeffs.each_with_index do |c, i|
if(c != 0 && ft_not_set)
@terms << PolynomialTerm.new(coeffs[i], coeffs.size - i - 1, true)
ft_not_set = false
else
@terms << PolynomialTerm.new(coeffs[i], coeffs.size - i - 1)
end
end
end
def to_s
output = ''
@terms.each { |term| output = output + term.to_s }
(output == '' && '0') || output
end
end
 
 
class PolynomialTerm
def initialize(coeff, expo, ft = nil)
@coeff = coeff # Coefficient
@expo = expo # Exponent
@ft = ft || false # Boolean indicating if this is the first term
end
def to_s()
return '' if @coeff == 0
sign_of_coeff + format_coeff + format_expo
end
private
# Returns the sign of the coefficient based on whether this is the first term
def sign_of_coeff()
if @ft
@coeff < 0 ? '-' : ''
else
@coeff < 0 ? '-' : '+'
end
end
def format_expo
case @expo
when 0 then ''
when 1 then 'x'
else 'x^' + @expo.to_s
end
end
def format_coeff
if @expo == 0 || @coeff.abs > 1
@coeff.abs.to_s
else
''
end
end
end
polynomial_test.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 41 42
# assuming that your solution is polynomial.rb
require 'test/unit'
require 'test/unit/ui/console/testrunner'
require 'polynomial'
 
class Polynomial_Test < Test::Unit::TestCase
def setup
@p1 = Polynomial.new([-3,-4,1,0,6])
@p2 = Polynomial.new([1,0,2])
@p3 = Polynomial.new([-1,-2,3,0])
@p4 = Polynomial.new([0,0,0])
@p5 = Polynomial.new([0,0,1,2,-1,2])
end
 
def test_first_negative
assert_equal("-3x^4-4x^3+x^2+6", @p1.to_s)
end
 
def test_simple
assert_equal("x^2+2", @p2.to_s)
end
 
def test_first_minus_one
assert_equal("-x^3-2x^2+3x", @p3.to_s)
end
 
def test_all_zero
assert_equal("0", @p4.to_s)
end
 
def test_error
e = assert_raise(ArgumentError) { Polynomial.new([1]) }
assert_match(/Need at least 2 coefficients/, e.message)
end
# Added the following test to check for cases like [0,0,12,-1,3,4]
def test_non_zero_leading_coeffients_in_input_array
assert_equal("x^3+2x^2-x+2", @p5.to_s)
end
end
 
Test::Unit::UI::Console::TestRunner.run(Polynomial_Test)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.