Skip to content

Instantly share code, notes, and snippets.

@ksauzz

ksauzz/ex.erl

Created Aug 19, 2011
Embed
What would you like to do?
お題:文字列を先頭から見て同じところまで除去 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

This comment has been minimized.

Copy link
Owner Author

@ksauzz ksauzz commented Aug 19, 2011

日本語は非対応

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