문제 : http://club.filltong.net/codingdojo/6437
-module(rect).
-compile(export_all).
-include_lib("eunit/include/eunit.hrl").
totalAreaOfBoxes(L) ->
area(lists:foldl(fun(X,S) -> segmentsOfBox(X) ++ S end, [], L)).
removeOverlappedSegment(L) -> sets:to_list(sets:from_list(L)).
segmentsOfBox([X1,Y1,X2,Y2]) ->
[[X,Y] || X <- lists:seq(X1,X2-1), Y <- lists:seq(Y1,Y2-1)].
area(Box=[_,_,_,_]) -> length(segmentsOfBox(Box));
area(S) -> length(removeOverlappedSegment(S)).
rect_test_() -> [
?_assertEqual( 26,
totalAreaOfBoxes( [ [1,2,4,4], [2,3,5,7], [3,1,6,5], [7,3,8,6] ] )),
?_assertEqual( 6,
totalAreaOfBoxes( [ [1,2,4,4], [1,2,4,4], [1,2,4,4], [1,2,4,4] ] )),
?_assertEqual( 15,
totalAreaOfBoxes( [ [1,2,4,4], [4,4,5,5], [5,5,6,7], [7,8,9,11] ] )),
?_assertEqual( 6, area([1,2,4,4]) )
].
다음글 우애수(Objective-C) | 달룟