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

KiligWY's solution

to Secret Handshake in the Objective-C Track

Published at Jul 13 2018 · 0 comments
Test suite

There are 10 types of people in the world: Those who understand binary, and those who don't.

You and your fellow cohort of those in the "know" when it comes to binary decide to come up with a secret "handshake".

1 = wink
10 = double blink
100 = close your eyes
1000 = jump

10000 = Reverse the order of the operations in the secret handshake.

Given a decimal number, convert it to the appropriate sequence of events for a secret handshake.

Here's a couple of examples:

Given the input 3, the function would return the array ["wink", "double blink"] because 3 is 11 in binary.

Given the input 19, the function would return the array ["double blink", "wink"] because 19 is 10011 in binary. Notice that the addition of 16 (10000 in binary) has caused the array to be reversed.


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/secret-handshake/SecretHandshake.m

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


Bert, in Mary Poppins http://www.imdb.com/title/tt0058331/quotes/qt0437047

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("SecretHandshakeExample.h")
# import "SecretHandshakeExample.h"
# else
# import "SecretHandshake.h"

@interface SecretHandshakeTest : XCTestCase


@implementation SecretHandshakeTest

- (void)testHandshake1ToWink {
    SecretHandshake *handshake = [[SecretHandshake alloc] initWithNumber:1];
    XCTAssertEqualObjects(@[@"wink"], handshake.commands);

- (void)testHandshake10ToDoubleBlink {
    SecretHandshake *handshake = [[SecretHandshake alloc] initWithNumber:2];
    XCTAssertEqualObjects(@[@"double blink"], handshake.commands);

- (void)testHandshake100ToCloseYourEyes {
    SecretHandshake *handshake = [[SecretHandshake alloc] initWithNumber:4];
    XCTAssertEqualObjects(@[@"close your eyes"], handshake.commands);

- (void)testHandshake1000ToJump {
    SecretHandshake *handshake = [[SecretHandshake alloc] initWithNumber:8];
    XCTAssertEqualObjects(@[@"jump"], handshake.commands);

- (void)testHandshake11ToWinkAndDoubleBlink {
    SecretHandshake *handshake = [[SecretHandshake alloc] initWithNumber:3];
    NSArray<NSString *> *expected = @[@"wink", @"double blink"];
    XCTAssertEqualObjects(expected, handshake.commands);

- (void)testHandshake10011ToDoubleBlinkAndWink {
    SecretHandshake *handshake = [[SecretHandshake alloc] initWithNumber:19];
    NSArray<NSString *> *expected = @[@"double blink", @"wink"];
    XCTAssertEqualObjects(expected, handshake.commands);

- (void)testHandshake11111ToDoubleBlinkAndWink {
    SecretHandshake *handshake = [[SecretHandshake alloc] initWithNumber:31];
    NSArray<NSString *> *expected = @[@"jump", @"close your eyes", @"double blink", @"wink"];
    XCTAssertEqualObjects(expected, handshake.commands);

- (void)testNonValidHandshake {
    SecretHandshake *handshake = [[SecretHandshake alloc] initWithNumber:0];
    XCTAssertEqualObjects(@[], handshake.commands);



#import <Foundation/Foundation.h>

@interface SecretHandshake : NSObject

@property (nonatomic, strong) NSArray *commands;

- (instancetype)initWithNumber:(NSInteger)number;



#import "SecretHandshake.h"

@implementation SecretHandshake

- (instancetype)initWithNumber:(NSInteger)number {
    if (self = [super init]) {
        NSMutableArray *array = [NSMutableArray array];
        NSMutableArray *result = [NSMutableArray array];
        do {
            [array addObject:[@(number % 2) stringValue]];
            number = number / 2;
        } while (number > 0);
        NSString *binaryString = [[[array reverseObjectEnumerator] allObjects] componentsJoinedByString:@""];
        NSInteger binary = [binaryString integerValue];
        BOOL reverse = NO;
        if (binary >= 10000) {
            reverse = YES;
            binary = binary % 10000;
        if (binary >= 1000) {
            binary = binary % 1000;
            [result insertObject:@"jump" atIndex:0];
        if (binary >= 100) {
            binary = binary % 100;
            [result insertObject:@"close your eyes" atIndex:0];
        if (binary >= 10) {
            binary = binary % 10;
            [result insertObject:@"double blink" atIndex:0];
        if (binary == 1) {
            [result insertObject:@"wink" atIndex:0];
        if (reverse) {
            self.commands = [NSMutableArray arrayWithArray:[[result reverseObjectEnumerator] allObjects]];
        } else {
            self.commands = result;
    return self;


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?