diff --git a/config.json b/config.json index 0e3c780d..ef6a26f7 100644 --- a/config.json +++ b/config.json @@ -164,14 +164,6 @@ "prerequisites": [], "difficulty": 2 }, - { - "slug": "phone-number", - "name": "Phone Number", - "uuid": "c1e07140-0566-4b50-aca1-3eddaa87d4d2", - "practices": [], - "prerequisites": [], - "difficulty": 2 - }, { "slug": "resistor-color", "name": "Resistor Color", @@ -413,6 +405,14 @@ "prerequisites": [], "difficulty": 5 }, + { + "slug": "phone-number", + "name": "Phone Number", + "uuid": "c1e07140-0566-4b50-aca1-3eddaa87d4d2", + "practices": [], + "prerequisites": [], + "difficulty": 5 + }, { "slug": "robot-simulator", "name": "Robot Simulator", diff --git a/exercises/practice/phone-number/.meta/config.json b/exercises/practice/phone-number/.meta/config.json index 043d3fc9..6fcb80ff 100644 --- a/exercises/practice/phone-number/.meta/config.json +++ b/exercises/practice/phone-number/.meta/config.json @@ -3,16 +3,17 @@ "kytrinyx" ], "contributors": [ + "BNAndras", "adolfopa", "etrepum", "yurrriq" ], "files": { "solution": [ - "src/phone.lfe" + "src/phone-number.lfe" ], "test": [ - "test/phone-tests.lfe" + "test/phone-number-tests.lfe" ], "example": [ ".meta/example.lfe" diff --git a/exercises/practice/phone-number/.meta/example.lfe b/exercises/practice/phone-number/.meta/example.lfe index 9f024b80..34c61806 100644 --- a/exercises/practice/phone-number/.meta/example.lfe +++ b/exercises/practice/phone-number/.meta/example.lfe @@ -1,33 +1,25 @@ -(defmodule phone - (export (area-code 1) - (number 1) - (pprint 1))) +(defmodule phone-number + (export (clean 1))) -(defun zeros () "0000000000") +(include-lib "lfe/include/clj.lfe") -(defun strip-one - (((cons 49 tail)) tail) - (((cons _h _t)) (zeros))) - -(defun parts (str) - (let* ((number (number str)) - (area-code (string:sub_string number 1 3)) - (exchange (string:sub_string number 4 6)) - (subscriber (string:sub_string number 7 10))) - `#(,area-code ,exchange ,subscriber))) - -(defun area-code (str) - (let (((tuple area-code _ _) (parts str))) - area-code)) - -(defun number (str) - (let ((digits (re:replace str "\\D" "" `(global #(return list))))) - (case (length digits) - (10 digits) - (11 (strip-one digits)) - (_ (zeros))))) - -(defun pprint (str) - (let (((tuple area-code exchange subscriber) (parts str))) - (lists:flatten - (io_lib:format "\(~s\) ~s-~s" `(,area-code ,exchange ,subscriber))))) +(defun clean (value) + (let* ((digits (re:replace value "\\D" "" `(global #(return list)))) + (cleaned (case (length digits) + (10 digits) + (11 (string:slice digits 1)) + (_ 'false))) + (start-area-code (iff cleaned (lists:sublist cleaned 1 1))) + (start-exchange-code (iff cleaned (lists:sublist cleaned 4 1)))) + (cond ((=:= 'false cleaned) + 'false) + ((string:equal "0" start-area-code) + 'false) + ((string:equal "1" start-area-code) + 'false) + ((string:equal "0" start-exchange-code) + 'false) + ((string:equal "1" start-exchange-code) + 'false) + ('true + cleaned)))) diff --git a/exercises/practice/phone-number/src/phone-number.app.src b/exercises/practice/phone-number/src/phone-number.app.src index a2372a90..be927083 100644 --- a/exercises/practice/phone-number/src/phone-number.app.src +++ b/exercises/practice/phone-number/src/phone-number.app.src @@ -3,7 +3,7 @@ [{description, ""}, {vsn, "0.0.1"}, {modules, - [phone]}, + ['phone-number']}, {registered, []}, {applications, [kernel, stdlib]}, diff --git a/exercises/practice/phone-number/src/phone-number.lfe b/exercises/practice/phone-number/src/phone-number.lfe new file mode 100644 index 00000000..a27e8f01 --- /dev/null +++ b/exercises/practice/phone-number/src/phone-number.lfe @@ -0,0 +1,4 @@ +(defmodule phone-number + (export (clean 1))) + + ; Please implement the clean function. diff --git a/exercises/practice/phone-number/src/phone.lfe b/exercises/practice/phone-number/src/phone.lfe deleted file mode 100644 index e69de29b..00000000 diff --git a/exercises/practice/phone-number/test/phone-number-tests.lfe b/exercises/practice/phone-number/test/phone-number-tests.lfe new file mode 100644 index 00000000..1980ea9e --- /dev/null +++ b/exercises/practice/phone-number/test/phone-number-tests.lfe @@ -0,0 +1,59 @@ +(defmodule phone-number-tests + (behaviour ltest-unit) + (export all)) + +(include-lib "ltest/include/ltest-macros.lfe") + +(deftest cleans-the-number + (is-equal "2234567890" (phone-number:clean "(223) 456-7890"))) + +(deftest cleans-number-with-dots + (is-equal "2234567890" (phone-number:clean "223.456.7890"))) + +(deftest cleans-number-with-multiple-spaces + (is-equal "2234567890" (phone-number:clean "223 456 7890 "))) + +(deftest invalid-when-nine-digits + (is-not (phone-number:clean "123456789"))) + +(deftest invalid-when-eleven-digits-not-starting-with-one + (is-not (phone-number:clean "21234567890"))) + +(deftest valid-when-eleven-digits-starting-with-one + (is-equal "2234567890" (phone-number:clean "12234567890"))) + +(deftest valid-when-eleven-digits-starting-with-one-even-with-punctuation + (is-equal "2234567890" (phone-number:clean "+1 (223) 456-7890"))) + +(deftest invalid-when-more-then-11-digits + (is-not (phone-number:clean "321234567890"))) + +(deftest invalid-with-letters + (is-not (phone-number:clean "523-abc-7890"))) + +(deftest invalid-with-punctuation + (is-not (phone-number:clean "523-@:!-7890"))) + +(deftest invalid-if-area-code-starts-with-0 + (is-not (phone-number:clean "(023) 456-7890"))) + +(deftest invalid-if-area-code-starts-with-1 + (is-not (phone-number:clean "(123) 456-7890"))) + +(deftest invalid-if-exchange-code-starts-with-0 + (is-not (phone-number:clean "(223) 056-7890"))) + +(deftest invalid-if-exchange-code-starts-with-1 + (is-not (phone-number:clean "(223) 156-7890"))) + +(deftest invalid-if-area-code-starts-with-0-on-valid-11-digit-number + (is-not (phone-number:clean "1 (023) 456-7890"))) + +(deftest invalid-if-area-code-starts-with-1-on-valid-11-digit-number + (is-not (phone-number:clean "1 (123) 456-7890"))) + +(deftest invalid-if-exchange-code-starts-with-0-on-valid-11-digit-number + (is-not (phone-number:clean "1 (223) 056-7890"))) + +(deftest invalid-if-exchange-code-starts-with-1-on-valid-11-digit-number + (is-not (phone-number:clean "1 (223) 156-7890"))) diff --git a/exercises/practice/phone-number/test/phone-tests.lfe b/exercises/practice/phone-number/test/phone-tests.lfe deleted file mode 100644 index ff8e1681..00000000 --- a/exercises/practice/phone-number/test/phone-tests.lfe +++ /dev/null @@ -1,27 +0,0 @@ -(defmodule phone-tests - (behaviour ltest-unit) - (export all)) - -(include-lib "ltest/include/ltest-macros.lfe") - -(deftest cleans-number - (is-equal "1234567890" (phone:number "(123) 456-7890"))) - -(deftest cleans-number-with-dots - (is-equal "1234567890" (phone:number "123.456.7890"))) - -(deftest valid-when-eleven-digits - (is-equal "1234567890" (phone:number "11234567890"))) - -(deftest invalid-when-eleven-digits - (is-equal "0000000000" (phone:number "21234567890"))) - -(deftest invalid-when-nine-digits - (is-equal "0000000000" (phone:number "123456789"))) - -(deftest area-code - (is-equal "123" (phone:area-code "1234567890"))) - -(deftest pprint - (is-equal "(123) 456-7890" (phone:pprint "1234567890")) - (is-equal "(123) 456-7890" (phone:pprint "11234567890")))