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

ingemar's solution

to Pangram in the Objective-C Track

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

Determine if a sentence is a pangram. A pangram (Greek: παν γράμμα, pan gramma, "every letter") is a sentence using every letter of the alphabet at least once. The best known English pangram is:

The quick brown fox jumps over the lazy dog.

The alphabet used consists of ASCII letters a to z, inclusive, and is case insensitive. Input will not contain non-ASCII symbols.

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/pangram/Pangram.m

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

Source

Wikipedia https://en.wikipedia.org/wiki/Pangram

Submitting Incomplete Solutions

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

PangramTest.m

#import <XCTest/XCTest.h>

#if __has_include("PangramExample.h")
# import "PangramExample.h"
# else
# import "Pangram.h"
#endif

@interface PangramTest : XCTestCase

@end

@implementation PangramTest

- (void)testSentenceEmpty {
    XCTAssertFalse([Pangram isPangram:@""]);
}

- (void)test_pangram_with_only_lower_case {
    XCTAssertTrue([Pangram isPangram:@"the quick brown fox jumps over the lazy dog"]);
}

- (void)testMissingCharacterX {
    XCTAssertFalse([Pangram isPangram:@"a quick movement of the enemy will jeopardize five gunboats"]);
}

- (void)testAnotherMissingCharacterX {
    XCTAssertFalse([Pangram isPangram:@"the quick brown fish jumps over the lazy dog"]);
}

- (void)testPangramWithUnderscores {
    XCTAssertTrue([Pangram isPangram:@"the_quick_brown_fox_jumps_over_the_lazy_dog"]);
}

- (void)testPangramWithNumbers {
    XCTAssertTrue([Pangram isPangram:@"the 1 quick brown fox jumps over the 2 lazy dogs"]);
}

- (void)testMissingLettersReplacedByNumbers {
    XCTAssertFalse([Pangram isPangram:@"7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog"]);
}

- (void)testPangramWithMixedCaseAndPunctuation {
    XCTAssertTrue([Pangram isPangram:@"Five quacking Zephyrs jolt my wax bed."]);
}

- (void)testPangramWithNonAsciiCharacters {
    XCTAssertTrue([Pangram isPangram:@"Victor jagt zwölf Boxkämpfer quer über den großen Sylter Deich."]);
}

@end

pangram-tests/Pangram.h

//
//  Pangram.h
//  pangram-tests
//
//  Created by ingemar pertl on 19.07.19.
//  Copyright © 2019 ingemar pertl. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface Pangram : NSObject

+ (BOOL)isPangram:(NSString*)input;

@end

NS_ASSUME_NONNULL_END

pangram-tests/Pangram.m

//
//  Pangram.m
//  pangram-tests
//
//  Created by ingemar pertl on 19.07.19.
//  Copyright © 2019 ingemar pertl. All rights reserved.
//

#import "Pangram.h"

@implementation Pangram

+ (BOOL)isPangram:(NSString*)input {
    NSMutableDictionary *letters = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                @false,@"a",
                                @false,@"b",
                                @false,@"c",
                                @false,@"d",
                                @false,@"e",
                                @false,@"f",
                                @false,@"g",
                                @false,@"h",
                                @false,@"i",
                                @false,@"j",
                                @false,@"k",
                                @false,@"l",
                                @false,@"m",
                                @false,@"o",
                                @false,@"p",
                                @false,@"q",
                                @false,@"r",
                                @false,@"s",
                                @false,@"t",
                                @false,@"u",
                                @false,@"v",
                                @false,@"w",
                                @false,@"x",
                                @false,@"y",
                                @false,@"z",
                                nil];
    
    const char *inputCharacters = [[input lowercaseString] UTF8String];
    for (int i = 0; i < strlen(inputCharacters); i++) {
        [letters setObject:@true forKey:[NSString stringWithFormat:@"%c", inputCharacters[i]]];
    }
    
    return ![[letters allValues] doesContain:@false];
}

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