Skip to content

Commit c783d3f

Browse files
authored
add squeaky-clean exercise (#327)
1 parent 5c1882d commit c783d3f

File tree

5 files changed

+116
-0
lines changed

5 files changed

+116
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@ pom.xml.asc
1818
*.lein-plugins/
1919
*.lein-failures
2020
*.nrepl-port
21+
.clj-kondo
22+
.lsp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
In this exercise you will implement a partial set of utility routines to help a developer
2+
clean up identifier names.
3+
4+
In the 4 tasks you will gradually build up the routine `clean`. A valid identifier comprises
5+
zero or more letters and underscores.
6+
7+
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.
8+
9+
Note that the caller should avoid calling the routine `clean` with an empty identifier since such identifiers are ineffectual.
10+
11+
### 1. Replace any spaces encountered with underscores
12+
13+
Implement the `clean` function to replace any spaces with underscores. This also applies to leading and trailing spaces.
14+
15+
```clojure
16+
(clean "my Id")
17+
;;=> "my___Id"
18+
```
19+
20+
### 2. Replace control characters with the upper case string "CTRL"
21+
22+
Modify the `clean` function to replace control characters with the upper case string `"CTRL"`.
23+
24+
```clojure
25+
(clean "my\0Id")
26+
;;=> "myCTRLId"
27+
```
28+
29+
### 3. Convert kebab-case to camelCase
30+
31+
Modify the `clean` function to convert kebab-case to camelCase.
32+
33+
```clojure
34+
(clean "à-ḃç")
35+
;;=> "àḂç"
36+
```
37+
38+
### 4. Omit Greek lower case letters
39+
40+
Modify the `clean` function to omit any Greek letters in the range 'α' to 'ω'.
41+
42+
```clojure
43+
(clean "MyΟβιεγτFinder")
44+
;;=> "MyΟFinder"
45+
```
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{:aliases {:test {:extra-paths ["test"]
2+
:extra-deps {com.cognitect/test-runner {:git/url "https://github.com/cognitect-labs/test-runner.git"
3+
:sha "209b64504cb3bd3b99ecfec7937b358a879f55c1"}}
4+
:main-opts ["-m" "cognitect.test-runner"]}}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
(ns exercism.squeaky-clean
2+
(:require [clojure.string :as str]))
3+
4+
(defn escape-ctrl-char
5+
"If c is a control character, outputs \"CTRL\", otherwise itself"
6+
[c]
7+
(if (Character/isISOControl c)
8+
"CTRL" c))
9+
10+
(defn escape-ctrl [s]
11+
(apply str (map escape-ctrl-char s)))
12+
13+
(defn camelize [s]
14+
(let [words (str/split s #"-")]
15+
(if (str/includes? s "-")
16+
(str/join "" (cons (first words)
17+
(cons (str/upper-case (ffirst (rest words)))
18+
(rest (first (rest words))))))
19+
s)))
20+
21+
(defn letters [s]
22+
(apply str (filter #(or (Character/isLetter %)
23+
(= \_ %))
24+
s)))
25+
26+
(defn clean-greek [s]
27+
(apply str (filter #(not (<= 945 (int %) 969)) s)))
28+
29+
(defn clean [s]
30+
(-> s
31+
(str/replace " " "_")
32+
camelize
33+
escape-ctrl
34+
letters
35+
clean-greek))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
(ns exercism.squeaky-clean-test
2+
(:require [clojure.test :refer [deftest testing is]]
3+
exercism.squeaky-clean))
4+
5+
(deftest clean-single-letter
6+
(is (= "A" (exercism.squeaky-clean/clean "A"))))
7+
8+
(deftest clean-clean-string
9+
(is (= "àḃç" (exercism.squeaky-clean/clean "àḃç"))))
10+
11+
(deftest clean-string-with-spaces
12+
(is (= "my___Id" (exercism.squeaky-clean/clean "my Id"))))
13+
14+
(deftest clean-string-with-control-char
15+
(is (= "myCTRLId" (exercism.squeaky-clean/clean "my\u0000Id"))))
16+
17+
(deftest clean-string-with-no-letters
18+
(is (= "" (exercism.squeaky-clean/clean "😀😀😀"))))
19+
20+
(deftest clean-empty-string
21+
(is (= "" (exercism.squeaky-clean/clean ""))))
22+
23+
(deftest convert-kebab-to-camel-case
24+
(is (= "àḂç" (exercism.squeaky-clean/clean "à-ḃç"))))
25+
26+
(deftest omit-lower-case-greek-letters
27+
(is (= "MyΟFinder" (exercism.squeaky-clean/clean "MyΟβιεγτFinder"))))
28+
29+
(deftest combine-conversions
30+
(is (= "_AbcĐCTRL" (exercism.squeaky-clean/clean "9 -abcĐ😀ω\0"))))

0 commit comments

Comments
 (0)