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

nadiar429's solution

to Leap in the Objective-C Track

Published at Dec 09 2020 · 0 comments
Instructions
Test suite
Solution

Given a year, report if it is a leap year.

The tricky thing here is that a leap year in the Gregorian calendar occurs:

on every year that is evenly divisible by 4
  except every year that is evenly divisible by 100
    unless the year is also evenly divisible by 400

For example, 1997 is not a leap year, but 1996 is. 1900 is not a leap year, but 2000 is.

If your language provides a method in the standard library that does this look-up, pretend it doesn't exist and implement it yourself.

Notes

Though our exercise adopts some very simple rules, there is more to learn!

For a delightful, four minute explanation of the whole leap year phenomenon, go watch this youtube video.

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/leap/Leap.m

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

Source

JavaRanch Cattle Drive, exercise 3 http://www.javaranch.com/leap.jsp

Submitting Incomplete Solutions

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

LeapTest.m

#import <XCTest/XCTest.h>

#if __has_include("LeapExample.h")
# import "LeapExample.h"
# else
# import "Leap.h"
#endif

NS_ASSUME_NONNULL_BEGIN

@interface LeapTest : XCTestCase

@end

@implementation LeapTest

- (void)testVanillaLeapYear {
  Leap *year = [[Leap alloc] initWithCalendarYear:@1996];
  XCTAssert(year.isLeapYear);
}

- (void)testAnyOldYear {
  Leap *year = [[Leap alloc] initWithCalendarYear:@1997];
  XCTAssertFalse(year.isLeapYear);
}

- (void)testCentury {
  Leap *year = [[Leap alloc] initWithCalendarYear:@1900];
  XCTAssertFalse(year.isLeapYear);
}

- (void)testExceptionalCentury {
  Leap *year = [[Leap alloc] initWithCalendarYear:@2400];
  XCTAssert(year.isLeapYear);
}

@end
NS_ASSUME_NONNULL_END

Leap/Leap Tests/Leap.h

//
//  Leap.h
//  Leap Tests
//
//  Created by Nadiar Syaripul on 09/12/20.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface Leap : NSObject

@property BOOL isLeapYear;

- (id) initWithCalendarYear:(NSNumber *)year;

@end

NS_ASSUME_NONNULL_END

Leap/Leap Tests/Leap.m

//
//  Leap.m
//  Leap Tests
//
//  Created by Nadiar Syaripul on 09/12/20.
//

#import "Leap.h"

@implementation Leap

- (id) initWithCalendarYear:(NSNumber *)year {
    self = [super init];
    if (self) {
        _isLeapYear = [Leap isDivisibleBy400:year] || (![Leap isDivisibleBy100:year] && [Leap isDivisibleBy4:year]);
    }
    return self;
}

+ (BOOL) isDivisibleBy4:(NSNumber *)year {
    return [year intValue] % 4 == 0;
}

+ (BOOL) isDivisibleBy100:(NSNumber *)year {
    return [year intValue] % 100 == 0;
}

+ (BOOL) isDivisibleBy400:(NSNumber *)year {
    return [year intValue] % 400 == 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?