secret
Created — forked from hso/josesazo.rb

  • Download Gist
josesazo.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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
# polynomial.rb
#
# This is the solution to RPCFN: Ruby**Fun (#4)
# (http://rubylearning.com/blog/2009/11/26/rpcfn-rubyfun-4/).
#
# Here we have two classes: Polynomial and Term.
#
# - Polynomial is pretty simple. On contruction, it accepts an array
# containing the polynomial's coefficients
# - Polynomial#to_s calls Polynomial#generate just the first time it is
# called
# - Polynomial#generate populates @terms and injects exponents into
# place
# - Each Term instance on @terms always holds the coefficient and exponent
# numbers in spite of having special cases for zeros or ones and thus no
# need to print them
# - Term#to_s covers the special cases. It filters the output mostly
# with regular expresions: no arithmetics needed
#
# José Sazo -- jose.sazo@gmail.com
 
class Polynomial
attr_reader :coefficients, :terms, :degree
 
def initialize(array)
@coefficients = array
@degree = @coefficients.length
@terms = []
raise ArgumentError, 'Need at least 2 coefficients' unless @degree > 1
end
 
def generate
@coefficients.each do |c|
@terms << Term.new(c)
end
@terms.inject(@degree - 1) do |n, term|
term.exponent = n
n - 1
end
end
 
def to_s
generate if @terms.empty?
output = @terms.join.sub(/^\+/, '')
return "0" if output.empty?
output
end
end
 
class Term
attr_accessor :coefficient, :exponent
def initialize(coefficient)
@coefficient = coefficient
end
 
def to_s
s = @coefficient.to_s + 'x^' + @exponent.to_s
s = '+' + s unless @coefficient < 0
s.slice!("1") if @coefficient.abs == 1 and not @exponent.zero?
s.sub!(/x\^1$/, 'x')
s.sub!(/x\^0$/, '')
return '' if @coefficient.zero?
return s
end
end
### Solution code ends here ###
 
### Testing code begins here ###
#
# Added my own test cases and adapted the code for MiniTest (Ruby
# 1.9.1's Test::Unit replacement) from
# https://gist.github.com/280aa4797a580fb8ae75
 
require 'minitest/unit'
 
MiniTest::Unit.autorun
class Polynomial_Test < MiniTest::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([3,-2,+1])
@p6 = Polynomial.new([-43,0,5,0,-1])
@p7 = Polynomial.new([2,11])
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
 
# Added by me
def test_last_plus_one
assert_equal("3x^2-2x+1", @p5.to_s)
end
 
# Added by me
def test_last_minus_one
assert_equal("-43x^4+5x^2-1", @p6.to_s)
end
 
# Added by me
def test_last_eleven
assert_equal("2x+11", @p7.to_s)
end
 
def test_error
e = assert_raises(ArgumentError) { Polynomial.new([1]) }
assert_match(/Need at least 2 coefficients/, e.message)
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.