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

ingemar's solution

to Grade School in the Objective-C Track

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

Given students' names along with the grade that they are in, create a roster for the school.

In the end, you should be able to:

  • Add a student's name to the roster for a grade
    • "Add Jim to grade 2."
    • "OK."
  • Get a list of all students enrolled in a grade
    • "Which students are in grade 2?"
    • "We've only got Jim just now."
  • Get a sorted list of all students in all grades. Grades should sort as 1, 2, 3, etc., and students within a grade should be sorted alphabetically by name.
    • "Who all is enrolled in school right now?"
    • "Grade 1: Anna, Barb, and Charlie. Grade 2: Alex, Peter, and Zoe. Grade 3…"

Note that all our students only have one name. (It's a small town, what do you want?)

For bonus points

Did you get the tests passing and the code clean? If you want to, these are some additional things you could try:

  • If you're working in a language with mutable data structures and your implementation allows outside code to mutate the school's internal DB directly, see if you can prevent this. Feel free to introduce additional tests.

Then please share your thoughts in a comment on the submission. Did this experiment make the code better? Worse? Did you learn anything from it?

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/grade-school/GradeSchool.m

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

Source

A pairing session with Phil Battos at gSchool http://gschool.it

Submitting Incomplete Solutions

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

GradeSchoolTest.m

#import <XCTest/XCTest.h>

#if __has_include("GradeSchoolExample.h")
# import "GradeSchoolExample.h"
# else
# import "GradeSchool.h"
#endif

NS_ASSUME_NONNULL_BEGIN

@interface GradeSchoolTest : XCTestCase

@end

@implementation GradeSchoolTest

- (void)testAnEmptySchool {
    GradeSchool *school = [[GradeSchool alloc] init];
    NSDictionary<NSNumber *, NSArray<NSString *>*> *expected = @{};
    NSDictionary<NSNumber *, NSArray<NSString *>*> *result = [school db];
    XCTAssertEqualObjects(result, expected);
}

- (void)testAddStudent {
    GradeSchool *school = [[GradeSchool alloc] init];
    [school addStudentWithName:@"Aimee" andGrade:@2];
    NSDictionary<NSNumber *, NSArray<NSString *>*> *result = [school db];
    
    NSDictionary<NSNumber *, NSArray<NSString *>*> *expected = @{ @2 : @[ @"Aimee" ] };
    
    XCTAssertEqualObjects(result, expected);
}

- (void)testAddMoreStudentsInSameClass {
    GradeSchool *school = [[GradeSchool alloc] init];
    [school addStudentWithName:@"James" andGrade:@2];
    [school addStudentWithName:@"Blair" andGrade:@2];
    [school addStudentWithName:@"Paul" andGrade:@2];
    NSDictionary<NSNumber *, NSArray<NSString *>*> *result = [school db];
    
    NSDictionary<NSNumber *, NSArray<NSString *>*> *expected = @{ @2 : @[ @"James", @"Blair", @"Paul" ] };
    
    XCTAssertEqualObjects(result, expected);
}


- (void)testAddStudentsToDifferentGrades {
    GradeSchool *school = [[GradeSchool alloc] init];
    [school addStudentWithName:@"Chelsea" andGrade:@3];
    [school addStudentWithName:@"Logan" andGrade:@7];
    NSDictionary<NSNumber *, NSArray<NSString *>*> *result = [school db];
    
    NSDictionary<NSNumber *, NSArray<NSString *>*> *expected = @{ @3 : @[ @"Chelsea" ], @7 : @[ @"Logan" ] };
    XCTAssertEqualObjects(result, expected);
}

- (void)testGetStudentsInAGrade {
    GradeSchool *school = [[GradeSchool alloc] init];
    [school addStudentWithName:@"Franklin" andGrade:@5];
    [school addStudentWithName:@"Bradley" andGrade:@5];
    [school addStudentWithName:@"Jeff" andGrade:@1];
    NSArray<NSString *> *result = [school studentsInGrade:@5];
    
    NSArray<NSString *> *expected = @[ @"Franklin", @"Bradley" ];
    XCTAssertEqualObjects(result, expected);
}

- (void)testGetStudentsInANonExistantGrade {
    GradeSchool *school = [[GradeSchool alloc] init];
    NSArray<NSString *> *result = [school studentsInGrade:@1];
    
    NSArray<NSString *> *expected = @[];
    XCTAssertEqualObjects(result, expected);
}

- (void)testSortSchool {
    GradeSchool *school = [[GradeSchool alloc] init];
    [school addStudentWithName:@"Jennifer" andGrade:@4];
    [school addStudentWithName:@"Kareem" andGrade:@6];
    [school addStudentWithName:@"Christopher" andGrade:@4];
    [school addStudentWithName:@"Kyle" andGrade:@3];
    NSDictionary<NSNumber *, NSArray<NSString *>*> *result = [school sort];
    
    NSDictionary<NSNumber *, NSArray<NSString *>*> *expected = @{ @3 : @[ @"Kyle"],
                                @4 : @[ @"Christopher", @"Jennifer" ],
                                @6 : @[ @"Kareem"] };
    
    XCTAssertEqualObjects(result, expected);
}

@end
NS_ASSUME_NONNULL_END

GradeSchool/GradeSchool Tests/GradeSchool.h

//
//  GradeSchool.h
//  GradeSchool Tests
//
//  Created by Ingemar Pertl on 15.07.19.
//  Copyright © 2019 Ingemar Pertl. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface GradeSchool : NSObject

-(instancetype)init;

-(void) addStudentWithName:(NSString*)name andGrade:(NSNumber*)grade;
-(NSDictionary<NSNumber *, NSArray<NSString *>*> *) db;
-(NSArray<NSString *> *) studentsInGrade: (NSNumber*)grade;
-(NSDictionary<NSNumber *, NSArray<NSString *>*> *) sort;

@end

NS_ASSUME_NONNULL_END

GradeSchool/GradeSchool Tests/GradeSchool.m

//
//  GradeSchool.m
//  GradeSchool Tests
//
//  Created by Ingemar Pertl on 15.07.19.
//  Copyright © 2019 Ingemar Pertl. All rights reserved.
//

#import "GradeSchool.h"

@implementation GradeSchool {
    NSMutableDictionary<NSNumber *, NSMutableArray<NSString *>*> *roster;
}

-(instancetype)init {
    self = [super init];
    roster = [NSMutableDictionary dictionary];
    
    return self;
}

-(NSDictionary<NSNumber *, NSArray<NSString *>*> *) db {
    return roster;
}

-(void)addStudentWithName:(NSString*)name andGrade:(NSNumber*)grade {
    if(![roster.allKeys containsObject:grade]) {
        [roster setObject:[NSMutableArray<NSString *> new] forKey:grade];
    }
    
    [roster[grade] addObject:name];
}

-(NSArray<NSString *> *)studentsInGrade:(NSNumber*)grade {
    return roster[grade] ?: @[];
}

-(NSDictionary<NSNumber *, NSArray<NSString *>*> *)sort {
    NSMutableDictionary<NSNumber *, NSArray<NSString *>*> *dictionary = [NSMutableDictionary dictionary];
    NSArray *sortedKeys = [[roster allKeys] sortedArrayUsingSelector:@selector(intValue)];
    
    for (NSNumber *key in sortedKeys) {
        [dictionary setObject:[roster[key] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] forKey:key];
    }
    
    return dictionary;
}

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