This repository has been archived by the owner on May 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3db2b20
commit 37ff8b1
Showing
7 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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." | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]}}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)))))) |