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

lordvidex's solution

to Bob in the Objective-C Track

Published at Dec 23 2020 · 0 comments
Instructions
Test suite
Solution

Bob is a lackadaisical teenager. In conversation, his responses are very limited.

Bob answers 'Sure.' if you ask him a question.

He answers 'Whoa, chill out!' if you yell at him.

He says 'Fine. Be that way!' if you address him without actually saying anything.

He answers 'Whatever.' to anything else.

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/bob/Bob.m

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

Source

Inspired by the 'Deaf Grandma' exercise in Chris Pine's Learn to Program tutorial. http://pine.fm/LearnToProgram/?Chapter=06

Submitting Incomplete Solutions

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

BobTest.m

#import <XCTest/XCTest.h>

#if __has_include("BobExample.h")
# import "BobExample.h"
#else
# import "Bob.h"
#endif

NS_ASSUME_NONNULL_BEGIN

@interface BobTest : XCTestCase

@end

@implementation BobTest

- (Bob *)bob {
    return [[Bob alloc] init];
}

- (void)testStatingSomething {
    NSString *input = @"Tom-ay-to, tom-aaaah-to.";
    NSString *expected = @"Whatever.";
    NSString *result = [[self bob] hey:input];
    XCTAssertEqualObjects(expected, result);
}

- (void)testShouting {
    NSString *input = @"WATCH OUT!";
    NSString *expected = @"Whoa, chill out!";
    NSString *result = [[self bob] hey:input];
    XCTAssertEqualObjects(expected, result);
}

- (void)testAskingAQuestion {
    NSString *input = @"Does this cryogenic chamber make me look fat?";
    NSString *expected = @"Sure.";
    NSString *result = [[self bob] hey:input];
    XCTAssertEqualObjects(expected, result);
}

- (void)testTalkingForcefully {
    NSString *input = @"Let's go make out behind the gym!";
    NSString *expected = @"Whatever.";
    NSString *result = [[self bob] hey:input];
    XCTAssertEqualObjects(expected, result);
}

- (void)testUsingAcronyms {
    NSString *input = @"It's OK if you don't want to go to the DMV.";
    NSString *expected = @"Whatever.";
    NSString *result = [[self bob] hey:input];
    XCTAssertEqualObjects(expected, result);
}

- (void)testForcefulQuestions {
    NSString *input = @"WHAT THE HELL WERE YOU THINKING?";
    NSString *expected = @"Whoa, chill out!";
    NSString *result = [[self bob] hey:input];
    XCTAssertEqualObjects(expected, result);
}

- (void)testShoutingNumbers {
    NSString *input = @"1, 2, 3 GO!";
    NSString *expected = @"Whoa, chill out!";
    NSString *result = [[self bob] hey:input];
    XCTAssertEqualObjects(expected, result);
}

- (void)testOnlyNumbers {
    NSString *input = @"1, 2, 3.";
    NSString *expected = @"Whatever.";
    NSString *result = [[self bob] hey:input];
    XCTAssertEqualObjects(expected, result);
}
- (void)testQuestionWithOnlyNumbers {
    NSString *input = @"4?";
    NSString *expected = @"Sure.";
    NSString *result = [[self bob] hey:input];
    XCTAssertEqualObjects(expected, result);
}

- (void)testShoutingWithSpecialCharacters {
    NSString *input = @"ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!";
    NSString *expected = @"Whoa, chill out!";
    NSString *result = [[self bob] hey:input];
    XCTAssertEqualObjects(expected, result);
}

- (void)testShoutingWithUmlautsCharacters {
    NSString *input = @"ÄMLÄTS!";
    NSString *expected = @"Whoa, chill out!";
    NSString *result = [[self bob] hey:input];
    XCTAssertEqualObjects(expected, result);
}

- (void)testCalmlySpeakingAboutUmlauts {
    NSString *input = @"ÄMLäTS!";
    NSString *expected = @"Whatever.";
    NSString *result = [[self bob] hey:input];
    XCTAssertEqualObjects(expected, result);
}

- (void)testShoutingWithNoExclamationMark {
    NSString *input = @"I HATE YOU";
    NSString *expected = @"Whoa, chill out!";
    NSString *result = [[self bob] hey:input];
    XCTAssertEqualObjects(expected, result);
}

- (void)testStatementContainingQuestionsMark {
    NSString *input = @"Ending with a ? means a question.";
    NSString *expected = @"Whatever.";
    NSString *result = [[self bob] hey:input];
    XCTAssertEqualObjects(expected, result);
}

- (void)testPrattlingOn {
    NSString *input = @"Wait! Hang on.  Are you going to be OK?";
    NSString *expected = @"Sure.";
    NSString *result = [[self bob] hey:input];
    XCTAssertEqualObjects(expected, result);
}

- (void)testSilence {
    NSString *input = @"";
    NSString *expected = @"Fine. Be that way!";
    NSString *result = [[self bob] hey:input];
    XCTAssertEqualObjects(expected, result);
}

- (void)testProlongedSilence {
    NSString *input = @"     ";
    NSString *expected = @"Fine. Be that way!";
    NSString *result = [[self bob] hey:input];
    XCTAssertEqualObjects(expected, result);
}

@end
NS_ASSUME_NONNULL_END
//
//  Bob.m
//  BobTest
//
//  Created by Evans Owamoyo on 24.12.2020.
//

#import "Bob.h"

@implementation Bob
- (NSString *)hey:(NSString *)input{
    BOOL tracker = YES;
    BOOL hasLetters  = NO;
    for(NSInteger it = 0;it<[input length];it+=1){
        NSString * temp = [NSString stringWithFormat:@"%C",[input characterAtIndex:it]];
        if([[NSCharacterSet letterCharacterSet] characterIsMember:[input characterAtIndex:it]])hasLetters = YES;
        tracker = tracker &&[[temp uppercaseString] isEqualToString:temp];
    }
    if(tracker && hasLetters){
        return @"Whoa, chill out!";
    }
    if([[input stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] isEqualToString:@""]){
        return @"Fine. Be that way!";
    }else if([input characterAtIndex:[input length]-1]==(int)'?'){
        return @"Sure.";
    }
    return @"Whatever.";
}
@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?