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

jaishreedhage's solution

to Pangram in the Objective-C Track

Published at Jul 13 2018 · 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/PangramTest/Pangram.h

//
//  Pangram.h
//  Pangram
//
//  Created by Jaishree Dhage on 11/12/17.
//  Copyright © 2017 Jaishree Dhage. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Pangram : NSObject

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

@end

Pangram/PangramTest/Pangram.m

//
//  Pangram.m
//  Pangram
//
//  Created by Jaishree Dhage on 11/12/17.
//  Copyright © 2017 Jaishree Dhage. All rights reserved.
//

#import "Pangram.h"

@implementation Pangram

+(BOOL)isPangram:(NSString *)string{
    
    string = [string lowercaseString];
    NSMutableCharacterSet *set = [NSMutableCharacterSet whitespaceCharacterSet];
    [set formUnionWithCharacterSet:[NSMutableCharacterSet punctuationCharacterSet]];
    
    string = [[string componentsSeparatedByCharactersInSet:set]componentsJoinedByString:@""];
    
    if([string rangeOfCharacterFromSet:[[NSCharacterSet alphanumericCharacterSet] invertedSet]].location!=NSNotFound)
        return false;
    
    NSMutableSet<NSString *> *alphabets = [NSMutableSet set];
    for(int i=0;i<string.length;i++){
        if([string characterAtIndex:i] >= 'a' && [string characterAtIndex:i]<='z')
            [alphabets addObject:[NSString stringWithFormat:@"%c",[string characterAtIndex:i]]];
    }
        
    if([alphabets count] < 26)
        return false;
    
    return true;
    
}

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