Created
January 29, 2010 07:34
-
-
Save saivenkat/289531 to your computer and use it in GitHub Desktop.
The problem: given a list of strings, produce a list where sequential non-empty strings are concatenate
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" | |
class GlomTest < Test::Unit::TestCase | |
def glom(l) | |
l.inject([""]) do |r,x| | |
r << "" if x.empty? | |
r.last << x | |
r | |
end.reject{|a| a.empty?} | |
end | |
def test_concatinate_elements_when_list_elements_have_no_empty_between | |
l = ["a", "b", "c"] | |
assert_equal ["abc"], glom(l) | |
end | |
def test_donot_concatinate_elements_when_they_have_empty_between | |
l = ["a", "b", "", "c"] | |
assert_equal(["ab", "c"], glom(l)) | |
i = ["a", "", "", "c"] | |
assert_equal(["a", "c"], glom(i)) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment