Exercism v3 launches on Sept 1st 2021. Learn more! 🚀🚀🚀
Avatar of redspike

redspike's solution

to Anagram in the Objective-C Track

Published at Jul 13 2018 · 2 comments
Test suite

Given a word and a list of possible anagrams, select the correct sublist.

Given "listen" and a list of candidates like "enlists" "google" "inlets" "banana" the program should return a list containing "inlets".


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/anagram/Anagram.m

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


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.


#import <XCTest/XCTest.h>

#if __has_include("AnagramExample.h")
# import "AnagramExample.h"
# else
# import "Anagram.h"


@interface AnagramTest : XCTestCase


@implementation AnagramTest

- (void)testNoMatches {
    Anagram *anagram = [[Anagram alloc] initWithString:@"diaper"];
    NSArray<NSString *> *results = [anagram match:@[@"hello",@"world",@"zombies",@"pants"]];
    NSArray<NSString *> *expected = @[];
    XCTAssertEqualObjects(results, expected);

- (void)testDetectSimpleAnagram {
    Anagram *anagram = [[Anagram alloc] initWithString:@"ant"];
    NSArray<NSString *> *results = [anagram match:@[@"tan",@"stand",@"at"]];
    NSArray<NSString *> *expected = @[@"tan"];
    XCTAssertEqualObjects(results, expected);

- (void)testDetectMultipleAnagrams {
    Anagram *anagram = [[Anagram alloc] initWithString:@"master"];
    NSArray<NSString *> *results = [anagram match:@[@"stream",@"pigeon",@"maters"]];
    NSArray<NSString *> *expected = @[@"stream",@"maters"];
    XCTAssertEqualObjects(results, expected);

- (void)testDoesNotConfuseDifferentDuplicates {
    Anagram *anagram = [[Anagram alloc] initWithString:@"galea"];
    NSArray<NSString *> *results = [anagram match:@[@"eagle"]];
    NSArray<NSString *> *expected = @[];
    XCTAssertEqualObjects(results, expected);

- (void)testIdenticalWordIsNotAnagram {
    Anagram *anagram = [[Anagram alloc] initWithString:@"corn"];
    NSArray<NSString *> *results = [anagram match:@[@"corn", @"dark", @"Corn", @"rank", @"CORN", @"cron", @"park"]];
    NSArray<NSString *> *expected = @[@"cron"];
    XCTAssertEqualObjects(results, expected);

- (void)testEliminateAnagramsWithSameChecksum {
    Anagram *anagram = [[Anagram alloc] initWithString:@"mass"];
    NSArray<NSString *> *results = [anagram match:@[@"last"]];
    NSArray<NSString *> *expected = @[];
    XCTAssertEqualObjects(results, expected);

- (void)testEliminateAnagramSubsets {
    Anagram *anagram = [[Anagram alloc] initWithString:@"good"];
    NSArray<NSString *> *results = [anagram match:@[@"dog",@"goody"]];
    NSArray<NSString *> *expected = @[];
    XCTAssertEqualObjects(results, expected);

- (void)testDetectAnagram {
    Anagram *anagram = [[Anagram alloc] initWithString:@"listen"];
    NSArray<NSString *> *results = [anagram match:@[@"enlists",@"google",@"inlets",@"banana"]];
    NSArray<NSString *> *expected = @[@"inlets"];
    XCTAssertEqualObjects(results, expected);

- (void)testMultipleAnagrams {
    Anagram *anagram = [[Anagram alloc] initWithString:@"allergy"];
    NSArray<NSString *> *results = [anagram match:@[@"gallery",@"ballerina",@"regally",@"clergy",@"largely",@"leading"]];
    NSArray<NSString *> *expected = @[@"gallery",@"regally",@"largely"];
    XCTAssertEqualObjects(results, expected);

- (void)testAnagramsAreCaseInsensitive {
    Anagram *anagram = [[Anagram alloc] initWithString:@"Orchestra"];
    NSArray<NSString *> *results = [anagram match:@[@"cashregister",@"Carthorse",@"radishes"]];
    NSArray<NSString *> *expected = @[@"Carthorse"];
    XCTAssertEqualObjects(results, expected);

//  Anagram.m
//  anagram
//  Created by Daniel Burnett on 14/11/2013.
//  Copyright (c) 2013 iNtuitiveApps. All rights reserved.

#import "Anagram.h"

@implementation Anagram
@synthesize input;
-(id)initWithString:(NSString *)inputString
    self.input = [inputString lowercaseString];
    return self;

-(NSArray *)match:(NSArray *)matchArray
    NSArray *inputArr = [self charArray:input];
    NSMutableArray *array = [NSMutableArray array];
    for (NSString *string in matchArray) {
        NSString *lString = [string lowercaseString];
        NSArray *arr1 = [self charArray:lString];
        if ([arr1 isEqualToArray:inputArr] && ![lString isEqualToString:input]) {
            [array addObject:string];
    return array;

-(NSArray *)charArray:(NSString *)inputString
    NSMutableArray *array = [NSMutableArray array];
    for (int i=0; i<inputString.length; i++)
        [array addObject:[NSString stringWithFormat:@"%c",[inputString characterAtIndex:i]]];
    return [self sortCharArray:array];

-(NSArray *)sortCharArray:(NSArray *)inputArray
    NSArray *sorted = [inputArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        if(obj1 > obj2)
            return NSOrderedAscending;
        else if (obj1 < obj2)
            return NSOrderedDescending;
            return NSOrderedSame;
    return sorted;


Community comments

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

A very simple algorithm, simply implemented. I did find myself getting lost in array, lstring, string. I think more meaningful names would make the code more expressive.

Avatar of burtlo

I like that you have charArray: and sortCharArray:. Having the two methods separated like that make the code a little more reusable in the future.

It feels like charArray: should likely be called sortedCharArray: as it also sorts the letters that it is returning. I would make a new method that simply does the conversion from an NSString to NSArray and create the new method sortedCharArray: which converts the NSString to a sorted NSArray of letters.

Lastly, the work that is being done between 24 and 28 seem as though it could be a little more clear. Definitely giving the variables better names, as you mentioned, would be a great start. The other thought is that maybe you could hide more of the implementation details that are going on in the middle of the loop within another method. So you could call something like this within the for loop: if ([self isWord:self.input anagramOf:string]) { [matches addObject:obj]; }

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?