public
Description: deepblue's thoughts
Homepage: http://myruby.net/
Clone URL: git://github.com/deepblue/snippets.git
snippets / chalenge / jolly_jumpers / jolly.erl
100644 25 lines (19 sloc) 0.611 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
-module (jolly).
-include_lib("eunit/include/eunit.hrl").
 
% from trip.erl
subtract(A, B) -> subtract(A, B, []).
subtract([A|H], [B|T], L) -> subtract(H, T, L ++ [abs(A-B)]);
subtract([], [], L) -> L.
 
differences(A) ->
  List1 = lists:sublist(A, length(A)-1),
  List2 = lists:sublist(A, 2, length(A)),
  subtract(List1, List2).
  
is_jolly(L) ->
  lists:sort(differences(L)) == lists:seq(1, length(L)-1).
  
  
differences_test_() -> [
  ?_assert([3,2,1] == differences([1,4,2,3]))
].
 
minimum_transfer_test_() -> [
  ?_assert(true == is_jolly([1, 4, 2, 3])),
  ?_assert(false == is_jolly([1, 4, 2, -1, 6]))
].