Skip to content

Instantly share code, notes, and snippets.

@fumokmm
Created August 12, 2011 16:45
Show Gist options
  • Save fumokmm/1142439 to your computer and use it in GitHub Desktop.
Save fumokmm/1142439 to your computer and use it in GitHub Desktop.
お題:文字列を先頭から見て同じところまで除去 solved by Groovy
// http://d.hatena.ne.jp/fumokmm/20110812/1313138407
def dropStartsSame(String... args) {
if (args.any{!it}) return args
args*.substring(
args*.toList().transpose().with{
def i=0; it.collect{[i++, it]}
}.with{ dup ->
dup.find{ it[1].unique().size() != 1 }.with {
it ? it[0] : dup.last()[0]+1
}
}
)
}
assert ['def', '123'] == dropStartsSame('abcdef', 'abc123')
assert ['うえお', 'さんさん', 'どる'] == dropStartsSame('あいうえお', 'あいさんさん', 'あいどる')
assert ['12345', '67890', '12abc'] == dropStartsSame('12345', '67890', '12abc')
assert ['', 'b', 'bc'] == dropStartsSame('a', 'ab', 'abc')
assert [null, 'a', 'ab'] == dropStartsSame(null, 'a', 'ab')
assert ['', 'a', 'ab'] == dropStartsSame('', 'a', 'ab')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment