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

rsslldnphy's solution

to Word Count in the Objective-C Track

Published at Jul 13 2018 · 2 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"

#define SEPARATORS [[ NSCharacterSet alphanumericCharacterSet ] invertedSet ]

@implementation WordCount : NSObject

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

- (NSString*) downcased
{
    return [ self.source lowercaseString];
}

- (NSArray*) words
{
    return [[self downcased] componentsSeparatedByCharactersInSet: SEPARATORS ];
}

- (NSDictionary*) count
{
    NSMutableDictionary *counts = [[NSMutableDictionary alloc] init];
    NSArray *words = [self words];
    for (id word in words) {
        counts[word] = @([counts[word] intValue] + 1);
    }
    return counts;
}
@end

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of burtlo

It is completely valid to use a #define to set up what one would consider a constant. I am more inclined to place that behind a method.

  • (NSCharacterSet)validSeparators { return [[ NSCharacterSet alphanumericCharacterSet ] invertedSet ]; }

The reason being is that the set of valid separators may want to change in a subclass (not likely) or may grow or change over time (more likely).

Now this is me bringing in what I feel is a OOP practice that I use in Ruby all the time. Objective-C tends to be different and defines do often rule the roost.

Avatar of rsslldnphy

I would tend to agree with you. I have to say my choice of define was entirely down to a desperate (and most probably futile) attempt to get the code to obey the 80 character rule while simultaneously not looking completely horrific. I did try having the character set returned by a method in a previous iteration but couldn't get it to quite fit.

So not the best motivation, perhaps, but there you go :-)

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?