Sha256: 924e8ea06d62384c032793ab9ef68858ef810834df888463f91a6f1f23fd74e2

Contents?: true

Size: 935 Bytes

Versions: 310

Compression:

Stored size: 935 Bytes

Contents

-module(example).

-export([number/1, areacode/1, pretty_print/1, test_version/0]).

-define(ZEROS, "0000000000").

number(String) ->
  Digits = digits(String),
  case length(Digits) of
    10 -> Digits;
    11 -> strip_one(Digits);
    _ -> ?ZEROS
  end.

% Strips a leading '1' off the front of a string.
strip_one([49|T]) -> T;
strip_one([_P|_T]) -> ?ZEROS.

parts(Number) ->
  PhoneNumber = number(Number),
  AreaCode = string:sub_string(PhoneNumber, 1, 3),
  Exchange = string:sub_string(PhoneNumber, 4, 6),
  Subscriber = string:sub_string(PhoneNumber, 7, 10),
  {AreaCode, Exchange, Subscriber}.

digits(String) ->
  re:replace(String, "\\D", "", [global, {return, list}]).

areacode(String) ->
  {AreaCode, _, _} = parts(String),
  AreaCode.

pretty_print(String) ->
  {AreaCode, Exchange, Subscriber} = parts(String),
  lists:flatten(io_lib:format("\(~s\) ~s-~s", [AreaCode, Exchange, Subscriber])).

test_version() ->
    1.

Version data entries

310 entries across 310 versions & 1 rubygems

Version Path
trackler-2.2.1.139 tracks/erlang/exercises/phone-number/src/example.erl
trackler-2.2.1.138 tracks/erlang/exercises/phone-number/src/example.erl
trackler-2.2.1.137 tracks/erlang/exercises/phone-number/src/example.erl
trackler-2.2.1.136 tracks/erlang/exercises/phone-number/src/example.erl
trackler-2.2.1.135 tracks/erlang/exercises/phone-number/src/example.erl
trackler-2.2.1.134 tracks/erlang/exercises/phone-number/src/example.erl
trackler-2.2.1.133 tracks/erlang/exercises/phone-number/src/example.erl
trackler-2.2.1.132 tracks/erlang/exercises/phone-number/src/example.erl
trackler-2.2.1.131 tracks/erlang/exercises/phone-number/src/example.erl
trackler-2.2.1.130 tracks/erlang/exercises/phone-number/src/example.erl
trackler-2.2.1.129 tracks/erlang/exercises/phone-number/src/example.erl
trackler-2.2.1.128 tracks/erlang/exercises/phone-number/src/example.erl
trackler-2.2.1.127 tracks/erlang/exercises/phone-number/src/example.erl
trackler-2.2.1.126 tracks/erlang/exercises/phone-number/src/example.erl
trackler-2.2.1.125 tracks/erlang/exercises/phone-number/src/example.erl
trackler-2.2.1.124 tracks/erlang/exercises/phone-number/src/example.erl
trackler-2.2.1.123 tracks/erlang/exercises/phone-number/src/example.erl
trackler-2.2.1.122 tracks/erlang/exercises/phone-number/src/example.erl
trackler-2.2.1.121 tracks/erlang/exercises/phone-number/src/example.erl
trackler-2.2.1.120 tracks/erlang/exercises/phone-number/src/example.erl