Skip to content

Instantly share code, notes, and snippets.

@ksauzz
Created August 19, 2011 14:53
Show Gist options
  • Save ksauzz/1156976 to your computer and use it in GitHub Desktop.
Save ksauzz/1156976 to your computer and use it in GitHub Desktop.
お題:文字列を先頭から見て同じところまで除去 http://bit.ly/oby7HE
-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"])).
@ksauzz
Copy link
Author

ksauzz commented Aug 19, 2011

日本語は非対応

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment