Skip to content

Instantly share code, notes, and snippets.

@ygrenzinger
Last active February 26, 2017 13:49
Show Gist options
  • Save ygrenzinger/e2a9dd874be1719d6940a9954d65fc73 to your computer and use it in GitHub Desktop.
Save ygrenzinger/e2a9dd874be1719d6940a9954d65fc73 to your computer and use it in GitHub Desktop.
Future Learn Erlang Mooc - Week 1
-module(week1).
-export([perimeter/1,area/1,enclose/1, bitsD/1, bitsT/1]).
% I don't use Erlang Unit test http://erlang.org/doc/apps/eunit/chapter.html
% Because I don't know how to setup a project with dependancies at this point
% Each shape is defined by it's name, some general info and some points
perimeter({circle,R, _CenterPoint}) ->
2 * math:pi() * R;
perimeter({rectangle,W,H, _Points}) ->
(H + W) * 2;
perimeter({triangle,A,B,C, _Points}) ->
(A + B + C).
area({circle,R, _CenterPoint}) ->
math:pi() * R * R;
area({rectangle,W,H,_Points}) ->
H * W;
% I use Heron formula for triangle's area http://jwilson.coe.uga.edu/emt725/Heron/Heron.html
area({triangle,A,B,C,_Points}) ->
S = (A + B + C) / 2,
math:sqrt(S*(S-A)*(S-B)*(S-C)).
getX({X,_Y}) ->
X.
getY({_X,Y}) ->
Y.
% it's maybe a brute force solution :)
% some tests cases :
% week1:enclose({triangle,1,1,1,[{-12,-10}, {8,8}, {12, 10}]}).
% result == {rectangle,24,20,{-12,10},{12,10},{12,-10},{-12,-10}}
% week1:enclose({rectangle,10,10,[{-5,5},{5,5},{5,-5},{-5,-5}]}).
% result == {rectangle,10,10,{-5,5},{5,5},{5,-5},{-5,-5}}
enclosing_rectangle_of_points(Points) ->
Xs = lists:map(fun(Point) -> getX(Point) end, Points),
Xmax = lists:max(Xs),
Xmin = lists:min(Xs),
Ys = lists:map(fun(Point) -> getY(Point) end, Points),
Ymax = lists:max(Ys),
Ymin = lists:min(Ys),
{rectangle, (Xmax-Xmin), (Ymax-Ymin), {Xmin, Ymax}, {Xmax, Ymax}, {Xmax, Ymin}, {Xmin, Ymin}}.
enclose({circle,R,CenterPoint}) ->
Xmax = getX(CenterPoint)+R,
Xmin = getX(CenterPoint)-R,
Ymax = getY(CenterPoint)+R,
Ymin = getY(CenterPoint)-R,
{rectangle, (Xmax-Xmin), (Ymax-Ymin), {Xmin, Ymax}, {Xmax, Ymax}, {Xmax, Ymin}, {Xmin, Ymin}};
enclose({rectangle,_W,_H,Points}) ->
enclosing_rectangle_of_points(Points);
enclose({triangle,_A,_B,_C,Points}) ->
enclosing_rectangle_of_points(Points).
% with direct recursion
bitsD(0) ->
0;
bitsD(1) ->
1;
bitsD(N) ->
% i use log2 and pow2 to find the greatest binary divisor
% using trunc to get Integer from Float
BinaryPosition = erlang:trunc(math:log2(N)),
GreatestDivisor = erlang:trunc(math:pow(2,BinaryPosition)),
1 + bitsD(N rem GreatestDivisor).
% with tail cail recursion
bitsT(N) ->
bits(N, 0).
bits(0, Acc) ->
Acc;
bits(1, Acc) ->
Acc + 1;
bits(N, Acc) ->
% i use log2 and pow2 to find the greatest binary divisor
% using trunc to get Integer from Float
BinaryPosition = erlang:trunc(math:log2(N)),
GreatestDivisor = erlang:trunc(math:pow(2,BinaryPosition)),
bits(N rem GreatestDivisor, Acc+1).
% Some test cases for bits function
% week1:bitsT(63) == 6 == week1:bitsD(63)
% week1:bitsT(35) == 3 == week1:bitsD(35)
% week1:bitsT(32) == 1 == week1:bitsD(32)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment