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

junmo-kim's solution

to Word Count in the Objective-C Track

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

Given a phrase, count the occurrences of each word in that phrase.

For example for the input "olly olly in come free"

olly: 2
in: 1
come: 1
free: 1

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/word-count/WordCount.m

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

Source

This is a classic toy problem, but we were reminded of it by seeing it in the Go Tour.

Submitting Incomplete Solutions

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

WordCountTest.m

#import <XCTest/XCTest.h>

#if __has_include("WordCountExample.h")
# import "WordCountExample.h"
# else
# import "WordCount.h"
#endif

NS_ASSUME_NONNULL_BEGIN

@interface WordCountTest : XCTestCase

@end

@implementation WordCountTest

- (void)testCountOneWord {
  WordCount *words = [[WordCount alloc] initWithString:@"word"];
  NSDictionary<NSString *, NSNumber *> *expected = @{ @"word" : @1 };
  NSDictionary<NSString *, NSNumber *> *result = [words count];

  XCTAssertEqualObjects(expected, result);
}

- (void)testCountOneOfEeach {
  WordCount *words = [[WordCount alloc] initWithString:@"one of each"];
  NSDictionary<NSString *, NSNumber *> *expected = @{ @"one" : @1, @"of" : @1, @"each" : @1 };
  NSDictionary<NSString *, NSNumber *> *result = [words count];

  XCTAssertEqualObjects(expected, result);
}

- (void)testCountMultipleOccurrences {
  WordCount *words = [[WordCount alloc] initWithString:@"one fish two fish red fish blue fish"];
  NSDictionary<NSString *, NSNumber *> *expected = @{ @"one" : @1, @"fish" : @4, @"two" : @1, @"red" : @1, @"blue" : @1 };
  NSDictionary<NSString *, NSNumber *> *result = [words count];

  XCTAssertEqualObjects(expected, result);
}

- (void)testIgnorePunctation {
  WordCount *words = [[WordCount alloc] initWithString:@"car : carpet as java : javascript!!&@$%^&"];
  NSDictionary<NSString *, NSNumber *> *expected = @{ @"car" : @1, @"carpet" : @1, @"as" : @1, @"java" : @1, @"javascript" : @1 };
  NSDictionary<NSString *, NSNumber *> *result = [words count];

  XCTAssertEqualObjects(expected, result);
}

- (void)testIncludeNumbers {
  WordCount *words = [[WordCount alloc] initWithString:@"testing, 1, 2 testing"];
  NSDictionary<NSString *, NSNumber *> *expected = @{ @"testing" : @2, @"1" : @1, @"2" : @1 };
  NSDictionary<NSString *, NSNumber *> *result = [words count];

  XCTAssertEqualObjects(expected, result);
}

- (void)testNormalizeCase {
  WordCount *words = [[WordCount alloc] initWithString:@"go Go GO"];
  NSDictionary<NSString *, NSNumber *> *expected = @{ @"go" : @3 };
  NSDictionary<NSString *, NSNumber *> *result = [words count];

  XCTAssertEqualObjects(expected, result);
}

@end
NS_ASSUME_NONNULL_END
#import "WordCount.h"

@interface WordCount ()

@property (nonatomic, strong) NSDictionary *count;

@end

@implementation WordCount

- (id)initWithString:(NSString *)string {
    self = [self init];
    if (self) {
        self.string = string;
    }
    return self;
}

- (void)setString:(NSString *)string {
    if ([_string isEqualToString:string]) return;
    
    _string = string;
    self.count = nil;
}

- (NSDictionary *)count {
    if (!_count) {
        NSCharacterSet *notAlphaNumerticCharSet = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
        NSArray *words = [self.string componentsSeparatedByCharactersInSet:notAlphaNumerticCharSet];
        NSMutableDictionary *count = [NSMutableDictionary dictionary];
        for (NSString *word in words) {
            NSString *lowerCasedWord = [word lowercaseString];
            NSNumber *existingCount = [count objectForKey:lowerCasedWord];
            [count setObject:@([existingCount integerValue] + 1) forKey:lowerCasedWord];
        }
        _count = [count copy];
    }
    
    return _count;
}

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