Skip to content
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 @@ -490,6 +490,14 @@
"prerequisites": [],
"difficulty": 4
},
{
"slug": "robot-name",
"name": "Robot Name",
"uuid": "9c506aa1-3dfd-4530-a44a-200730289fbf",
"practices": [],
"prerequisites": [],
"difficulty": 4
},
{
"slug": "robot-simulator",
"name": "Robot Simulator",
Expand Down
5 changes: 5 additions & 0 deletions exercises/practice/robot-name/.busted
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
return {
default = {
ROOT = { '.' }
}
}
15 changes: 15 additions & 0 deletions exercises/practice/robot-name/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Hints

## OO Programming

* `reset_names` is a **class method**.
See [Class Variables][class-vars] in the MoonScript Language Guide.
* You'll need to track state across all robot instances (which names have been assigned).
Class variables are perfect for this -- but think about what other approaches might work too.

## Performance Hints

* A good strategy for assigning names to all the robots is to generate all 676,000 names first and shuffle that list.
Then, each new robot can simply take the next name from the shuffled list.

[class-vars]: https://moonscript.org/reference/#the-language/object-oriented-programming/class-variables
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": [
"glennj"
],
"files": {
"solution": [
"robot_name.moon"
],
"test": [
"robot_name_spec.moon"
],
"example": [
".meta/example.moon"
]
},
"blurb": "Manage robot factory settings.",
"source": "A debugging session with Paul Blackwell at gSchool."
}
27 changes: 27 additions & 0 deletions exercises/practice/robot-name/.meta/example.moon
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-- https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#Modern_method
table.shuffle = (t) ->
for i = #t, 2, -1
j = math.random i
t[i], t[j] = t[j], t[i]


-- Generate all the names
NAMES = [string.format '%c%c%03d', a, b, c for a = 65,90 for b = 65,90 for c = 0,999]
table.shuffle NAMES


class Robot
@idx: 0

@reset_names: =>
@idx = 0

new: =>
@reset!

reset: =>
@@idx += 1
assert @@idx <= #NAMES, 'all names taken'
@_name = NAMES[@@idx]

name: => @_name
13 changes: 13 additions & 0 deletions exercises/practice/robot-name/robot_name.moon
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Robot
@reset_names: =>
error 'Implement the class method: reset_names'

new: =>
error 'Implement the constructor'

reset: =>
error 'Implement the reset method'

name: => @id
error 'Implement the names method'

75 changes: 75 additions & 0 deletions exercises/practice/robot-name/robot_name_spec.moon
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import table_size from require './test_helpers'

Robot = require 'robot_name'

describe 'robot-name', ->
describe 'one robot', ->
it 'a robot has a name', ->
robot = Robot!
name = robot\name!
assert.is.match name, '^[A-Z][A-Z][0-9][0-9][0-9]$'

pending 'name does not change', ->
robot = Robot!
name1 = robot\name!
name2 = robot\name!
assert.are.equal name1, name2

pending 'reset changes name', ->
robot = Robot!
name1 = robot\name!
robot\reset!
name2 = robot\name!
assert.not.equal name1, name2

pending 'reset before name called does not cause an error', ->
f = ->
robot = Robot!
robot\reset!
robot\name!
assert.has_no.errors f

describe 'two robots', ->
pending 'different robots have different names', ->
r1 = Robot!
r2 = Robot!
assert.not.equal r1\name!, r2\name!

pending 'names must be random not consecutive', ->
Robot\reset_names!
diffs = {}
for _ = 1, 10
n1 = Robot!\name!
n2 = Robot!\name!
-- a robot name is a valid base-36 number
diff = tonumber(n1, 36) - tonumber(n2, 36)
diffs[diff] = true
assert.not.equal 1, table_size diffs

describe 'lots of robots', ->
pending 'a large number of robots have unique names', ->
sample_size = 10000
seen = {}
for i = 1, sample_size
name = Robot!\name!
seen[name] = true
assert.is.equal sample_size, table_size seen


-- The next test is optional.
-- Change `test_all_robots` to `true` to run it.
-- Check the performance tip in the hints.
test_all_robots = false

if test_all_robots
pending 'all the robots and more', ->
Robot\reset_names!
sample_size = 26 * 26 * 1000
seen = {}
for i = 1, sample_size
name = Robot!\name!
seen[name] = true
assert.is.equal sample_size, table_size seen
-- the 676,001st robot
f = -> Robot!
assert.has_error f, 'all names taken'
21 changes: 21 additions & 0 deletions exercises/practice/robot-name/test_helpers.moon
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-- create a custom assertion: does a string match a pattern
assert = require 'luassert'
say = require 'say'

match = (state, arguments) ->
{str, patt} = arguments
string.match str, patt

say\set 'assertion.match.positive', 'Expected %s to match pattern %s.'
say\set 'assertion.match.negative', 'Expected %s not to match pattern %s.'
assert\register 'assertion', 'match', match, 'assertion.match.positive', 'assertion.match.negative'


-- return the helper functions
{
table_size: (t) ->
-- because `#t` is insufficient for non-sequence tables
count = 0
count += 1 for _ in pairs t
count
}
Loading