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

ingemar's solution

to Scrabble Score in the Objective-C Track

Published at Jul 18 2019 · 0 comments
Instructions
Test suite
Solution

Given a word, compute the scrabble score for that word.

Letter Values

You'll need these:

Letter                           Value
A, E, I, O, U, L, N, R, S, T       1
D, G                               2
B, C, M, P                         3
F, H, V, W, Y                      4
K                                  5
J, X                               8
Q, Z                               10

Examples

"cabbage" should be scored as worth 14 points:

  • 3 points for C
  • 1 point for A, twice
  • 3 points for B, twice
  • 2 points for G
  • 1 point for E

And to total:

  • 3 + 2*1 + 2*3 + 2 + 1
  • = 3 + 2 + 6 + 3
  • = 5 + 9
  • = 14

Extensions

  • You can play a double or a triple letter.
  • You can play a double or a triple word.

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/scrabble-score/ScrabbleScore.m

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

Source

Inspired by the Extreme Startup game https://github.com/rchatley/extreme_startup

Submitting Incomplete Solutions

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

ScrabbleScoreTest.m

#import <XCTest/XCTest.h>

#if __has_include("ScrabbleScoreExample.h")
# import "ScrabbleScoreExample.h"
# else
# import "ScrabbleScore.h"
#endif

@interface ScrabbleScoreTest : XCTestCase

@end

@implementation ScrabbleScoreTest

- (void)testEmptyWordScoresZero {
    XCTAssertEqual(0, [[[ScrabbleScore alloc] initWithWord:@""] score]);
}

- (void)testWhitespaceScoresZero {
    XCTAssertEqual(0, [[[ScrabbleScore alloc] initWithWord:@" \t\n"] score]);
}

- (void)testNilScoresZero {
    XCTAssertEqual(0, [[[ScrabbleScore alloc] initWithWord:nil] score]);
}

- (void)testScoresVeryShortWord {
    XCTAssertEqual(1, [[[ScrabbleScore alloc] initWithWord:@"a"] score]);
}

- (void)testScoresOtherVeryShortWord {
    XCTAssertEqual(4, [[[ScrabbleScore alloc] initWithWord:@"f"] score]);
}

- (void)testSimpleWordScoresTheNumberOfLetters {
    XCTAssertEqual(6, [[[ScrabbleScore alloc] initWithWord:@"street"] score]);
}

- (void)testComplicatedWordScoresMore {
    XCTAssertEqual(22, [[[ScrabbleScore alloc] initWithWord:@"quirky"] score]);
}

- (void)testScoresAreCaseInsensitive {
    XCTAssertEqual(41, [[[ScrabbleScore alloc] initWithWord:@"OXYPHENBUTAZONE"] score]);
}

- (void)testConvenientScoring {
    XCTAssertEqual(13, [ScrabbleScore score:@"alacrity"]);
}

@end

scrabble-score-tests/ScrabbleScore.h

//
//  ScrabbleScore.h
//  scrabble-score-tests
//
//  Created by Ingemar Pertl on 18.07.19.
//  Copyright © 2019 Ingemar Pertl. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface ScrabbleScore : NSObject

+ (int)score:(NSString * _Nullable)word;

- (instancetype)initWithWord:(NSString * _Nullable)word;

- (int)score;

@end

NS_ASSUME_NONNULL_END

scrabble-score-tests/ScrabbleScore.m

//
//  ScrabbleScore.m
//  scrabble-score-tests
//
//  Created by Ingemar Pertl on 18.07.19.
//  Copyright © 2019 Ingemar Pertl. All rights reserved.
//

#import "ScrabbleScore.h"

@interface ScrabbleScore ()

@property (nonatomic) NSString *word;
@property (nonatomic) NSDictionary *letterValues;

@end

@implementation ScrabbleScore

+ (int)score:(NSString * _Nullable)word {
    ScrabbleScore *scrabbleScore = [[ScrabbleScore alloc] initWithWord:word];
    return [scrabbleScore score];
}

- (instancetype)initWithWord:(NSString*)word {
    self = [super init];
    self.word = [word uppercaseString];
    self.letterValues = [NSDictionary dictionaryWithObjectsAndKeys:
                         @[@"A", @"E", @"I", @"O", @"U", @"L", @"N", @"R", @"S", @"T"], @1,
                         @[@"D", @"G"], @2,
                         @[@"B", @"C", @"M", @"P"], @3,
                         @[@"F", @"H", @"V", @"W", @"Y"], @4,
                         @[@"K"], @5,
                         @[@"J", @"X"], @8,
                         @[@"Q", @"Z"], @10,
                         nil];
    return self;
}

- (int)score {
    int sum = 0;
    
    if(self.word == nil)
        return sum;
    
    const char *letters = [self.word UTF8String];
    
    for (int i = 0; i < strlen(letters); i++) {
        for (NSNumber *key in self.letterValues) {
            NSArray *values = self.letterValues[key];
            
            if([values containsObject:[NSString stringWithFormat:@"%c",letters[i]]]) {
                sum+=[key integerValue];
            }
        }
    }
    
    return sum;
}

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