Skip to content
This repository was archived by the owner on May 30, 2023. It is now read-only.

Commit 3db2b20

Browse files
Add say exercise (#148)
1 parent 4ae54aa commit 3db2b20

File tree

8 files changed

+205
-0
lines changed

8 files changed

+205
-0
lines changed

config.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,22 @@
585585
],
586586
"difficulty": 2
587587
},
588+
{
589+
"slug": "say",
590+
"name": "Say",
591+
"uuid": "f8a4f68b-df8d-469f-9969-8b4f53c1b41b",
592+
"practices": [
593+
"numbers",
594+
"strings",
595+
"conditionals"
596+
],
597+
"prerequisites": [
598+
"numbers",
599+
"strings",
600+
"conditionals"
601+
],
602+
"difficulty": 3
603+
},
588604
{
589605
"slug": "secret-handshake",
590606
"name": "Secret Handshake",
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Instructions
2+
3+
Given a number from 0 to 999,999,999,999, spell out that number in English.
4+
5+
## Step 1
6+
7+
Handle the basic case of 0 through 99.
8+
9+
If the input to the program is `22`, then the output should be `'twenty-two'`.
10+
11+
Your program should complain loudly if given a number outside the blessed range.
12+
13+
Some good test cases for this program are:
14+
15+
- 0
16+
- 14
17+
- 50
18+
- 98
19+
- -1
20+
- 100
21+
22+
### Extension
23+
24+
If you're on a Mac, shell out to Mac OS X's `say` program to talk out loud.
25+
If you're on Linux or Windows, eSpeakNG may be available with the command `espeak`.
26+
27+
## Step 2
28+
29+
Implement breaking a number up into chunks of thousands.
30+
31+
So `1234567890` should yield a list like 1, 234, 567, and 890, while the far simpler `1000` should yield just 1 and 0.
32+
33+
The program must also report any values that are out of range.
34+
35+
## Step 3
36+
37+
Now handle inserting the appropriate scale word between those chunks.
38+
39+
So `1234567890` should yield `'1 billion 234 million 567 thousand 890'`
40+
41+
The program must also report any values that are out of range.
42+
It's fine to stop at "trillion".
43+
44+
## Step 4
45+
46+
Put it all together to get nothing but plain English.
47+
48+
`12345` should give `twelve thousand three hundred forty-five`.
49+
50+
The program must also report any values that are out of range.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"ErikSchierboom"
4+
],
5+
"files": {
6+
"solution": [
7+
"src/say.cljs"
8+
],
9+
"test": [
10+
"test/say_test.cljs"
11+
],
12+
"example": [
13+
".meta/src/example.cljs"
14+
]
15+
},
16+
"blurb": "Given a number from 0 to 999,999,999,999, spell out that number in English.",
17+
"source": "A variation on the JavaRanch CattleDrive, Assignment 4",
18+
"source_url": "https://coderanch.com/wiki/718804"
19+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
(ns say
2+
(:require [clojure.pprint :as pp]
3+
[clojure.string :as s]))
4+
5+
(defn number [input]
6+
(if
7+
(or (< input 0) (> input 999999999999))
8+
(throw (js/Error. "Out of range"))
9+
(s/replace (pp/cl-format nil "~R" input) #"," "")))
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# This is an auto-generated file. Regular comments will be removed when this
2+
# file is regenerated. Regenerating will not touch any manually added keys,
3+
# so comments can be added in a "comment" key.
4+
5+
[5d22a120-ba0c-428c-bd25-8682235d83e8]
6+
description = "zero"
7+
8+
[9b5eed77-dbf6-439d-b920-3f7eb58928f6]
9+
description = "one"
10+
11+
[7c499be1-612e-4096-a5e1-43b2f719406d]
12+
description = "fourteen"
13+
14+
[f541dd8e-f070-4329-92b4-b7ce2fcf06b4]
15+
description = "twenty"
16+
17+
[d78601eb-4a84-4bfa-bf0e-665aeb8abe94]
18+
description = "twenty-two"
19+
20+
[e417d452-129e-4056-bd5b-6eb1df334dce]
21+
description = "one hundred"
22+
23+
[d6924f30-80ba-4597-acf6-ea3f16269da8]
24+
description = "one hundred twenty-three"
25+
26+
[3d83da89-a372-46d3-b10d-de0c792432b3]
27+
description = "one thousand"
28+
29+
[865af898-1d5b-495f-8ff0-2f06d3c73709]
30+
description = "one thousand two hundred thirty-four"
31+
32+
[b6a3f442-266e-47a3-835d-7f8a35f6cf7f]
33+
description = "one million"
34+
35+
[2cea9303-e77e-4212-b8ff-c39f1978fc70]
36+
description = "one million two thousand three hundred forty-five"
37+
38+
[3e240eeb-f564-4b80-9421-db123f66a38f]
39+
description = "one billion"
40+
41+
[9a43fed1-c875-4710-8286-5065d73b8a9e]
42+
description = "a big number"
43+
44+
[49a6a17b-084e-423e-994d-a87c0ecc05ef]
45+
description = "numbers below zero are out of range"
46+
47+
[4d6492eb-5853-4d16-9d34-b0f61b261fd9]
48+
description = "numbers above 999,999,999,999 are out of range"

exercises/practice/say/deps.edn

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{:deps
2+
{org.clojure/clojure {:mvn/version "1.10.1"}
3+
org.clojure/clojurescript {:mvn/version "1.10.773"}}
4+
5+
:aliases
6+
{:test
7+
{:extra-paths ["test"]
8+
:extra-deps
9+
{olical/cljs-test-runner {:mvn/version "3.8.0"}}
10+
:main-opts ["-m" "cljs-test-runner.main"]}}}

exercises/practice/say/src/say.cljs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
(ns say)
2+
3+
(defn number [num] ;; <- arglist goes here
4+
;; your code goes here
5+
)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
(ns say-test
2+
(:require [clojure.test :refer [deftest is]]
3+
say))
4+
5+
(deftest zero-test
6+
(is (= "zero" (say/number 0))))
7+
8+
(deftest one-test
9+
(is (= "one" (say/number 1))))
10+
11+
(deftest fourteen-test
12+
(is (= "fourteen" (say/number 14))))
13+
14+
(deftest twenty-test
15+
(is (= "twenty" (say/number 20))))
16+
17+
(deftest twenty-two-test
18+
(is (= "twenty-two" (say/number 22))))
19+
20+
(deftest one-hundred-test
21+
(is (= "one hundred" (say/number 100))))
22+
23+
(deftest one-hundred-twenty-three-test
24+
(is (= "one hundred twenty-three" (say/number 123))))
25+
26+
(deftest one-thousand-test
27+
(is (= "one thousand" (say/number 1000))))
28+
29+
(deftest one-thousand-two-hundred-thirty-four-test
30+
(is (= "one thousand two hundred thirty-four" (say/number 1234))))
31+
32+
(deftest one-million-test
33+
(is (= "one million" (say/number 1000000))))
34+
35+
(deftest one-million-two-thousand-three-hundred-forty-five-test
36+
(is (= "one million two thousand three hundred forty-five" (say/number 1002345))))
37+
38+
(deftest one-billion-test
39+
(is (= "one billion" (say/number 1000000000))))
40+
41+
(deftest a-big-number-test
42+
(is (= "nine hundred eighty-seven billion six hundred fifty-four million three hundred twenty-one thousand one hundred twenty-three" (say/number 987654321123))))
43+
44+
(deftest below-zero-is-out-of-range-test
45+
(is (thrown? js/Error (say/number -1))))
46+
47+
(deftest numbers-above-999999999999-out-of-range-test
48+
(is (thrown? js/Error (say/number 1000000000000))))

0 commit comments

Comments
 (0)