🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of SixFiveSoftware

SixFiveSoftware's solution

to Robot Name in the Objective-C Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

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.

Setup

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.

Source

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.

RobotNameTest.m

#import <XCTest/XCTest.h>

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

NS_ASSUME_NONNULL_BEGIN

@interface RobotNameTest : XCTestCase

@end

@implementation RobotNameTest

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

  NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:regexString
                                                                              options:NSRegularExpressionCaseInsensitive
                                                                                error:NULL];

  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);
}

@end
NS_ASSUME_NONNULL_END
//
//  RobotName.m
//  ObjCRobotName
//
//  Created by BJ Miller on 7/4/14.
//  Copyright (c) 2014 Six Five Software, LLC. All rights reserved.
//

#import "RobotName.h"

@interface Robot ()
@property (nonatomic, copy) NSString *robotName;
@end

@implementation Robot

- (NSString *)name
{
    if (!self.robotName || [self.robotName isEqualToString:@""]) {
        self.robotName = [self generateRandomString];
    }
    return self.robotName;
}

- (void)reset
{
    self.robotName = @"";
}

- (NSString *)generateRandomString
{
    NSString *result = @"";
    NSInteger count = 0;
    for (count = 0; count < 2; ++count) {
        result = [result stringByAppendingString:[self randomCharFromString:[self letters]]];
    }
    
    for (count = 0; count < 3; ++count) {
        result = [result stringByAppendingString:[self randomCharFromString:[self numbers]]];
    }
    
    return result;
}

- (NSString *)letters
{
    return @"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
}

- (NSString *)numbers
{
    return @"0123456789";
}

- (NSString *)randomCharFromString:(NSString *)inputString
{
    NSInteger randomNumber = arc4random() % inputString.length;
    return [inputString substringWithRange:NSMakeRange(randomNumber, 1)];
}

@end

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?