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

ingemar's solution

to Isogram in the Objective-C Track

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

Determine if a word or phrase is an isogram.

An isogram (also known as a "nonpattern word") is a word or phrase without a repeating letter, however spaces and hyphens are allowed to appear multiple times.

Examples of isograms:

  • lumberjacks
  • background
  • downstream
  • six-year-old

The word isograms, however, is not an isogram, because the s repeats.

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/isogram/Isogram.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/Isogram

Submitting Incomplete Solutions

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

IsogramTest.m

#import <XCTest/XCTest.h>

#if __has_include("IsogramExample.h")
# import "IsogramExample.h"
# else
# import "Isogram.h"
#endif

@interface IsogramTest : XCTestCase

@end

@implementation IsogramTest

- (void)testEmptyString {
    XCTAssertTrue([Isogram isIsogram:@""]);
}

- (void)testIsogramWithOnlyLowerCaseCharacters {
    XCTAssertTrue([Isogram isIsogram:@"isogram"]);
}

- (void)testWordWithOneDuplicatedCharacter {
    XCTAssertFalse([Isogram isIsogram:@"eleven"]);
}

- (void)testLongestReportedEnglishIsogram {
    XCTAssertTrue([Isogram isIsogram:@"subdermatoglyphic"]);
}

- (void)testWordWithDuplicatedCharacterInMixedCase {
    XCTAssertFalse([Isogram isIsogram:@"Alphabet"]);
}

- (void)testHypotheticalIsogrammicWordWithHyphen {
    XCTAssertTrue([Isogram isIsogram:@"thumbscrew-japingly"]);
}

- (void)testIsogramWithDuplicatedNonLetterCharacter {
    XCTAssertTrue([Isogram isIsogram:@"Hjelmqvist-Gryb-Zock-Pfund-Wax"]);
}

- (void)testMadeUpNameThatIsAnIsogram {
    XCTAssertTrue([Isogram isIsogram:@"Emily Jung Schwartzkopf"]);
}

@end

isogram-tests/Isogram.h

//
//  Isogram.h
//  isogram-tests
//
//  Created by Ingemar Pertl on 16.07.19.
//  Copyright © 2019 Ingemar Pertl. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface Isogram : NSObject

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

@end

NS_ASSUME_NONNULL_END

isogram-tests/Isogram.m

//
//  Isogram.m
//  isogram-tests
//
//  Created by Ingemar Pertl on 16.07.19.
//  Copyright © 2019 Ingemar Pertl. All rights reserved.
//

#import "Isogram.h"

@implementation Isogram

+(BOOL)isIsogram:(NSString *)input {
    NSArray *charactersToIgnore = [NSArray arrayWithObjects:@"-", @" ", nil];
    NSUInteger inputLength = [input length];
    unichar characters[inputLength + 1];
    [input getCharacters:characters range:NSMakeRange(0, inputLength)];
    
    for (int i = 0; i < inputLength; i++) {
        NSString *character = [NSString stringWithFormat:@"%c", characters[i]];
        if([charactersToIgnore containsObject:character])
            break;
        
        NSRange range = NSMakeRange(i + 1, inputLength - (i+1));
        NSString *remaining = [input substringWithRange:range];
        if([remaining containsString:[character lowercaseString]])
            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?