121

코딩 도장

일주일에 하나씩 가벼운 마음으로 코드를 작성합니다. 누가 떠밀지 않아도 스스로 자라길 원하는 사람들...
클럽장: 아샬 | http://club.filltong.net/codingdojo
최근 방문자들   모든회원보기
풀이 풀이
Four Boxes (Erlang)  

2008. 09. 15. 23:05   |   조회수:210
CoMKiD CoMKiD
http://club.filltong.net/codingdojo/6450  복사

 문제 : 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]) )
].

댓글 0

등록
목록 맨뒤로 글쓰기
다음글다음글   우애수(Objective-C)   |   
이전글이전글   구구단 (파이썬)   |