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

ingemar's solution

to Allergies in the Objective-C Track

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

Given a person's allergy score, determine whether or not they're allergic to a given item, and their full list of allergies.

An allergy test produces a single numeric score which contains the information about all the allergies the person has (that they were tested for).

The list of items (and their value) that were tested are:

  • eggs (1)
  • peanuts (2)
  • shellfish (4)
  • strawberries (8)
  • tomatoes (16)
  • chocolate (32)
  • pollen (64)
  • cats (128)

So if Tom is allergic to peanuts and chocolate, he gets a score of 34.

Now, given just that score of 34, your program should be able to say:

  • Whether Tom is allergic to any one of those allergens listed above.
  • All the allergens Tom is allergic to.

Note: a given score may include allergens not listed above (i.e. allergens that score 256, 512, 1024, etc.). Your program should ignore those components of the score. For example, if the allergy score is 257, your program should only report the eggs (1) allergy.

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/allergies/Allergies.m

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

Source

Jumpstart Lab Warm-up http://jumpstartlab.com

Submitting Incomplete Solutions

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

AllergiesTest.m

#import <XCTest/XCTest.h>

#if __has_include("AllergiesExample.h")
# import "AllergiesExample.h"
# else
# import "Allergies.h"
#endif

@interface AllergiesTest : XCTestCase

@end

@implementation AllergiesTest

- (NSArray<NSNumber *> *)allAllergens {
    return @[@(AllergenEggs), @(AllergenPeanuts), @(AllergenShellfish), @(AllergenStrawberries), @(AllergenTomatoes), @(AllergenChocolate), @(AllergenPollen), @(AllergenCats)];
}

- (void)testAllergicToEggs { //!OCLint
    Allergies *allergies = [[Allergies alloc] initWithScore:1];
    XCTAssertTrue([allergies hasAllergy:AllergenEggs]);
    XCTAssertFalse([allergies hasAllergy:AllergenPeanuts]);
    XCTAssertFalse([allergies hasAllergy:AllergenPollen]);
}

- (void)testAllergicToChocolate { //!OCLint
    Allergies *allergies = [[Allergies alloc] initWithScore:32];
    XCTAssertTrue([allergies hasAllergy:AllergenChocolate]);
    XCTAssertFalse([allergies hasAllergy:AllergenEggs]);
    XCTAssertFalse([allergies hasAllergy:AllergenTomatoes]);
}

- (void)testBob { //!OCLint
    Allergies *allergies = [[Allergies alloc] initWithScore:34];
    XCTAssertTrue([allergies hasAllergy:AllergenPeanuts], "Bob is allergic to peanuts");
    XCTAssertTrue([allergies hasAllergy:AllergenChocolate], "Bob is allergic to chocolate");
    XCTAssertFalse([allergies hasAllergy:AllergenCats], "Bob is not allergic to cats");
}

- (void)testEggsAndCats { //!OCLint
    Allergies *allergies = [[Allergies alloc] initWithScore:129];
    XCTAssertTrue([allergies hasAllergy:AllergenEggs]);
    XCTAssertTrue([allergies hasAllergy:AllergenCats]);
    XCTAssertFalse([allergies hasAllergy:AllergenChocolate]);
}

- (void)testNone {
    Allergies *allergies = [[Allergies alloc] initWithScore:0];
    for (NSNumber *allergen in [self allAllergens]) {
        XCTAssertFalse([allergies hasAllergy:[allergen intValue]]);
    }
}

- (void)testAll {
    Allergies *allergies = [[Allergies alloc] initWithScore:255];
    for (NSNumber *allergen in [self allAllergens]) {
        XCTAssertTrue([allergies hasAllergy:[allergen intValue]]);
    }
}

@end

allergies-test/Allergies.h

//
//  Allergies.h
//  allergies-test
//
//  Created by ingemar pertl on 19.07.19.
//  Copyright © 2019 ingemar pertl. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

typedef NS_OPTIONS(int, Allergens) {
    AllergenEggs         = 1,
    AllergenPeanuts      = 2,
    AllergenShellfish    = 4,
    AllergenStrawberries = 8,
    AllergenTomatoes     = 16,
    AllergenChocolate    = 32,
    AllergenPollen       = 64,
    AllergenCats         = 128
};

@interface Allergies : NSObject

- (instancetype)initWithScore:(int)score;

- (BOOL)hasAllergy:(Allergens)allergy;

@end

NS_ASSUME_NONNULL_END

allergies-test/Allergies.m

//
//  Allergies.m
//  allergies-test
//
//  Created by ingemar pertl on 19.07.19.
//  Copyright © 2019 ingemar pertl. All rights reserved.
//

#import "Allergies.h"

@interface Allergies ()

@property (nonatomic) int score;

@end

@implementation Allergies

- (instancetype)initWithScore:(int)score {
    self = [super init];
    self.score = score;

    return self;
}

- (BOOL)hasAllergy:(Allergens)allergy {
    return (self.score & allergy) != 0;
}

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