Skip to content

Instantly share code, notes, and snippets.

@sylph01
Created September 15, 2016 07:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sylph01/4701f09ec3b1dde6814272dc39c2f4c6 to your computer and use it in GitHub Desktop.
Save sylph01/4701f09ec3b1dde6814272dc39c2f4c6 to your computer and use it in GitHub Desktop.
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