Created
August 19, 2011 14:53
-
-
Save ksauzz/1156976 to your computer and use it in GitHub Desktop.
お題:文字列を先頭から見て同じところまで除去 http://bit.ly/oby7HE
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
-module(ex). | |
-export([drop_starts_same/1]). | |
drop_starts_same([HList|[]]) -> | |
[HList]; | |
drop_starts_same(NestedList) -> | |
ShortList=lists:last(lists:sort(fun(A, B)->length(A)>length(B)end, NestedList)), | |
MatchCnt=lists:min(lists:map(fun(X)->match_count(ShortList, X) end, NestedList)), | |
lists:map(fun(Y)-> lists:sublist(Y, MatchCnt+1, length(Y)-MatchCnt) end, NestedList). | |
match_count(List1, List2) -> | |
match_count(List1, List2, 0). | |
match_count([], _, Cnt) -> | |
Cnt; | |
match_count([H1|T1], [H2|T2], Cnt) when H1 == H2 -> | |
match_count(T1, T2, Cnt+1); | |
match_count([_|T1], [_|T2], Cnt) -> | |
match_count(T1, T2, Cnt). | |
-include_lib("eunit/include/eunit.hrl"). | |
drop_save_test() -> | |
?assertEqual(["def"], drop_starts_same(["def"])), | |
?assertEqual(["def", "123"], drop_starts_same(["abcdef", "abc123"])), | |
?assertEqual(["12345", "67890", "12abc"], drop_starts_same(["12345", "67890", "12abc"])). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
日本語は非対応