Skip to content

Commit

Permalink
add squeaky-clean exercise (#327)
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbicodes authored Feb 5, 2021
1 parent 5c1882d commit c783d3f
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ pom.xml.asc
*.lein-plugins/
*.lein-failures
*.nrepl-port
.clj-kondo
.lsp
45 changes: 45 additions & 0 deletions exercises/concept/squeaky-clean/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
In this exercise you will implement a partial set of utility routines to help a developer
clean up identifier names.

In the 4 tasks you will gradually build up the routine `clean`. A valid identifier comprises
zero or more letters and underscores.

In all cases the input string is guaranteed to be non-nil. If an empty string is passed to the `clean` function, an empty string should be returned.

Note that the caller should avoid calling the routine `clean` with an empty identifier since such identifiers are ineffectual.

### 1. Replace any spaces encountered with underscores

Implement the `clean` function to replace any spaces with underscores. This also applies to leading and trailing spaces.

```clojure
(clean "my Id")
;;=> "my___Id"
```

### 2. Replace control characters with the upper case string "CTRL"

Modify the `clean` function to replace control characters with the upper case string `"CTRL"`.

```clojure
(clean "my\0Id")
;;=> "myCTRLId"
```

### 3. Convert kebab-case to camelCase

Modify the `clean` function to convert kebab-case to camelCase.

```clojure
(clean "à-ḃç")
;;=> "àḂç"
```

### 4. Omit Greek lower case letters

Modify the `clean` function to omit any Greek letters in the range 'α' to 'ω'.

```clojure
(clean "MyΟβιεγτFinder")
;;=> "MyΟFinder"
```
4 changes: 4 additions & 0 deletions exercises/concept/squeaky-clean/deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{:aliases {:test {:extra-paths ["test"]
:extra-deps {com.cognitect/test-runner {:git/url "https://github.com/cognitect-labs/test-runner.git"
:sha "209b64504cb3bd3b99ecfec7937b358a879f55c1"}}
:main-opts ["-m" "cognitect.test-runner"]}}}
35 changes: 35 additions & 0 deletions exercises/concept/squeaky-clean/src/exercism/squeaky_clean.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
(ns exercism.squeaky-clean
(:require [clojure.string :as str]))

(defn escape-ctrl-char
"If c is a control character, outputs \"CTRL\", otherwise itself"
[c]
(if (Character/isISOControl c)
"CTRL" c))

(defn escape-ctrl [s]
(apply str (map escape-ctrl-char s)))

(defn camelize [s]
(let [words (str/split s #"-")]
(if (str/includes? s "-")
(str/join "" (cons (first words)
(cons (str/upper-case (ffirst (rest words)))
(rest (first (rest words))))))
s)))

(defn letters [s]
(apply str (filter #(or (Character/isLetter %)
(= \_ %))
s)))

(defn clean-greek [s]
(apply str (filter #(not (<= 945 (int %) 969)) s)))

(defn clean [s]
(-> s
(str/replace " " "_")
camelize
escape-ctrl
letters
clean-greek))
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
(ns exercism.squeaky-clean-test
(:require [clojure.test :refer [deftest testing is]]
exercism.squeaky-clean))

(deftest clean-single-letter
(is (= "A" (exercism.squeaky-clean/clean "A"))))

(deftest clean-clean-string
(is (= "àḃç" (exercism.squeaky-clean/clean "àḃç"))))

(deftest clean-string-with-spaces
(is (= "my___Id" (exercism.squeaky-clean/clean "my Id"))))

(deftest clean-string-with-control-char
(is (= "myCTRLId" (exercism.squeaky-clean/clean "my\u0000Id"))))

(deftest clean-string-with-no-letters
(is (= "" (exercism.squeaky-clean/clean "😀😀😀"))))

(deftest clean-empty-string
(is (= "" (exercism.squeaky-clean/clean ""))))

(deftest convert-kebab-to-camel-case
(is (= "àḂç" (exercism.squeaky-clean/clean "à-ḃç"))))

(deftest omit-lower-case-greek-letters
(is (= "MyΟFinder" (exercism.squeaky-clean/clean "MyΟβιεγτFinder"))))

(deftest combine-conversions
(is (= "_AbcĐCTRL" (exercism.squeaky-clean/clean "9 -abcĐ😀ω\0"))))

0 comments on commit c783d3f

Please sign in to comment.