Skip to content

@Gimi /polynomial.rb
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
#!/usr/bin/env ruby
# --*-- encoding: UTF-8 --*--
# RPCFN: Ruby Fun (#4)
# @author 梁智敏(Gimi Liang) liang.gimi at gmail dot com
class Polynomial
COEFFICIENT_PATTERN = '%+d'.freeze
ANNOYING_COEFFICIENT = /\A([+-])1\Z/.freeze
# Creates a new polynomial.
# @usage
# Polynomial.new coefficients
# @overload
# @param [Array<Integer>] coefficients
# @example
# Polynomial.new [1, 2, 3, 4]
# @overload
# @param [Integer, ...] coefficients
# @example
# Polynomial.new 1, 2, 3, 4
def initialize *args
args.flatten!
raise ArgumentError, "Need at least 2 coefficients" if args.size < 2
@coefficients = args
end
# Returns a pretty-prints polynomial.
def to_s
@to_s ||=
begin
e = @coefficients.size
@coefficients.inject('') { |result, c|
e -= 1
next result if c == 0
result << (COEFFICIENT_PATTERN % c).sub(ANNOYING_COEFFICIENT, '\1')
result << (e == 0 ? '' : e == 1 && 'x' || "x^#{e}")
}.sub(/\A\+/, '').sub(/\A\Z/, '0=0')
end
end
end
# -- TESTS ------------------------------------------------
if __FILE__ == $0
require 'test/unit'
require 'test/unit/ui/console/testrunner'
class Polynomial_Test < Test::Unit::TestCase
def setup
@p1 = Polynomial.new([-3,-4,1,0,6])
@p1a = Polynomial.new(-3,-4,1,0,6)
@p2 = Polynomial.new([1,0,2])
@p2a = Polynomial.new(1,0,2)
@p3 = Polynomial.new([-1,-2,3,0])
@p3a = Polynomial.new(-1,-2,3,0)
@p4 = Polynomial.new([0,0,0])
@p4a = Polynomial.new(0,0,0)
end
def test_first_negative
assert_equal("-3x^4-4x^3+x^2+6", @p1.to_s)
assert_equal("-3x^4-4x^3+x^2+6", @p1a.to_s)
end
def test_simple
assert_equal("x^2+2", @p2.to_s)
assert_equal("x^2+2", @p2a.to_s)
end
def test_first_minus_one
assert_equal("-x^3-2x^2+3x", @p3.to_s)
assert_equal("-x^3-2x^2+3x", @p3a.to_s)
end
def test_all_zero
assert_equal("0=0", @p4.to_s)
assert_equal("0=0", @p4a.to_s)
end
def test_error
e = assert_raise(ArgumentError) { Polynomial.new([1]) }
assert_match(/Need at least 2 coefficients/, e.message)
e = assert_raise(ArgumentError) { Polynomial.new(1) }
assert_match(/Need at least 2 coefficients/, e.message)
end
end
Test::Unit::UI::Console::TestRunner.run(Polynomial_Test)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.