Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -module(second).
- -export([double/1, evens/1, median/1, modes/1]).
- -include_lib("eunit/include/eunit.hrl").
- %% run unit tests as second:test().
- %% transform
- %%
- %% double all elements of a list of numbers
- double([]) -> [];
- double([N|Ns]) -> [2 * N | double(Ns)].
- double_test_() ->
- [?_assertEqual([2,4,6], double([1,2,3])),
- ?_assertEqual([], double([]))].
- %% filter
- %%
- %% evens returns a list of even numbers
- evens([]) -> [];
- evens([N|Ns]) ->
- case N rem 2 of
- 0 -> [N | evens(Ns)];
- _ -> evens(Ns)
- end.
- evens_test_() ->
- [?_assertEqual([2,4,6], evens([1,2,3,4,5,6])),
- ?_assertEqual([], evens([1,3,5,7]))].
- %% return the median of a list
- %% which is the middle value of the list or the average of the
- %% two middle values when the list length is even
- %% assumes a numeric list
- median([X|Xs]) ->
- Len = len(Xs) + 1,
- Mid = Len div 2,
- case Len rem 2 of
- 0 ->
- {Mid1, Mid2} = nthPlus1([X|Xs], Mid),
- (Mid1 + Mid2) / 2;
- _ ->
- nth([X|Xs], Mid + 1)
- end.
- median_test_() ->
- [?_assertEqual(7, median([1,2,3,5,7,8,16,18,22])),
- ?_assertEqual(7.0, median([1,2,3,5,6,8,12,16,18,22]))].
- %% returns a list of values which occur more than once
- modes([X|Xs]) ->
- SortedList = sort([X|Xs]),
- modes(SortedList, []).
- modes([], Acc) -> Acc;
- modes([X|Xs], Acc) ->
- CountOfX = len(takeMatching(X, [X|Xs])),
- Rest = dropMatching(X, [X|Xs]),
- case CountOfX == 1 of
- true -> modes(Rest, Acc);
- false -> modes(Rest, [{X, CountOfX}|Acc])
- end.
- modes_test_() ->
- [?_assertEqual([{5,2},{3,6},{1,8}], modes([3,1,3,5,1,3,1,1,3,3,3,1,1,2,1,1,5]))].
- %% Helpers for median and modes
- %%
- %% sort via quicksort
- %% from http://erlang.org/doc/programming_examples/list_comprehensions.html
- sort([]) ->
- [];
- sort([Pivot|Tail]) ->
- sort([X || X <- Tail, X < Pivot])
- ++ [Pivot] ++
- sort([X || X <- Tail, X >= Pivot]).
- sort_test_() ->
- [?_assertEqual([1,2,3,5,7,8,16,18,22], sort([3,1,16,5,8,22,7,2,18])),
- ?_assertEqual([], sort([]))].
- %% find the length of a list
- len([]) -> 0;
- len([H|T]) -> len([H|T], 0).
- len([], Count) -> Count;
- len([_H|T], Count) -> len(T, Count + 1).
- len_test_() ->
- [?_assertEqual(9, len([1,2,3,5,7,8,16,18,22])),
- ?_assertEqual(0, len([]))].
- %% get a specific value from a list
- nth([H|_T], 1) -> H;
- nth([_H|T], N) when N > 1 ->
- nth(T, N - 1).
- %% get a specific value and its adjacent value from a list
- nthPlus1([H|T], N) when N > 1 -> {nth([H|T], N), nth([H|T], N + 1)}.
- nth_test()->
- ?assertEqual(5, nth([1,2,3,5,7,8,16,18,22], 4)).
- nthPlus1_test() ->
- ?assertEqual({5,7}, nthPlus1([1,2,3,5,7,8,16,18,22], 4)).
- %% returns a list of values matching the initial parameter
- %% assumes the incoming list is sorted
- takeMatching(_X, []) -> [];
- takeMatching(X, [X|Xs]) -> [X | takeMatching(X, Xs)];
- takeMatching(_X, [_Y|_]) -> [].
- takeMatching_test_() ->
- [?_assertEqual([1,1,1,1], takeMatching(1, [1,1,1,1,2,2,2])),
- ?_assertEqual([], takeMatching(2, [1,1,1,1]))].
- %% returns a list whithout the values matching the initial parameter
- %% opposite of takeMatching
- %% assumes the incoming list is sorted
- dropMatching(_X, []) -> [];
- dropMatching(X, [X|Xs]) -> dropMatching(X, Xs);
- dropMatching(_X, [Y|Xs]) -> [Y|Xs].
- dropMatching_test_() ->
- [?_assertEqual([2,2,2], dropMatching(1, [1,1,2,2,2]))].
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement