Skip to content

Instantly share code, notes, and snippets.

@jaksonmoura
Created October 3, 2011 15:57
Show Gist options
  • Select an option

  • Save jaksonmoura/1259442 to your computer and use it in GitHub Desktop.

Select an option

Save jaksonmoura/1259442 to your computer and use it in GitHub Desktop.
Chop
# http://codekata.pragprog.com/2007/01/kata_two_karate.html
class Chop
def chop(int, ar)
return -1 unless ar.include?(int)
return ar.index(int)
end
end
require 'test/unit'
require './Chop'
class TestChop < Test::Unit::TestCase
def setup
@chop = Chop.new
end
def teardown
@chop = nil
end
def test_chop
assert_equal(-1, @chop.chop(0, [1, 2, 3]))
assert_equal(0, @chop.chop(1, [1, 2, 3]))
assert_equal(2, @chop.chop(3, [1, 2, 3]))
#
assert_equal(0, @chop.chop(1, [1, 3, 5]))
assert_equal(1, @chop.chop(3, [1, 3, 5]))
assert_equal(2, @chop.chop(5, [1, 3, 5]))
assert_equal(-1, @chop.chop(0, [1, 3, 5]))
assert_equal(-1, @chop.chop(2, [1, 3, 5]))
assert_equal(-1, @chop.chop(4, [1, 3, 5]))
assert_equal(-1, @chop.chop(6, [1, 3, 5]))
#
assert_equal(0, @chop.chop(1, [1, 3, 5, 7]))
assert_equal(1, @chop.chop(3, [1, 3, 5, 7]))
assert_equal(2, @chop.chop(5, [1, 3, 5, 7]))
assert_equal(3, @chop.chop(7, [1, 3, 5, 7]))
assert_equal(-1, @chop.chop(0, [1, 3, 5, 7]))
assert_equal(-1, @chop.chop(2, [1, 3, 5, 7]))
assert_equal(-1, @chop.chop(4, [1, 3, 5, 7]))
assert_equal(-1, @chop.chop(6, [1, 3, 5, 7]))
assert_equal(-1, @chop.chop(8, [1, 3, 5, 7]))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment