-
-
Save RLGGHC/de5c8b0c34a06b9052ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'test/unit' | |
require 'test/unit/ui/console/testrunner' | |
require 'stevewilhelm' | |
class Polynomial_Test < Test::Unit::TestCase | |
def setup | |
@p1 = Studio831::Polynomial.new([-3,-4,1,0,6]) | |
@p2 = Studio831::Polynomial.new([1,0,2]) | |
@p3 = Studio831::Polynomial.new([-1,-2,3,0]) | |
@p4 = Studio831::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) { Studio831::Polynomial.new([1]) } | |
assert_match(/Need at least 2 coefficients/, e.message) | |
end | |
end | |
Test::Unit::UI::Console::TestRunner.run(Polynomial_Test) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# From: http://rubylearning.com/blog/2009/11/26/rpcfn-rubyfun-4/ | |
# You just started working for CoolNewCompany which is developing | |
# mathematics related software. Since you are new to the team, | |
# your boss gives you an easy task to test your abilities. | |
# | |
# 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 | |
module Studio831 | |
class Polynomial | |
def initialize x | |
raise ArgumentError, "Expecting Array", caller if ! x.instance_of?(Array) | |
raise ArgumentError, "Need at least 2 coefficients", caller if x.size <= 1 | |
x.each { |v| | |
raise ArgumentError, "Array contains non numeric elements", caller if !v.is_a? Numeric | |
} | |
@p = x | |
end | |
# Notes: | |
# reverse the array to make the rule tests below easier to test | |
# Idea for using inject inspired by Gennady Bystritsky's solution to Roman Numerals | |
# post http://www.ruby-forum.com/topic/198968 | |
def to_s | |
str = @p.reverse.each_with_index.inject("") do |s,(c,i)| | |
if (c == 0) then # rule #3: if a coefficient is 0, nothing gets added to the output | |
e = "" | |
else | |
e = "%+d" % c # rule #2: if a coefficient is negative, you have to display something like “-2x^3″ | |
e.delete! "1" if (c.abs == 1) # rule #1: if a coefficient is 1, it doesn’t get printed | |
e << "x" if (i != 0) # rule #5: x^0 == 1, so we don’t need to display it | |
e << "^%d" % i if (i > 1) # rule #4: for x^1 the ^1 part gets omitted | |
end | |
e + s | |
end | |
# clean up the special cases of 0 and leading + | |
str = "0" if str.empty? | |
str.slice!(0) if str[0].chr == "+" | |
str | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment