Advertisement
wolfjb

Week 2 exercises

Jun 26th, 2017
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 3.47 KB | None | 0 0
  1. -module(second).
  2. -export([double/1, evens/1, median/1, modes/1]).
  3.  
  4. -include_lib("eunit/include/eunit.hrl").
  5. %% run unit tests as second:test().
  6.  
  7. %% transform
  8. %%
  9. %% double all elements of a list of numbers
  10. double([])     -> [];
  11. double([N|Ns]) -> [2 * N | double(Ns)].
  12.  
  13. double_test_() ->
  14.     [?_assertEqual([2,4,6], double([1,2,3])),
  15.      ?_assertEqual([], double([]))].
  16.  
  17. %% filter
  18. %%
  19. %% evens returns a list of even numbers
  20. evens([])     -> [];
  21. evens([N|Ns]) ->
  22.     case N rem 2 of
  23.         0 -> [N | evens(Ns)];
  24.         _ -> evens(Ns)
  25.     end.
  26.  
  27. evens_test_() ->
  28.     [?_assertEqual([2,4,6], evens([1,2,3,4,5,6])),
  29.      ?_assertEqual([], evens([1,3,5,7]))].
  30.  
  31. %% return the median of a list
  32. %% which is the middle value of the list or the average of the
  33. %% two middle values when the list length is even
  34. %% assumes a numeric list
  35. median([X|Xs]) ->
  36.     Len = len(Xs) + 1,
  37.     Mid = Len div 2,
  38.     case Len rem 2 of
  39.         0 ->
  40.             {Mid1, Mid2} = nthPlus1([X|Xs], Mid),
  41.             (Mid1 + Mid2) / 2;
  42.         _ ->
  43.             nth([X|Xs], Mid + 1)
  44.     end.
  45.  
  46. median_test_() ->
  47.     [?_assertEqual(7, median([1,2,3,5,7,8,16,18,22])),
  48.      ?_assertEqual(7.0, median([1,2,3,5,6,8,12,16,18,22]))].
  49.  
  50.  
  51. %% returns a list of values which occur more than once
  52. modes([X|Xs]) ->
  53.     SortedList = sort([X|Xs]),
  54.     modes(SortedList, []).
  55.  
  56. modes([], Acc) -> Acc;
  57. modes([X|Xs], Acc) ->
  58.     CountOfX = len(takeMatching(X, [X|Xs])),
  59.     Rest = dropMatching(X, [X|Xs]),
  60.     case CountOfX == 1 of
  61.     true -> modes(Rest, Acc);
  62.     false -> modes(Rest, [{X, CountOfX}|Acc])
  63.     end.
  64.  
  65. modes_test_() ->
  66.     [?_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]))].
  67.  
  68. %% Helpers for median and modes
  69. %%
  70. %% sort via quicksort
  71. %% from http://erlang.org/doc/programming_examples/list_comprehensions.html
  72. sort([]) ->
  73.     [];
  74. sort([Pivot|Tail]) ->
  75.     sort([X || X <- Tail, X < Pivot])
  76.         ++ [Pivot] ++
  77.         sort([X || X <- Tail, X >= Pivot]).
  78.  
  79. sort_test_() ->
  80.     [?_assertEqual([1,2,3,5,7,8,16,18,22], sort([3,1,16,5,8,22,7,2,18])),
  81.      ?_assertEqual([], sort([]))].
  82.  
  83. %% find the length of a list
  84. len([])            -> 0;
  85. len([H|T])         -> len([H|T], 0).
  86. len([], Count)     -> Count;
  87. len([_H|T], Count) -> len(T, Count + 1).
  88.  
  89. len_test_() ->
  90.     [?_assertEqual(9, len([1,2,3,5,7,8,16,18,22])),
  91.      ?_assertEqual(0, len([]))].
  92.  
  93. %% get a specific value from a list
  94. nth([H|_T], 1)            -> H;
  95. nth([_H|T], N) when N > 1 ->
  96.     nth(T, N - 1).
  97.  
  98. %% get a specific value and its adjacent value from a list
  99. nthPlus1([H|T], N) when N > 1 -> {nth([H|T], N), nth([H|T], N + 1)}.
  100.  
  101. nth_test()->
  102.     ?assertEqual(5, nth([1,2,3,5,7,8,16,18,22], 4)).
  103. nthPlus1_test() ->
  104.     ?assertEqual({5,7}, nthPlus1([1,2,3,5,7,8,16,18,22], 4)).
  105.  
  106. %% returns a list of values matching the initial parameter
  107. %% assumes the incoming list is sorted
  108. takeMatching(_X, [])     -> [];
  109. takeMatching(X, [X|Xs])  -> [X | takeMatching(X, Xs)];
  110. takeMatching(_X, [_Y|_]) -> [].
  111.  
  112. takeMatching_test_() ->
  113.     [?_assertEqual([1,1,1,1], takeMatching(1, [1,1,1,1,2,2,2])),
  114.      ?_assertEqual([],  takeMatching(2, [1,1,1,1]))].
  115.  
  116. %% returns a list whithout the values matching the initial parameter
  117. %% opposite of takeMatching
  118. %% assumes the incoming list is sorted
  119. dropMatching(_X, [])     -> [];
  120. dropMatching(X, [X|Xs])  -> dropMatching(X, Xs);
  121. dropMatching(_X, [Y|Xs]) -> [Y|Xs].
  122.  
  123. dropMatching_test_() ->
  124.     [?_assertEqual([2,2,2], dropMatching(1, [1,1,2,2,2]))].
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement