Skip to content

Commit

Permalink
Add robot-name exercise (#373)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom authored Oct 17, 2024
1 parent 7a1eed9 commit 8a7c257
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 4
},
{
"slug": "robot-name",
"name": "Robot Name",
"uuid": "445bd3fd-011d-4a8f-aa83-5acc53225679",
"practices": [],
"prerequisites": [],
"difficulty": 4
}
]
},
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": [
"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."
}
20 changes: 20 additions & 0 deletions exercises/practice/robot-name/.meta/example.R
Original file line number Diff line number Diff line change
@@ -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 = ""
)
}
7 changes: 7 additions & 0 deletions exercises/practice/robot-name/robot-name.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
new_robot <- function() {

}

reset_robot <- function(robot) {

}
29 changes: 29 additions & 0 deletions exercises/practice/robot-name/test_robot-name.R
Original file line number Diff line number Diff line change
@@ -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)
})

0 comments on commit 8a7c257

Please sign in to comment.