Avatar of MSirio

MSirio's solution

to Grains in the TypeScript Track

Published at Sep 25 2019 · 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, with the number of grains doubling on each successive square.

There are 64 squares on a chessboard (where square 1 has one grain, square 2 has two grains, and so on).

Write code that shows:

  • how many grains were on a given square, and
  • the total number of grains on the chessboard

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

Go through the setup instructions for TypeScript to install the necessary dependencies:

https://exercism.io/tracks/typescript/installation

Requirements

Install assignment dependencies:

$ yarn install

Making the test suite pass

Execute the tests with:

$ yarn test

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by changing xit to it.

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.

grains.test.ts

import Grains from "./grains"

describe("returns the number of grains on the square", () => {
  it("1", () => {
    const expected = 1
    expect(Grains.square(1)).toEqual(expected)
  })

  xit("2", () => {
    const expected = 2
    expect(Grains.square(2)).toEqual(expected)
  })

  xit("3", () => {
    const expected = 4
    expect(Grains.square(3)).toEqual(expected)
  })

  xit("4", () => {
    const expected = 8
    expect(Grains.square(4)).toEqual(expected)
  })

  xit("16", () => {
    const expected = 32768
    expect(Grains.square(16)).toEqual(expected)
  })

  xit("32", () => {
    const expected = 2147483648
    expect(Grains.square(32)).toEqual(expected)
  })

  xit("64", () => {
    const expected = 9223372036854775808
    expect(Grains.square(64)).toEqual(expected)
  })

  xit("square 0 raises an exception", () => {
    expect(() => Grains.square(0)).toThrow()
  })

  xit("negative square raises an exception", () => {
    expect(() => Grains.square(-1)).toThrow()
  })

  xit("square greater than 64 raises an exception", () => {
    expect(() => Grains.square(65)).toThrow()
  })
})

describe("returns the total number of grains on the board", () => {
  xit("total", () => {
    const expected = 18446744073709551615
    expect(Grains.total()).toEqual(expected)
  })
})
export default class Grains {
    public static square(val: number) {
        if (val <= 0 || val > 64) {
            throw new Error();
        }
        return 2 ** (val-1);
    }

    public static total() {
        let sum = 0;
        for (let i = 0; i < 64; i++) {
            sum += 2 ** i
        }
        return sum;
    }
}

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?