Created
May 15, 2020 10:57
-
-
Save Joakineee/3a5eaecf3ff2f0a28e05cf92b1a71663 to your computer and use it in GitHub Desktop.
exerciese create list
This file contains hidden or 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(constructing_lists). | |
-export([double/1,evens/1,test/0,median/1,modes/1]). | |
%double function start here. | |
double(L) -> double(L,[]). | |
double([],Acc) -> | |
reverse(Acc,[]); | |
double([H|T],Acc) -> | |
%Not ussing concat \ ++ , have to call reverse latter. | |
double(T,[H,H|Acc]). | |
%Reverse function to as we can't use lists:reverse(). | |
reverse([],Acc)-> Acc; | |
reverse([H|T],Acc) -> reverse(T,[H|Acc]). | |
%Evens function starts here | |
evens(L) -> evens(L,[]). | |
evens([],Acc) -> | |
reverse(Acc,[]); | |
evens([H|T],Acc) when H rem 2 == 0 -> | |
%Not ussing concat \ ++ , have to call reverse latter. | |
evens(T,[H|Acc]); | |
evens([_|T],Acc) -> | |
evens(T,Acc). | |
%Median function starts here: | |
median(L) -> sort(L,[],L). | |
%sorts a list | |
%params: | |
% -List: List to be sorted. | |
% -List: Classic accumulator. | |
% -List: a previous stated that will be compared with Acc to know that the list is sorted. | |
sort(_,Acc,Acc) -> calculate_median(reverse(Acc,[])); | |
sort([],Acc,_) -> | |
sort(reverse(Acc,[]),[],Acc); | |
sort([H1|[H2|[]]],Acc,Initial) -> | |
sort([],[max(H1,H2),min(H1,H2)|Acc],Initial); | |
sort([H1|[H2|T]],Acc,Initial) -> | |
sort([max(H1,H2)|T],[min(H1,H2)|Acc],Initial). | |
%Calculates the median from a sorted list. | |
calculate_median(L) -> | |
case length(L) rem 2 of | |
0 -> (get_pos(L,length(L) div 2) + get_pos(L,(length(L) div 2) + 1))/2; | |
1 -> get_pos(L,length(L) div 2) | |
end. | |
%Retruns the value of the list in the possition X. | |
get_pos([H|_],0) -> H; | |
get_pos([_|T],X) -> get_pos(T,X-1). | |
%% | |
%% modes exercise statrts here. | |
%% | |
modes(L) -> create_tuple_list(L,[]). | |
%Devuelve el numero de veces que aparece un numero en una lista: | |
number_count([],_,Acc) -> Acc; | |
number_count([X|T],X,Acc) -> number_count(T,X,Acc + 1); | |
number_count([_|T],X,Acc) -> number_count(T,X,Acc). | |
%Check if the number is in a list of tuples {Number,Times} | |
%if is not there adds the tuple {Number,Times} to the Acc. | |
create_tuple_list([],Acc) -> more_coincidences(Acc,0,[]); | |
create_tuple_list([H|T],Acc) -> | |
Tuple = {H,number_count(T,H,1)}, | |
case insert_tuple(Tuple,Acc) of | |
true -> create_tuple_list(T,[Tuple|Acc]); | |
_ -> create_tuple_list(T,Acc) | |
end. | |
%Checks if the tuple is already in the list. | |
insert_tuple(_,[]) -> true; | |
insert_tuple({X,_},[{X,_}|_]) -> false; | |
insert_tuple(Tuple,[_|T]) -> insert_tuple(Tuple,T). | |
%Creates a list with the tuples with more coincidences. | |
more_coincidences([],_,Acc) -> final_list(Acc,[]); | |
more_coincidences([{_,Y}|T],MaxY,Acc) when Y < MaxY -> more_coincidences(T,MaxY,Acc); | |
more_coincidences([{X,Y}|T],Y,Acc) -> more_coincidences(T,Y,[{X,Y}|Acc]); | |
more_coincidences([{X,Y}|T],_,_) -> more_coincidences(T,Y,[{X,Y}]). | |
%Creates the final list. | |
final_list([],Acc) -> Acc; | |
final_list([{X,_}|T],Acc) -> final_list(T,[X|Acc]). | |
test() -> | |
[1,1,2,2,3,3] = constructing_lists:double([1,2,3]), | |
[] = constructing_lists:double([]), | |
[1,1] = constructing_lists:double([1]), | |
[2,4,6,8] = constructing_lists:evens([1,2,3,4,6,8]), | |
[] = constructing_lists:evens([]), | |
[] = constructing_lists:evens([1]), | |
[2] = constructing_lists:evens([2]), | |
[0] = constructing_lists:evens([0]), | |
26.5 = constructing_lists:median([4,1,2,6,8,523,45,6576]), | |
6 = constructing_lists:median([4,1,2,6,8,523,3,45,6576]), | |
3 = constructing_lists:number_count([3,4,3,7,8,6,6,5,6],3,1), | |
false = constructing_lists:insert_tuple({3,4},[{3,4}]), | |
true = constructing_lists:insert_tuple({3,4},[]), | |
[6,3] = constructing_lists:modes([2,3,3,4,3,7,8,6,6,5,6]), | |
[1] = constructing_lists:modes([1]), | |
[6] = constructing_lists:modes([1,2,3,4,6,7,8,6,8,6]), | |
ok. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment