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

Commit

Permalink
Add proverb exercise (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom authored May 7, 2023
1 parent abba7c6 commit eac4c15
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 0 deletions.
11 changes: 11 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,17 @@
],
"difficulty": 2
},
{
"slug": "proverb",
"name": "Proverb",
"uuid": "fa8ffb0a-4936-4aad-8b8e-e77116aca614",
"practices": [],
"prerequisites": [
"strings",
"vectors"
],
"difficulty": 3
},
{
"slug": "zipper",
"name": "Zipper",
Expand Down
19 changes: 19 additions & 0 deletions exercises/practice/proverb/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Instructions

For want of a horseshoe nail, a kingdom was lost, or so the saying goes.

Given a list of inputs, generate the relevant proverb.
For example, given the list `["nail", "shoe", "horse", "rider", "message", "battle", "kingdom"]`, you will output the full text of this proverbial rhyme:

```text
For want of a nail the shoe was lost.
For want of a shoe the horse was lost.
For want of a horse the rider was lost.
For want of a rider the message was lost.
For want of a message the battle was lost.
For want of a battle the kingdom was lost.
And all for the want of a nail.
```

Note that the list of inputs may vary; your solution should be able to handle lists of arbitrary length and content.
No line of the output text should be a static, unchanging string; all should vary according to the input given.
19 changes: 19 additions & 0 deletions exercises/practice/proverb/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"ErikSchierboom"
],
"files": {
"solution": [
"src/proverb.cljs"
],
"test": [
"test/proverb_test.cljs"
],
"example": [
".meta/src/example.cljs"
]
},
"blurb": "For want of a horseshoe nail, a kingdom was lost, or so the saying goes. Output the full text of this proverbial rhyme.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/For_Want_of_a_Nail"
}
16 changes: 16 additions & 0 deletions exercises/practice/proverb/.meta/src/example.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(ns proverb
(:require [clojure.string :as str]))

(defn last-line [subject]
(str "And all for the want of a " subject "."))

(defn- line [[cause-subject effect-subject]]
(str "For want of a " cause-subject " the " effect-subject " was lost."))

(defn recite [subjects]
(if (empty? subjects) ""
(->> subjects
(partition 2 1)
(map line)
(#(conj (vec %) (last-line (first subjects))))
(str/join "\n"))))
21 changes: 21 additions & 0 deletions exercises/practice/proverb/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# This is an auto-generated file. Regular comments will be removed when this
# file is regenerated. Regenerating will not touch any manually added keys,
# so comments can be added in a "comment" key.

[e974b73e-7851-484f-8d6d-92e07fe742fc]
description = "zero pieces"

[2fcd5f5e-8b82-4e74-b51d-df28a5e0faa4]
description = "one piece"

[d9d0a8a1-d933-46e2-aa94-eecf679f4b0e]
description = "two pieces"

[c95ef757-5e94-4f0d-a6cb-d2083f5e5a83]
description = "three pieces"

[433fb91c-35a2-4d41-aeab-4de1e82b2126]
description = "full proverb"

[c1eefa5a-e8d9-41c7-91d4-99fab6d6b9f7]
description = "four pieces modernized"
10 changes: 10 additions & 0 deletions exercises/practice/proverb/deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{:deps
{org.clojure/clojure {:mvn/version "1.10.1"}
org.clojure/clojurescript {:mvn/version "1.10.773"}}

:aliases
{:test
{:extra-paths ["test"]
:extra-deps
{olical/cljs-test-runner {:mvn/version "3.8.0"}}
:main-opts ["-m" "cljs-test-runner.main"]}}}
5 changes: 5 additions & 0 deletions exercises/practice/proverb/src/proverb.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(ns proverb)

(defn recite [] ;; <- arglist goes here
;; your code goes here
)
45 changes: 45 additions & 0 deletions exercises/practice/proverb/test/proverb_test.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
(ns proverb-test
(:require [clojure.test :refer [deftest is]]
[proverb :refer [recite]]
[clojure.string :as str]))

(deftest zero-pieces
(is (=
(recite ())
"")))

(deftest one-piece
(is (=
(recite '("nail"))
"And all for the want of a nail.")))

(deftest two-pieces
(is (=
(recite '("nail" "shoe"))
(str/join "\n" ["For want of a nail the shoe was lost."
"And all for the want of a nail."]))))

(deftest three-pieces
(is (=
(recite '("nail" "shoe" "horse"))
(str/join "\n" ["For want of a nail the shoe was lost."
"For want of a shoe the horse was lost."
"And all for the want of a nail."]))))

(deftest full-proverb
(is (= (recite '("nail" "shoe" "horse" "rider" "message" "battle" "kingdom"))
(str/join "\n" ["For want of a nail the shoe was lost."
"For want of a shoe the horse was lost."
"For want of a horse the rider was lost."
"For want of a rider the message was lost."
"For want of a message the battle was lost."
"For want of a battle the kingdom was lost."
"And all for the want of a nail."]))))

(deftest four-pieces-modernized
(is (=
(recite '("pin" "gun" "soldier" "battle"))
(str/join "\n" ["For want of a pin the gun was lost."
"For want of a gun the soldier was lost."
"For want of a soldier the battle was lost."
"And all for the want of a pin."]))))

0 comments on commit eac4c15

Please sign in to comment.