Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of grantjk

grantjk's solution

to Robot Name in the Objective-C Track

Published at Jul 13 2018 · 0 comments
Test suite

Manage robot factory settings.

When robots come off the factory floor, they have no name.

The first time you boot them up, 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 their name gets wiped. The next time you ask, it will respond with a new random name.

The names must be random: they should not follow a predictable sequence. Random names means a risk of collisions. Your solution must ensure that every existing robot has a unique name.


There are two different methods of getting set up to run the tests with Objective-C:

  • Create an Xcode project with a test target which will run the tests.
  • Use the ruby gem objc as a test runner utility.

Both are described in more detail here: http://exercism.io/languages/objective-c

Submitting Exercises

When submitting an exercise, make sure your solution file is in the same directory as the test code.

The submit command will look something like:

exercism submit <path-to-exercism-workspace>/objective-c/robot-name/RobotName.m

You can find the Exercism workspace by running exercism debug and looking for the line beginning with Workspace.


A debugging session with Paul Blackwell at gSchool. http://gschool.it

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.


#import <XCTest/XCTest.h>

#if __has_include("RobotNameExample.h")
# import "RobotNameExample.h"
# else
# import "RobotName.h"


@interface RobotNameTest : XCTestCase


@implementation RobotNameTest

- (BOOL)string:(NSString *)string matches:(NSString *)regexString {

  NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:regexString

  NSArray *matches = [expression matchesInString:string options:0 range:NSMakeRange(0,[string length])];

  return [matches count] >= 1;

- (void)testHasName {
  Robot *robot = [[Robot alloc] init];

  NSString *name = [robot name];
  XCTAssert([self string:name matches:@"\\A\\w{2}\\d{3}\\z"], @"%@ is not a valid robot name", name);

- (void)testNameSticks {
  Robot *robot = [[Robot alloc] init];
  [robot name];
  XCTAssertEqualObjects([robot name], [robot name]);

- (void)testDifferentRobotsHaveDifferentNames {
  Robot *firstRobot = [[Robot alloc] init];
  Robot *secondRobot = [[Robot alloc] init];
  XCTAssertNotEqualObjects([firstRobot name], [secondRobot name]);

- (void)testResetName {
  Robot *robot = [[Robot alloc] init];
  NSString *firstName = [robot name];
  [robot reset];
  NSString *secondName = [robot name];

  XCTAssertNotEqualObjects(firstName, secondName);

#import "RobotName.h"

static NSString * const letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

@interface Robot ()

@property (nonatomic, copy) NSString *generatedName;


@implementation Robot

- (NSString *)name
    return self.generatedName;

- (NSString *)generatedName
    if (!_generatedName) {
        _generatedName = [[self randomName] copy];

    return _generatedName;

- (void)reset
    self.generatedName = nil;

- (NSString *)randomName
    NSMutableString *name = [NSMutableString string];

    // Random characters
    for (NSInteger i = 0; i < 2; i++) {
        u_int32_t index = arc4random_uniform((u_int32_t)letters.length);
        [name appendFormat:@"%C", [letters characterAtIndex:index]];

    // Random digits
    for (NSInteger i = 0; i < 3; i++) {
        [name appendFormat:@"%tu", arc4random_uniform(10)];

    return name;


Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other peopleโ€™s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?