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

ahalls's solution

to Word Count in the Objective-C Track

Published at Jul 13 2018 · 1 comment
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
// WordCount.m
#import "WordCount.h"



@interface WordCountBucket: NSObject

-(void) incrementCountForWord: (NSString *) word;

-(NSDictionary *) dictionary;

@end

@interface WordCountBucket ()

@property (nonatomic, strong) NSMutableDictionary * wordBucket;

@end


@implementation WordCountBucket

-(instancetype) init {
    self = [super init];
    if (self) {
        self.wordBucket = [[NSMutableDictionary alloc] init];
    }
    return self;
}

-(void) incrementCountForWord: (NSString *) word {
    NSNumber * wordCount = self.wordBucket[word];
    self.wordBucket[word]  = (wordCount == nil) ? @1 : @([wordCount intValue] + 1);
}

-(NSDictionary *) dictionary {
    return [NSDictionary dictionaryWithDictionary:self.wordBucket];
}


@end

@interface WordCount ()

@property (nonatomic, strong) NSDictionary * count;


@end

@implementation WordCount

-(instancetype) initWithString: (NSString *) string {
    
    self = [super init];
    if (self) {
        self.count = [self countWordsInString: string];
        
    }
    return self;
}

-(NSDictionary *) countWordsInString: (NSString *) string {
    
    WordCountBucket * wordCountBucket = [[WordCountBucket alloc] init];
    NSScanner * scanner = [NSScanner scannerWithString:string];

    while ([scanner isAtEnd] == NO) {
        NSString * word = nil;
        if([scanner scanCharactersFromSet:[NSCharacterSet alphanumericCharacterSet] intoString:&word]) {
            [wordCountBucket incrementCountForWord:[word lowercaseString]];
        }
        else {
            [scanner scanUpToCharactersFromSet:[NSCharacterSet alphanumericCharacterSet] intoString:nil];
        }
    }
    
    return wordCountBucket.dictionary;
}



@end

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of ahalls
ahalls
Solution Author
commented over 7 years ago

First time I played with NSScanner didn't cleanup any code. Can also do something similar with Regex's won't be less lines of code.

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?