Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add robot-name exercise #373

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,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)
})
Loading