public
Last active

  • 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
class Polynomial
def initialize(polynomial)
if polynomial.length < 2
raise ArgumentError.new("Need at least 2 coefficients")
else
@polynomial = []
polynomial.reverse.each_with_index do |x, i|
@polynomial << Term.new(x, i, (i == (polynomial.size - 1)))
end
end
end
def to_s
res = @polynomial.reverse.map(&:to_s).join("")
end
 
class Term
def initialize(coefficient, degree, first_term = false)
@coefficient = coefficient
@degree = Degree.new(degree)
@first_term = first_term
end
 
def to_s
case @coefficient.abs
when 0
@first_term ? "0" : ""
when 1
if @degree.value == 0
"#{@coefficient}"
else
"#{sign}#{@degree.to_s}"
end
else
"#{sign}#{@coefficient.abs}#{@degree.to_s}"
end
end
 
def sign
if @coefficient < 0
"-"
elsif @coefficient >= 0 && @first_term
""
else
"+"
end
end
class Degree
def initialize(degree)
@degree = degree
end
 
def value
@degree
end
def to_s
case @degree
when 0
""
when 1
"x"
else
"x^#{@degree}"
end
end
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 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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
# 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])
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
end
 
class Term_Test < Test::Unit::TestCase
def test_zero_first_term
term = Polynomial::Term.new(0, 0, true)
assert_equal("0", term.to_s)
end
 
def test_zero_term
term = Polynomial::Term.new(0, 0)
assert_equal("", term.to_s)
end
 
def test_one_coefficient_zero_degree
term = Polynomial::Term.new(1, 0)
assert_equal("1", term.to_s)
end
 
def test_one_coefficient
term = Polynomial::Term.new(1, 1, true)
assert_equal("x", term.to_s)
end
 
def test_negative
term = Polynomial::Term.new(-1, 1)
assert_equal("-x", term.to_s)
end
 
def test_positive_first
term = Polynomial::Term.new(1, 1, true)
assert_equal("x", term.to_s)
end
 
end
 
class Degree_Test < Test::Unit::TestCase
def test_zero
degree = Polynomial::Term::Degree.new(0)
assert_equal("", degree.to_s)
end
 
def test_one
degree = Polynomial::Term::Degree.new(1)
assert_equal("x", degree.to_s)
end
 
def test_mode
degree = Polynomial::Term::Degree.new(2)
assert_equal("x^2", degree.to_s)
end
 
end
Test::Unit::UI::Console::TestRunner.run(Polynomial_Test)
Test::Unit::UI::Console::TestRunner.run(Term_Test)
Test::Unit::UI::Console::TestRunner.run(Degree_Test)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.