Created
September 15, 2016 07:48
-
-
Save sylph01/4701f09ec3b1dde6814272dc39c2f4c6 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 "minitest/autorun" | |
module InsertDummy | |
def self.insert_dummy(a) | |
dummy = "d" | |
if a.length < 2 | |
a + [dummy].cycle(3).to_a | |
else | |
case a.length % 4 | |
when 0 then a | |
when 1 then (a << dummy).insert(a.length - 4, dummy).insert(a.length - 8, dummy) | |
when 2 then (a << dummy).insert(a.length - 4, dummy) | |
when 3 then (a << dummy) | |
end | |
end | |
end | |
end | |
class TestInsertDummy < Minitest::Test | |
def test_length_after_inserting_dummy | |
assert_equal InsertDummy.insert_dummy(%w(a)).length, 4 | |
assert_equal InsertDummy.insert_dummy(%w(a a)).length, 4 | |
assert_equal InsertDummy.insert_dummy(%w(a a a)).length, 4 | |
assert_equal InsertDummy.insert_dummy(%w(a a a a)).length, 4 | |
assert_equal InsertDummy.insert_dummy(%w(a a a a a)).length, 8 | |
1000.times do | |
length = rand(100) | |
if length != 0 | |
expected_length = (length / 4.0).ceil * 4 | |
assert_equal InsertDummy.insert_dummy(["a"].cycle(length).to_a).length, expected_length | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment