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

jaishreedhage's solution

to Bracket Push in the Objective-C Track

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

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

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

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

Source

Ginna Baker

Submitting Incomplete Solutions

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

BracketPushTest.m

#import <XCTest/XCTest.h>
#if __has_include("BracketPushExample.h")
# import "BracketPushExample.h"
# else
# import "BracketPush.h"
#endif
@interface BracketPushTest : XCTestCase

@end

@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)"]);
}
@end

BracketPushExample/BracketPushExampleTest/BracketPushExample.h

//
//  BracketPushExample.h
//  BracketPushExample
//
//  Created by Jaishree Dhage on 12/12/17.
//  Copyright © 2017 Jaishree Dhage. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface BracketPushExample : NSObject

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

@end

BracketPushExample/BracketPushExampleTest/BracketPushExample.m

//
//  BracketPushExample.m
//  BracketPushExample
//
//  Created by Jaishree Dhage on 12/12/17.
//  Copyright © 2017 Jaishree Dhage. All rights reserved.
//

#import "BracketPushExample.h"

@implementation BracketPushExample

+(BOOL)validateBracketPairingAndNestingInString:(NSString *)string{
    
    NSMutableArray *stack = [NSMutableArray array];
    for(int i=0;i<string.length;i++){
        unichar c = [string characterAtIndex:i];
        if(c=='[' || c=='{' || c=='(')
            [stack addObject:[NSString stringWithFormat:@"%c",c]];
        else if(c!='[' && c!='{' && c!='(' && c!=']' && c!='}' && c!=')')
            continue;
        else if( [stack count]>0 && ( ( ( [[NSString stringWithFormat:@"%c",c] isEqualToString: [NSString stringWithFormat:@"%c",']']]) && [[stack lastObject] isEqualToString:[NSString stringWithFormat:@"%c",'[']]) ||
                (( [[NSString stringWithFormat:@"%c",c] isEqualToString: [NSString stringWithFormat:@"%c",')']]) && [[stack lastObject] isEqualToString:[NSString stringWithFormat:@"%c",'(']]) ||
                (( [[NSString stringWithFormat:@"%c",c] isEqualToString: [NSString stringWithFormat:@"%c",'}']]) && [[stack lastObject] isEqualToString:[NSString stringWithFormat:@"%c",'{']]) )){
            
            [stack removeLastObject];
        }
        
        else
            return false;
    }
    if([stack count]==0)
        return true;
    return false;
    
}

@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?