# 4d47's solution

## to Phone Number in the Clojure Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Clean up user-entered phone numbers so that they can be sent SMS messages.

The North American Numbering Plan (NANP) is a telephone numbering system used by many countries in North America like the United States, Canada or Bermuda. All NANP-countries share the same international country code: `1`.

NANP numbers are ten-digit numbers consisting of a three-digit Numbering Plan Area code, commonly known as area code, followed by a seven-digit local number. The first three digits of the local number represent the exchange code, followed by the unique four-digit number which is the subscriber number.

The format is usually represented as

``````(NXX)-NXX-XXXX
``````

where `N` is any digit from 2 through 9 and `X` is any digit from 0 through 9.

Your task is to clean up differently formatted telephone numbers by removing punctuation and the country code (1) if present.

For example, the inputs

• `+1 (613)-995-0253`
• `613-995-0253`
• `1 613 995 0253`
• `613.995.0253`

should all produce the output

`6139950253`

Note: As this exercise only deals with telephone numbers used in NANP-countries, only 1 is considered a valid country code.

## Source

Event Manager by JumpstartLab http://tutorials.jumpstartlab.com/projects/eventmanager.html

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### phone_number_test.clj

``````(ns phone-number-test
(:require [clojure.test :refer [deftest is]]
phone-number))

(deftest cleans-number
(is (= "1234567890" (phone-number/number "(123) 456-7890"))))

(deftest cleans-number-with-dots
(is (= "5558675309" (phone-number/number "555.867.5309"))))

(deftest valid-when-11-digits-and-first-is-1
(is (= "9876543210" (phone-number/number "19876543210"))))

(deftest invalid-when-11-digits
(is (= "0000000000" (phone-number/number "21234567890"))))

(deftest invalid-when-9-digits
(is (= "0000000000" (phone-number/number "123456789"))))

(deftest area-code
(is (= "123" (phone-number/area-code "1234567890"))))

(deftest area-code-with-dots
(is (= "555" (phone-number/area-code "555.867.5309"))))

(deftest area-code-with-parentheses
(is (= "987" (phone-number/area-code "(987) 654-3210"))))

(deftest area-code-with-full-us-phone-number
(is (= "123" (phone-number/area-code "11234567890"))))

(deftest pretty-print
(is (= "(123) 456-7890" (phone-number/pretty-print "1234567890"))))

(deftest pretty-print-with-dots
(is (= "(555) 867-5309" (phone-number/pretty-print "555.867.5309"))))

(deftest pretty-print-with-full-us-phone-number
(is (= "(987) 654-3210" (phone-number/pretty-print "19876543210"))))``````
``````(ns phone-number)

(defn number [s]
{:post [(= (count %) 10)]}
(let [s (apply str (filter #(Character/isDigit %) s))
cnt (count s)]
(cond
(= 10 cnt) s
(and (= 11 cnt) (= \1 (first s))) (subs s 1)
:else "0000000000")))

(defn area-code [s]
(subs (number s) 0 3))

(defn prefix [s]
(subs (number s) 3 6))

(defn line [s]
(subs (number s) 6 10))

(defn pretty-print [s]
(format "(%s) %s-%s" (area-code s) (prefix s) (line s)))``````