-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add exercise `Robot Name`
- Loading branch information
Showing
6 changed files
with
158 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,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() | ||
} | ||
} |
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": [ | ||
"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." | ||
} |
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,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" | ||
} | ||
} |
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 @@ | ||
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}$" | ||
} | ||
} | ||
} |