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

KiligWY's solution

to Grains in the Objective-C Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Calculate the number of grains of wheat on a chessboard given that the number on each square doubles.

There once was a wise servant who saved the life of a prince. The king promised to pay whatever the servant could dream up. Knowing that the king loved chess, the servant told the king he would like to have grains of wheat. One grain on the first square of a chess board. Two grains on the next. Four on the third, and so on.

There are 64 squares on a chessboard.

Write code that shows:

  • how many grains were on each square, and
  • the total number of grains

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:

  • Optimize for speed.
  • Optimize for readability.

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/grains/Grains.m

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

Source

JavaRanch Cattle Drive, exercise 6 http://www.javaranch.com/grains.jsp

Submitting Incomplete Solutions

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

GrainsTest.m

#import <XCTest/XCTest.h>

#if __has_include("GrainsExample.h")
# import "GrainsExample.h"
# else
# import "Grains.h"
#endif


NS_ASSUME_NONNULL_BEGIN

@interface GrainsTest : XCTestCase

@end

@implementation GrainsTest

- (void)testNumberOfGrainsOnSquare1 {
    long long expected = 1;
    long long result = [[[Grains alloc]init]grainsAtSquareNumber:1];
    XCTAssertEqual(expected,result);
}

- (void)testNumberOfGrainsOnSquare2 {
    long long expected = 2;
    long long result = [[[Grains alloc]init]grainsAtSquareNumber:2];
    XCTAssertEqual(expected,result);
}

- (void)testNumberOfGrainsOnSquare3 {
    unsigned long long expected = 4;
    unsigned long long result = [[[Grains alloc]init]grainsAtSquareNumber:3];
    XCTAssertEqual(expected,result);
}

- (void)testNumberOfGrainsOnSquare4 {
    unsigned long long expected = 8;
    unsigned long long result = [[[Grains alloc]init]grainsAtSquareNumber:4];
    XCTAssertEqual(expected,result);
}

- (void)testNumberOfGrainsOnSquare16 {
    unsigned long long expected = 32768;
    unsigned long long result = [[[Grains alloc]init]grainsAtSquareNumber:16];
    XCTAssertEqual(expected,result);
}

- (void)testNumberOfGrainsOnSquare32 {
    unsigned long long expected = 2147483648;
    unsigned long long result = [[[Grains alloc]init]grainsAtSquareNumber:32];
    XCTAssertEqual(expected,result);
}

- (void)testNumberOfGrainsOnSquare64 {
    unsigned long long expected = 9223372036854775808;
    unsigned long long result = [[[Grains alloc]init]grainsAtSquareNumber:64];
    XCTAssertEqual(expected,result);
}

- (void)testNumberOfGrainsOnSquare65 {
    unsigned long long expected = -1;
    unsigned long long result = [[[Grains alloc]init]grainsAtSquareNumber:65];
    XCTAssertEqual(expected,result);
}

- (void)testNumberOfGrainsOnSquareNeg1 {
    unsigned long long expected = -1;
    unsigned long long result = [[[Grains alloc]init]grainsAtSquareNumber:-1];
    XCTAssertEqual(expected,result);
}

- (void)testNumberOfGrainsOnSquare0 {
    unsigned long long expected = -1;
    unsigned long long result = [[[Grains alloc]init]grainsAtSquareNumber:0];
    XCTAssertEqual(expected,result);
}

- (void)testTotalNumberOfGrains {
    unsigned long long expected = 18446744073709551615;
    unsigned long long result = [[[Grains alloc]init]grainsAtBoard];
    XCTAssertEqual(expected,result);
}

@end
NS_ASSUME_NONNULL_END

Grains.h

#import <Foundation/Foundation.h>

@interface Grains : NSObject

- (unsigned long long)grainsAtSquareNumber:(NSInteger)number;
- (unsigned long long)grainsAtBoard;

@end

Grains.m

#import "Grains.h"

@implementation Grains

- (unsigned long long)grainsAtSquareNumber:(NSInteger)number {
    if (number <= 0 || number > 64) {
        return -1;
    }
    return exp2(number - 1);
}

- (unsigned long long)grainsAtBoard {
    return [self grainsAtSquareNumber:64] * 2 - 1;
}

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