Avatar of bkhl

bkhl's solution

to Grains in the Bash Track

Published at Jan 05 2019 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

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?

Run the tests with:

bats grains_test.sh

After the first test(s) pass, continue by commenting out or removing the skip annotations prepending other tests.

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.sh

#!/usr/bin/env bats

@test "Square 1" {
  #skip
  run bash grains.sh 1

  [ "$status" -eq 0 ]
  [ "$output" = "1" ]
}

@test "Square 2" {
  #skip
  run bash grains.sh 2

  [ "$status" -eq 0 ]
  [ "$output" = "2" ]
}

@test "Square 3" {
  #skip
  run bash grains.sh 3

  [ "$status" -eq 0 ]
  [ "$output" = "4" ]
}

@test "Square 4" {
  #skip
  run bash grains.sh 4

  [ "$status" -eq 0 ]
  [ "$output" = "8" ]
}

@test "Square 16" {
  #skip
  run bash grains.sh 16

  [ "$status" -eq 0 ]
  [ "$output" = "32768" ]
}

@test "Square 32" {
  #skip
  run bash grains.sh 32

  [ "$status" -eq 0 ]
  [ "$output" = "2147483648" ]
}

@test "Square 64" {
  #skip
  run bash grains.sh 64

  [ "$status" -eq 0 ]
  [ "$output" = "9223372036854775808" ]
}

@test "Square 0 causes error" {
  #skip
  run bash grains.sh 0

  [ "$status" -eq 1 ]
  [ "$output" = "Error: invalid input" ]
}

@test "Square -1 causes error" {
  #skip
  run bash grains.sh -1

  [ "$status" -eq 1 ]
  [ "$output" = "Error: invalid input" ]
}

@test "Square 65 causes error" {
  #skip
  run bash grains.sh 65

  [ "$status" -eq 1 ]
  [ "$output" = "Error: invalid input" ]
}
#!/bin/bash

# Print the number of grains on the given chess board square.
square() {
    n="$1"

    if [[ $n -lt 1 || $n -gt 64 ]]; then
        echo "Error: invalid input"
        return 1
    fi

    printf %u $((2 ** (n - 1)))
}

# Print the total number of grains on the given chess board square,
# and all the squares before it.
sum_of_squares() {
    n="$1"

    printf %u $(((2 ** n) - 1))
}

main() {
    input="$1"

    if [[ $input == total ]]; then
        sum_of_squares 64
    else
        square "$input" || return 1
    fi
}

main "$*" || exit 1

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?