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

KiligWY's solution

to Bracket Push in the Objective-C Track

Published at Jul 13 2018 · 0 comments
Test suite

Given a string containing brackets [], braces {} and parentheses (), verify that all the pairs are matched and nested correctly.


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/bracket-push/BracketPush.m

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


Ginna Baker

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("BracketPushExample.h")
# import "BracketPushExample.h"
# else
# import "BracketPush.h"
@interface BracketPushTest : XCTestCase


@implementation BracketPushTest

- (void)testPairedSquareBrackets {
    XCTAssertTrue([BracketPushExample validateBracketPairingAndNestingInString:@"[]"]);

- (void)testEmptyString {
    XCTAssertTrue([BracketPushExample validateBracketPairingAndNestingInString:@""]);

- (void)testUnpairedBrackets {
    XCTAssertFalse([BracketPushExample validateBracketPairingAndNestingInString:@"[["]);

- (void)testWrongOrderedBrackets {
    XCTAssertFalse([BracketPushExample validateBracketPairingAndNestingInString:@"}{"]);

- (void)testPairedWithWhitespace {
    XCTAssertTrue([BracketPushExample validateBracketPairingAndNestingInString:@"{ }"]);

- (void)testSimpleNestedBrackets {
    XCTAssertTrue([BracketPushExample validateBracketPairingAndNestingInString:@"{[]}"]);

- (void)testSeveralPairedBrackets {
    XCTAssertTrue([BracketPushExample validateBracketPairingAndNestingInString:@"{}[]"]);

- (void)testPairedAndNestedBrackets {
    XCTAssertTrue([BracketPushExample validateBracketPairingAndNestingInString:@"([{}({}[])])"]);

- (void)testUnopenedClosingBrackets {
    XCTAssertFalse([BracketPushExample validateBracketPairingAndNestingInString:@"{[)][]}"]);

- (void)testUnpairedAndNestedBrackets {
    XCTAssertFalse([BracketPushExample validateBracketPairingAndNestingInString:@"([{])"]);

- (void)testPairedAndWrongNestedBrackets {
    XCTAssertFalse([BracketPushExample validateBracketPairingAndNestingInString:@"[({]})"]);

- (void)testMathExpression {
    XCTAssertTrue([BracketPushExample validateBracketPairingAndNestingInString:@"(((185 + 223.85) * 15) - 543)/2"]);

- (void)testComplexLatexExpression {
    XCTAssertTrue([BracketPushExample validateBracketPairingAndNestingInString:@"\\left(\\begin{array}{cc} \\frac{1}{3} & x\\\\ \\mathrm{e}^{x} &... x^2 \\end{array}\\right)"]);


#import <Foundation/Foundation.h>

@interface BracketPushExample : NSObject

+ (BOOL)validateBracketPairingAndNestingInString:(NSString *)string;



#import "BracketPushExample.h"

@implementation BracketPushExample

+ (BOOL)validateBracketPairingAndNestingInString:(NSString *)string {
    __block NSMutableArray *array = [NSMutableArray array];
    __block BOOL result = YES;
    [string enumerateSubstringsInRange:NSMakeRange(0, string.length) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString * _Nullable substring, NSRange substringRange, NSRange enclosingRange, BOOL * _Nonnull stop) {
        if ([substring isEqualToString:@"["]
            || [substring isEqualToString:@"{"]
            || [substring isEqualToString:@"("]) {
            [array addObject:substring];
        if ([substring isEqualToString:@"]"]
            || [substring isEqualToString:@"}"]
            || [substring isEqualToString:@")"]) {
            if (([substring isEqualToString:@"]"] && [[array lastObject] isEqualToString:@"["])
                || ([substring isEqualToString:@"}"] && [[array lastObject] isEqualToString:@"{"])
                || ([substring isEqualToString:@")"] && [[array lastObject] isEqualToString:@"("])) {
                [array removeLastObject];
            } else {
                result = NO;
                *stop = YES;
    return result && (array.count == 0);


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?