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

Commit

Permalink
Add robot-name exercise (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom authored May 8, 2023
1 parent 3db2b20 commit 37ff8b1
Show file tree
Hide file tree
Showing 7 changed files with 129 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": "robot-name",
"name": "Robot Name",
"uuid": "33c4d67c-8365-41d2-9a3e-c41bbf0dd131",
"practices": [],
"prerequisites": [
"numbers",
"strings"
],
"difficulty": 2
},
{
"slug": "say",
"name": "Say",
Expand Down
14 changes: 14 additions & 0 deletions exercises/practice/robot-name/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Instructions

Manage robot factory settings.

When a robot comes off the factory floor, it has no name.

The first time you turn on a robot, a random name is generated in the format of two uppercase letters followed by three digits, such as RX837 or BC811.

Every once in a while we need to reset a robot to its factory settings, which means that its name gets wiped.
The next time you ask, that robot will respond with a new random name.

The names must be random: they should not follow a predictable sequence.
Using random names means a risk of collisions.
Your solution must ensure that every existing robot has a unique name.
18 changes: 18 additions & 0 deletions exercises/practice/robot-name/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"authors": [
"ErikSchierboom"
],
"files": {
"solution": [
"src/robot_name.cljs"
],
"test": [
"test/robot_name_test.cljs"
],
"example": [
".meta/src/example.cljs"
]
},
"blurb": "Manage robot factory settings.",
"source": "A debugging session with Paul Blackwell at gSchool."
}
16 changes: 16 additions & 0 deletions exercises/practice/robot-name/.meta/src/example.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(ns robot-name)

(def ^:private letters (map char (range 65 91)))
(defn- generate-name []
(apply str
(concat (repeatedly 2 #(rand-nth letters))
(repeatedly 3 #(rand-int 10)))))

(defn robot []
(atom {:name (generate-name)}))

(defn robot-name [robot]
(:name @robot))

(defn reset-name [robot]
(swap! robot assoc :name (generate-name)))
10 changes: 10 additions & 0 deletions exercises/practice/robot-name/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"]}}}
13 changes: 13 additions & 0 deletions exercises/practice/robot-name/src/robot_name.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(ns robot-name)

(defn robot [] ;; <- arglist goes here
;; your code goes here
)

(defn robot-name [robot] ;; <- arglist goes here
;; your code goes here
)

(defn reset-name [robot] ;; <- arglist goes here
;; your code goes here
)
47 changes: 47 additions & 0 deletions exercises/practice/robot-name/test/robot_name_test.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
(ns robot-name-test
(:require [clojure.test :refer [deftest is testing]]
robot-name))

(deftest robot-name
(let [a-robot (robot-name/robot)
its-name (robot-name/robot-name a-robot)]
(is (re-seq #"[A-Z]{2}\d{3}" its-name))))

(deftest name-matches-pattern
(let [a-robot (robot-name/robot)
its-name (robot-name/robot-name a-robot)]
(is (= its-name (robot-name/robot-name a-robot)))))

(deftest different-robots-different-names
(let [a-robot (robot-name/robot)
its-name (robot-name/robot-name a-robot)]
(is (not= its-name (-> (robot-name/robot) robot-name/robot-name)))))

(deftest new-name-matches
(let [a-robot (robot-name/robot)
its-original-name (robot-name/robot-name a-robot)
its-new-name (do (robot-name/reset-name a-robot)
(robot-name/robot-name a-robot))]
(is (re-seq #"[A-Z]{2}\d{3}" its-new-name))))

(deftest new-name-different
(let [a-robot (robot-name/robot)
its-original-name (robot-name/robot-name a-robot)
its-new-name (do (robot-name/reset-name a-robot)
(robot-name/robot-name a-robot))]
(is (not= its-original-name its-new-name))))

(deftest new-name-does-not-change-until-reset
(let [a-robot (robot-name/robot)
its-original-name (robot-name/robot-name a-robot)
its-new-name (do (robot-name/reset-name a-robot)
(robot-name/robot-name a-robot))]
(is (= its-new-name (robot-name/robot-name a-robot)))))

(deftest new-names-different-each-time
(let [a-robot (robot-name/robot)
its-original-name (robot-name/robot-name a-robot)
its-new-name (do (robot-name/reset-name a-robot)
(robot-name/robot-name a-robot))]
(is (not= its-new-name (do (robot-name/reset-name a-robot)
(robot-name/robot-name a-robot))))))

0 comments on commit 37ff8b1

Please sign in to comment.