Created
December 24, 2009 15:30
-
-
Save banyan/263232 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
class Collatz | |
def init(i) | |
buf = [i] | |
while i != 1 | |
if (i % 2) == 0 | |
i = i / 2 | |
else | |
i = i * 3 + 1 | |
end | |
buf << i | |
end | |
buf = [buf.size - 1, "[#{buf.join(', ')}]"] | |
end | |
end | |
require 'test/unit' | |
class TC_Collatz < Test::Unit::TestCase | |
def setup | |
@obj = Collatz.new | |
end | |
def test_init | |
assert_equal([6, "[10, 5, 16, 8, 4, 2, 1]"], @obj.init(10)) | |
assert_equal([12, "[17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]"], @obj.init(17)) | |
assert_equal([15, "[22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]"], @obj.init(22)) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment