Skip to content

Commit

Permalink
Update phone-number files (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
BNAndras authored May 3, 2024
1 parent c6444a6 commit 1b3e902
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 69 deletions.
16 changes: 8 additions & 8 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
5 changes: 3 additions & 2 deletions exercises/practice/phone-number/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
54 changes: 23 additions & 31 deletions exercises/practice/phone-number/.meta/example.lfe
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))))
2 changes: 1 addition & 1 deletion exercises/practice/phone-number/src/phone-number.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[{description, ""},
{vsn, "0.0.1"},
{modules,
[phone]},
['phone-number']},
{registered, []},
{applications,
[kernel, stdlib]},
Expand Down
4 changes: 4 additions & 0 deletions exercises/practice/phone-number/src/phone-number.lfe
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 exercises/practice/phone-number/test/phone-number-tests.lfe
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")))
27 changes: 0 additions & 27 deletions exercises/practice/phone-number/test/phone-tests.lfe

This file was deleted.

0 comments on commit 1b3e902

Please sign in to comment.