Skip to content

Commit

Permalink
[New Exercise]: Robot Name (#388)
Browse files Browse the repository at this point in the history
Add exercise `Robot Name`
  • Loading branch information
glaxxie authored Jun 17, 2024
1 parent a684b4d commit e2821a7
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,14 @@
"prerequisites": [],
"difficulty": 3
},
{
"slug": "robot-name",
"name": "Robot Name",
"uuid": "0b35919e-ccba-4f45-8a03-504869e229ae",
"practices": [],
"prerequisites": [],
"difficulty": 3
},
{
"slug": "simple-linked-list",
"name": "Simple Linked List",
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.
45 changes: 45 additions & 0 deletions exercises/practice/robot-name/.meta/RobotName.example.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<#
.SYNOPSIS
Manage robot factory settings.
.DESCRIPTION
Generate a random name for a robot when it is created.
The name should be in the format of two uppercase letters followed by three digits, such as RX837 or BC811.
The robot should be able to reset and get a completely new name. (Old name that got reset should not be available for future use)
.EXAMPLE
$robot = [Robot]::new()
$robot.Name
Returns: "EX341"
#>

Class Robot {
[String]$Name
static $NameBank = [System.Collections.Generic.HashSet[string]]::new()

Robot() {
$this.Name = $this.GenerateName()
}

[string] GenerateName() {
do {
$newName = $this.GenerateNum() + $this.GenerateChars()
} while (
[Robot]::NameBank.Contains($newName)
)
[Robot]::NameBank.Add($newName)
return $newName
}

[string] GenerateNum() {
return -join ('A'..'Z' | Get-Random -Count 2)
}

[string] GenerateChars() {
return -join ('1'..'9' | Get-Random -Count 3)
}

[void] Reset() {
$this.Name = $this.GenerateName()
}
}
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": [
"glaxxie"
],
"files": {
"solution": [
"RobotName.ps1"
],
"test": [
"RobotName.tests.ps1"
],
"example": [
".meta/RobotName.example.ps1"
]
},
"blurb": "Manage robot factory settings.",
"source": "A debugging session with Paul Blackwell at gSchool."
}
26 changes: 26 additions & 0 deletions exercises/practice/robot-name/RobotName.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<#
.SYNOPSIS
Manage robot factory settings.
.DESCRIPTION
Generate a random name for a robot when it is created.
The name should be in the format of two uppercase letters followed by three digits, such as RX837 or BC811.
The robot should be able to reset and get a completely new name. (Old name that got reset should not be available for future use)
.EXAMPLE
$robot = [Robot]::new()
$robot.Name
Returns: "EX341"
#>

Class Robot {
[String]$Name

Robot() {
Throw "Please implement this class"
}

[void] Reset() {
Throw "Please implement this function"
}
}
47 changes: 47 additions & 0 deletions exercises/practice/robot-name/RobotName.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
BeforeAll {
. "./RobotName.ps1"
}

Describe "RobotName test cases" {
BeforeEach {
$robot = [Robot]::new()
}

It "robot has a name" {
$robot.Name | Should -MatchExactly "^[A-Z]{2}\d{3}$"
}

It "name is the same each time" {
$robot.Name | Should -BeExactly $robot.Name
}

It "different robot has different name" {
$robot2 = [Robot]::new()
$robot2.Name | Should -Not -Be $robot.Name
}

It "name can be reset" {
$initialName = $robot.Name
$robot.Reset()

$robot.Name | Should -Not -Be $initialName
}

It "name is valid after many reset" {
for ($i = 0; $i -lt 100; $i++) {
$initialName = $robot.Name
$robot.Reset()
$robot.Name | Should -MatchExactly "^[A-Z]{2}\d{3}$"
$robot.Name | Should -Not -Be $initialName
}
}

It "robot name are unique" {
$names = [System.Collections.Generic.HashSet[string]]::new()
for ($i = 0; $i -lt 1000; $i++) {
$robot = [Robot]::new()
$names.Add($robot.Name) | Should -BeTrue
$robot.Name | Should -MatchExactly "^[A-Z]{2}\d{3}$"
}
}
}

0 comments on commit e2821a7

Please sign in to comment.