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

atfelix's solution

to Space Age in the Objective-C Track

Published at Sep 10 2018 · 0 comments
Instructions
Test suite
Solution

Given an age in seconds, calculate how old someone would be on:

  • Earth: orbital period 365.25 Earth days, or 31557600 seconds
  • Mercury: orbital period 0.2408467 Earth years
  • Venus: orbital period 0.61519726 Earth years
  • Mars: orbital period 1.8808158 Earth years
  • Jupiter: orbital period 11.862615 Earth years
  • Saturn: orbital period 29.447498 Earth years
  • Uranus: orbital period 84.016846 Earth years
  • Neptune: orbital period 164.79132 Earth years

So if you were told someone were 1,000,000,000 seconds old, you should be able to say that they're 31.69 Earth-years old.

If you're wondering why Pluto didn't make the cut, 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/space-age/SpaceAge.m

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

Source

Partially inspired by Chapter 1 in Chris Pine's online Learn to Program tutorial. http://pine.fm/LearnToProgram/?Chapter=01

Submitting Incomplete Solutions

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

SpaceAgeTest.m

#import <XCTest/XCTest.h>

#if __has_include("SpaceAgeExample.h")
# import "SpaceAgeExample.h"
# else
# import "SpaceAge.h"
#endif

NS_ASSUME_NONNULL_BEGIN

@interface SpaceAgeTest : XCTestCase

@end

@implementation SpaceAgeTest

static const double AgeAccuracy = 0.01;

- (void)testAgeInSeconds {
    SpaceAge *age = [[SpaceAge alloc] initWithSeconds:1000000];
    XCTAssertEqual(1000000, age.seconds);
}

- (void)testAgeInEarthYears { //!OCLINT
    SpaceAge *age = [[SpaceAge alloc] initWithSeconds:1000000000];
    XCTAssertEqualWithAccuracy(31.69, age.onEarth, AgeAccuracy);
}

- (void)testAgeInMercuryYears { //!OCLINT
    SpaceAge *age = [[SpaceAge alloc] initWithSeconds:2134835688];
    XCTAssertEqualWithAccuracy(67.65, age.onEarth, AgeAccuracy);
    XCTAssertEqualWithAccuracy(280.88, age.onMercury, AgeAccuracy);
}

- (void)testAgeInVenusYears { //!OCLINT
    SpaceAge *age = [[SpaceAge alloc] initWithSeconds:189839836];
    XCTAssertEqualWithAccuracy(6.02, age.onEarth, AgeAccuracy);
    XCTAssertEqualWithAccuracy(9.78, age.onVenus, AgeAccuracy);
}

- (void)testAgeOnMars { //!OCLINT
    SpaceAge *age = [[SpaceAge alloc] initWithSeconds:2329871239];
    XCTAssertEqualWithAccuracy(73.83, age.onEarth, AgeAccuracy);
    XCTAssertEqualWithAccuracy(39.25, age.onMars, AgeAccuracy);
}

- (void)testAgeOnJupiter { //!OCLINT
    SpaceAge *age = [[SpaceAge alloc] initWithSeconds:901876382];
    XCTAssertEqualWithAccuracy(28.58, age.onEarth, AgeAccuracy);
    XCTAssertEqualWithAccuracy(2.41, age.onJupiter, AgeAccuracy);
}

- (void)testAgeOnSaturn { //!OCLINT
    SpaceAge *age = [[SpaceAge alloc] initWithSeconds:3000000000];
    XCTAssertEqualWithAccuracy(95.06, age.onEarth, AgeAccuracy);
    XCTAssertEqualWithAccuracy(3.23, age.onSaturn, AgeAccuracy);
}

- (void)testAgeOnUranus { //!OCLINT
    SpaceAge *age = [[SpaceAge alloc] initWithSeconds:3210123456];
    XCTAssertEqualWithAccuracy(101.72, age.onEarth, AgeAccuracy);
    XCTAssertEqualWithAccuracy(1.21, age.onUranus, AgeAccuracy);
}

- (void)testAgeOnNeptune { //!OCLINT
    SpaceAge *age = [[SpaceAge alloc] initWithSeconds:8210123456];
    XCTAssertEqualWithAccuracy(260.16, age.onEarth, AgeAccuracy);
    XCTAssertEqualWithAccuracy(1.58, age.onNeptune, AgeAccuracy);
}

@end
NS_ASSUME_NONNULL_END

SpaceAge/SpaceAge Tests/Planet.h

@import Foundation;

typedef enum : NSUInteger {
    PlanetMercury,
    PlanetVenus,
    PlanetEarth,
    PlanetMars,
    PlanetJupiter,
    PlanetSaturn,
    PlanetUranus,
    PlanetNeptune
} EarthSolarSystem;

@interface Planet : NSObject

@property (nonatomic, readonly) NSTimeInterval lengthOfYear;

-(instancetype)initWithLengthOfYear:(NSTimeInterval)length;
-(NSTimeInterval)lengthInYearsForSeconds:(NSTimeInterval)seconds;

@end


@interface Planet (EarthPlanets)

+(Planet *)solarSystemFromPlanet:(EarthSolarSystem)planet;

@end

SpaceAge/SpaceAge Tests/Planet.m

#import "Planet.h"

@implementation Planet

-(instancetype)initWithLengthOfYear:(NSTimeInterval)length {
    self = [super init];
    if (self) {
        _lengthOfYear = length;
    }
    return self;
}

-(NSTimeInterval)lengthInYearsForSeconds:(NSTimeInterval)seconds {
    return seconds / self.lengthOfYear;
}

@end


@implementation Planet (EarthPlanets)

+(Planet *)solarSystemFromPlanet:(EarthSolarSystem)planet {
    NSTimeInterval earthSecondsPerYear = 31557600;
    switch (planet) {
        case PlanetMercury: return [[Planet alloc] initWithLengthOfYear:earthSecondsPerYear * 0.2408467];
        case PlanetVenus: return [[Planet alloc] initWithLengthOfYear:earthSecondsPerYear * 0.61519726];
        case PlanetEarth: return [[Planet alloc] initWithLengthOfYear:earthSecondsPerYear];
        case PlanetMars: return [[Planet alloc] initWithLengthOfYear:earthSecondsPerYear * 1.8808158];
        case PlanetJupiter: return [[Planet alloc] initWithLengthOfYear:earthSecondsPerYear * 11.862615];
        case PlanetSaturn: return [[Planet alloc] initWithLengthOfYear:earthSecondsPerYear * 29.447498];
        case PlanetUranus: return [[Planet alloc] initWithLengthOfYear:earthSecondsPerYear * 84.016846];
        case PlanetNeptune: return [[Planet alloc] initWithLengthOfYear:earthSecondsPerYear * 164.79132];
        default: return nil;
    }
}

@end

SpaceAge/SpaceAge Tests/SpaceAge.h

@import Foundation;

@interface SpaceAge : NSObject

@property (nonatomic, readonly) NSTimeInterval onMercury;
@property (nonatomic, readonly) NSTimeInterval onVenus;
@property (nonatomic, readonly) NSTimeInterval onEarth;
@property (nonatomic, readonly) NSTimeInterval onMars;
@property (nonatomic, readonly) NSTimeInterval onJupiter;
@property (nonatomic, readonly) NSTimeInterval onSaturn;
@property (nonatomic, readonly) NSTimeInterval onUranus;
@property (nonatomic, readonly) NSTimeInterval onNeptune;
@property (nonatomic, readonly) NSTimeInterval seconds;

-(instancetype)initWithSeconds:(NSUInteger)seconds;

@end

SpaceAge/SpaceAge Tests/SpaceAge.m

#import "SpaceAge.h"
#import "Planet.h"

@implementation SpaceAge

-(instancetype)initWithSeconds:(NSUInteger)seconds {
    self = [super init];
    if (self) {
        _seconds = seconds;
    }
    return self;
}

-(NSTimeInterval)onMercury {
    return [self convertFromEarthSeconds:self.seconds toEarthSolarSystemPlanet:PlanetMercury];
}

-(NSTimeInterval)onVenus {
    return [self convertFromEarthSeconds:self.seconds toEarthSolarSystemPlanet:PlanetVenus];
}

-(NSTimeInterval)onEarth {
    return [self convertFromEarthSeconds:self.seconds toEarthSolarSystemPlanet:PlanetEarth];
}

-(NSTimeInterval)onMars {
    return [self convertFromEarthSeconds:self.seconds toEarthSolarSystemPlanet:PlanetMars];
}

-(NSTimeInterval)onJupiter {
    return [self convertFromEarthSeconds:self.seconds toEarthSolarSystemPlanet:PlanetJupiter];
}

-(NSTimeInterval)onSaturn {
    return [self convertFromEarthSeconds:self.seconds toEarthSolarSystemPlanet:PlanetSaturn];
}

-(NSTimeInterval)onUranus {
    return [self convertFromEarthSeconds:self.seconds toEarthSolarSystemPlanet:PlanetUranus];
}

-(NSTimeInterval)onNeptune {
    return [self convertFromEarthSeconds:self.seconds toEarthSolarSystemPlanet:PlanetNeptune];
}

-(NSTimeInterval)convertFromEarthSeconds:(NSTimeInterval)seconds toEarthSolarSystemPlanet:(EarthSolarSystem)planet {
    return [[Planet solarSystemFromPlanet:planet] lengthInYearsForSeconds:seconds];
}

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