-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
98 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
(defmodule phone-number | ||
(export (clean 1))) | ||
|
||
; Please implement the clean function. |
Empty file.
59 changes: 59 additions & 0 deletions
59
exercises/practice/phone-number/test/phone-number-tests.lfe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"))) |
This file was deleted.
Oops, something went wrong.