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

ingemar's solution

to Word Count in the Objective-C Track

Published at Jul 17 2019 · 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

word-count-tests/WordCount.h

//
//  WordCount.h
//  word-count-tests
//
//  Created by Ingemar Pertl on 17.07.19.
//  Copyright © 2019 Ingemar Pertl. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface WordCount : NSObject

-(instancetype)initWithString:(NSString*)input;

-(NSDictionary<NSString *, NSNumber *>*)count;

@end

NS_ASSUME_NONNULL_END

word-count-tests/WordCount.m

//
//  WordCount.m
//  word-count-tests
//
//  Created by Ingemar Pertl on 17.07.19.
//  Copyright © 2019 Ingemar Pertl. All rights reserved.
//

#import "WordCount.h"

@interface WordCount ()
-(NSString*)removeSpecialCharacters:(NSString*)input;
@end

@implementation WordCount {
    NSString *phrase;
}

-(instancetype)initWithString:(NSString*)input {
    self = [super init];
    phrase = [[self removeSpecialCharacters:input] lowercaseString];
    
    return self;
}

-(NSDictionary<NSString *, NSNumber *>*)count {
    NSMutableDictionary<NSString *, NSNumber *> *wordCount = [NSMutableDictionary new];
    NSArray<NSString *> *words = [phrase componentsSeparatedByString:@" "];
    
    for (NSString *word in words) {
        if([word length] == 0)
            continue;
        
        NSNumber *count = @1;
        if(wordCount[word] != nil) {
            count = [wordCount valueForKey:word];
            int value = [count intValue];
            count = [NSNumber numberWithInt:value + 1];
        }
        [wordCount setObject:count forKey:word];
    }
    
    return wordCount;
}

-(NSString*)removeSpecialCharacters:(NSString *)input {
    NSCharacterSet *notAllowedChars = [[NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789 "] invertedSet];
    NSString *resultString = [[input componentsSeparatedByCharactersInSet:notAllowedChars] componentsJoinedByString:@""];
    return resultString;
}

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