diff --git a/config.json b/config.json index d8542af..4f8fe84 100644 --- a/config.json +++ b/config.json @@ -816,6 +816,14 @@ "practices": [], "prerequisites": [], "difficulty": 4 + }, + { + "slug": "robot-name", + "name": "Robot Name", + "uuid": "445bd3fd-011d-4a8f-aa83-5acc53225679", + "practices": [], + "prerequisites": [], + "difficulty": 4 } ] }, diff --git a/exercises/practice/robot-name/.docs/instructions.md b/exercises/practice/robot-name/.docs/instructions.md new file mode 100644 index 0000000..fca3a41 --- /dev/null +++ b/exercises/practice/robot-name/.docs/instructions.md @@ -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. diff --git a/exercises/practice/robot-name/.meta/config.json b/exercises/practice/robot-name/.meta/config.json new file mode 100644 index 0000000..0867167 --- /dev/null +++ b/exercises/practice/robot-name/.meta/config.json @@ -0,0 +1,18 @@ +{ + "authors": [ + "erikschierboom" + ], + "files": { + "solution": [ + "robot-name.R" + ], + "test": [ + "test_robot-name.R" + ], + "example": [ + ".meta/example.R" + ] + }, + "blurb": "Manage robot factory settings.", + "source": "A debugging session with Paul Blackwell at gSchool." +} diff --git a/exercises/practice/robot-name/.meta/example.R b/exercises/practice/robot-name/.meta/example.R new file mode 100644 index 0000000..2166cdc --- /dev/null +++ b/exercises/practice/robot-name/.meta/example.R @@ -0,0 +1,20 @@ +new_robot <- function() { + structure( + list( + name = random_name() + ), + class = "robot" + ) +} + +reset_robot <- function(robot) { + robot$name <- random_name() + robot +} + +random_name <- function() { + paste0( + c(sample(LETTERS, 2, replace = TRUE), sample(0:9, 3, replace = TRUE)), + collapse = "" + ) +} diff --git a/exercises/practice/robot-name/robot-name.R b/exercises/practice/robot-name/robot-name.R new file mode 100644 index 0000000..9078137 --- /dev/null +++ b/exercises/practice/robot-name/robot-name.R @@ -0,0 +1,7 @@ +new_robot <- function() { + +} + +reset_robot <- function(robot) { + +} diff --git a/exercises/practice/robot-name/test_robot-name.R b/exercises/practice/robot-name/test_robot-name.R new file mode 100644 index 0000000..40d5dc1 --- /dev/null +++ b/exercises/practice/robot-name/test_robot-name.R @@ -0,0 +1,29 @@ +source("./robot-name.R") +library(testthat) + +test_that("Robot has a class", { + robot <- new_robot() + expect_s3_class(robot, "robot") +}) + +test_that("Robot has a name", { + robot <- new_robot() + expect_match(robot$name, "^[A-Z]{2}\\d{3}$") +}) + +test_that("Name is the same each time", { + robot <- new_robot() + expect_equal(robot$name, robot$name) +}) + +test_that("Two different robots have different names", { + robot1 <- new_robot() + robot2 <- new_robot() + expect_true(robot1$name != robot2$name) +}) + +test_that("Can reset the name", { + robot <- new_robot() + reset_robot <- reset_robot(robot) + expect_true(robot$name != reset_robot$name) +})