-
Notifications
You must be signed in to change notification settings - Fork 0
/
one.erl
44 lines (36 loc) · 1014 Bytes
/
one.erl
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
-module(one).
% Compile and test:
% erlc -DTEST one.erl
% erl one -noshell -eval "eunit:test(one)" -s init stop
% Run from REPL:
% erl
% 1> c(one).
% {ok,one}
% one:captcha("82.. ").
-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
-endif.
-export([captcha/1]).
-spec captcha(unicode:chardata()) -> non_neg_integer().
captcha(Input) ->
Half = length(Input) div 2,
{First, Second} = lists:split(Half, Input),
captcha(First, Second, 0).
-spec captcha(unicode:chardata(), unicode:chardata(), non_neg_integer()) -> non_neg_integer().
captcha([], [], Accum) ->
Accum;
captcha([Head1|T1], [Head2|T2], Accum) ->
Add = Accum +
case Head1 =:= Head2 of
true -> erlang:list_to_integer([Head1]) * 2;
_ -> 0
end,
captcha(T1, T2, Add).
-ifdef(TEST).
captcha_test() ->
?assert(captcha("1212") =:= 6),
?assert(captcha("1221") =:= 0),
?assert(captcha("123425") =:= 4),
?assert(captcha("123123") =:= 12),
?assert(captcha("12131415") =:= 4).
-endif.