Skip to content

Instantly share code, notes, and snippets.

@JunichiIto
Created April 25, 2015 22:26
Show Gist options
  • Save JunichiIto/d4aa039f4b8aad040453 to your computer and use it in GitHub Desktop.
Save JunichiIto/d4aa039f4b8aad040453 to your computer and use it in GitHub Desktop.
require 'test/unit'
require 'csv'
class FillBlankTest < Test::Unit::TestCase
def fill_blank(csv_text)
CSV.parse(csv_text).inject([]) { |results, row|
results << row.zip(results.last || []).map { |curr, prev| curr || prev }
}
.map { |row| row.join(',') }
.join("\n")
end
def test_fill_blank
input = <<-TEXT
日本,東京,新宿
,,渋谷
,兵庫,神戸
アメリカ,カリフォルニア,サンフランシスコ
,,ロサンゼルス
TEXT
output = <<-TEXT.chomp
日本,東京,新宿
日本,東京,渋谷
日本,兵庫,神戸
アメリカ,カリフォルニア,サンフランシスコ
アメリカ,カリフォルニア,ロサンゼルス
TEXT
assert_equal output, fill_blank(input)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment