Last active
February 27, 2017 01:58
-
-
Save ivportilla/13b5446decae0ac70ea412013677b5e3 to your computer and use it in GitHub Desktop.
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(submission). | |
-export([bits/1, bitsTail/1, area/1, perimeter/1, enclose/1]). | |
% Direct recursive definition of bits func. | |
bits(0) -> 0; | |
bits(1) -> 1; | |
bits(N) when (N > 1) -> N rem 2 + bits(N div 2). | |
% Tail version of the bits func. | |
bitsTail(N) -> bitsTailIt(N, 0). | |
bitsTailIt(0, Acc) -> Acc; | |
bitsTailIt(N, Acc) -> bitsTailIt(N div 2, Acc + (N rem 2)). | |
% Shapes | |
perimeter({circle, {_X, _Y}, R}) -> | |
2 * math:pi() * R; | |
perimeter({rectangle, H, W}) -> | |
2 * (H + W); | |
perimeter({triangle, {A, B}, {C, D}, {E, F}}) -> | |
S1 = math:sqrt(square(A - C) + square(B - D)), | |
S2 = math:sqrt(square(A - E) + square(B - F)), | |
S3 = math:sqrt(square(C - E) + square(D - F)), | |
S1 + S2 + S3. | |
area({circle, {_X, _Y}, R}) -> | |
math:pi() * square(R); | |
area({rectangle, H, W}) -> | |
H * W; | |
area({triangle, {A, B}, {C, D}, {E, F}}) -> | |
S1 = math:sqrt(square(A - C) + square(B - D)), | |
S2 = math:sqrt(square(A - E) + square(B - F)), | |
S3 = math:sqrt(square(C - E) + square(D - F)), | |
S = (S1 + S2 + S3) / 2, | |
math:sqrt(S*(S - S1)*(S - S2)*(S - S3)). | |
% Smallest rectangle for a Shape | |
enclose({circle, {_X, _Y}, R}) -> | |
{rectangle, 2*R, 2*R}; | |
enclose({rectangle, H, W}) -> | |
{rectangle, H, W}; | |
enclose({triangle, {A, B}, {C, D}, {E, F}}) -> | |
Highest = max(B, max(D, F)), | |
Shortet = min(B, min(D, F)), | |
Leftest = min(A, min(C, E)), | |
Rightest = max(A, max(C, E)), | |
H = (Highest - Shortet), | |
W = (Rightest - Leftest), | |
{rectangle, H, W}. | |
% Util | |
square(X) -> X * X. |
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(submission_test). | |
-include_lib("eunit/include/eunit.hrl"). | |
-import(submission, [bits/1, bitsTail/1, area/1, perimeter/1, enclose/1]). | |
isClose(N1, N2) -> | |
abs(N1 - N2) < 0.01. | |
bits_test() -> | |
?assertEqual(3, bits(7)), | |
?assertEqual(1, bits(8)), | |
?assertEqual(0, bits(0)), | |
?assertEqual(2, bits(5)). | |
bitsTail_test() -> | |
?assertEqual(3, bitsTail(7)), | |
?assertEqual(1, bitsTail(8)), | |
?assertEqual(0, bitsTail(0)), | |
?assertEqual(2, bitsTail(5)). | |
% Area and perimeter for a circle | |
circle_test() -> | |
Circle = {circle, {0, 0}, 2}, | |
?assert(isClose(area(Circle), 12.56)), | |
?assert(isClose(perimeter(Circle), 12.56)). | |
% Area and perimeter for a rectangle | |
rectangle_test() -> | |
Rectangle = {rectangle, 2, 4}, | |
?assert(isClose(area(Rectangle), 8)), | |
?assert(isClose(perimeter(Rectangle), 12)). | |
% Area and perimeter for a triangle | |
triangle_test() -> | |
Triangle = {triangle, {0, 0}, {0, 3}, {2, 0}}, | |
?assert(isClose(perimeter(Triangle), 8.60)), | |
?assert(isClose(area(Triangle), 3)). | |
% Small enclosing rectangle | |
enclose_test() -> | |
Circle = {circle, {0, 0}, 2}, | |
Rectangle = {rectangle, 2, 4}, | |
Triangle = {triangle, {0, 0}, {0, 3}, {2, 0}}, | |
?assertMatch({rectangle, 4, 4}, enclose(Circle)), | |
?assertMatch({rectangle, 2, 4}, enclose(Rectangle)), | |
?assertMatch({rectangle, 3, 2}, enclose(Triangle)). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment