-
-
Save nikolat/1158513 to your computer and use it in GitHub Desktop.
お題:文字列を先頭から見て同じところまで除去
This file contains hidden or 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
// 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') |
This file contains hidden or 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
# -*- coding: utf-8 -*- | |
def dropStartsSame(*args): | |
args = list(args) | |
if None in args or len(args) == 0: | |
return args | |
while True: | |
if '' in args: | |
return args | |
_args = [] | |
for arg in args: | |
if arg[0] != args[0][0]: | |
return args | |
_args.append(arg[1:]) | |
args = _args | |
if __name__ == '__main__': | |
assert ['def', '123'] == dropStartsSame('abcdef', 'abc123') | |
assert [u'うえお', u'さんさん', u'どる'] == dropStartsSame(u'あいうえお', u'あいさんさん', u'あいどる') | |
assert ['12345', '67890', '12abc'] == dropStartsSame('12345', '67890', '12abc') | |
assert ['', 'b', 'bc'] == dropStartsSame('a', 'ab', 'abc') | |
assert [None, 'a', 'ab'] == dropStartsSame(None, '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