Avatar of nicolemon

nicolemon's solution

to Grains in the Bash Track

Published at Sep 11 2018 · 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

External utilities

Bash is a language to write scripts that works closely with various system utilities, like sed, awk, date and even other programming languages, like Python. This track does not restrict the usage of these utilities, and as long as your solution is portable between systems and does not require installing third party applications, feel free to use them to solve the exercise.

For an extra challenge, if you would like to have a better understanding of the language, try to re-implement the solution in pure Bash, without using any external tools.

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 "1" {
  #skip
  run bash grains.sh 1
  [ "$status" -eq 0 ]
  [ "$output" == "1" ]
}

@test "2" {
  skip
  run bash grains.sh 2
  [ "$status" -eq 0 ]
  [ "$output" == "2" ]
}

@test "3" {
  skip
  run bash grains.sh 3
  [ "$status" -eq 0 ]
  [ "$output" == "4" ]
}

@test "4" {
  skip
  run bash grains.sh 4
  [ "$status" -eq 0 ]
  [ "$output" == "8" ]
}

@test "16" {
  skip
  run bash grains.sh 16
  [ "$status" -eq 0 ]
  [ "$output" == "32768" ]
}

@test "32" {
  skip
  run bash grains.sh 32
  [ "$status" -eq 0 ]
  [ "$output" == "2147483648" ]
}

@test "64" {
  skip
  run bash grains.sh 64
  [ "$status" -eq 0 ]
  [ "$output" == "9223372036854775808" ]
}

@test "square 0 raises an exception" {
  skip
  run bash grains.sh 0
  [ "$status" -eq 1 ]
  [ "$output" == "Error: invalid input" ]
}

@test "negative square raises an exception" {
  skip
  run bash grains.sh -1
  [ "$status" -eq 1 ]
  [ "$output" == "Error: invalid input" ]
}

@test "square greater than 64 raises an exception" {
  skip
  run bash grains.sh 65
  [ "$status" -eq 1 ]
  [ "$output" == "Error: invalid input" ]
}

@test "returns the total number of grains on the board" {
  skip
  run bash grains.sh total
  [ "$status" -eq 0 ]
  [ "$output" == "18446744073709551615" ]
}
#!/usr/bin/env bash

set -o errexit
set -o nounset

_unsigned_int() {
    local number
    local hex

    number=$1
    hex=$(printf "0x%x" $(( "$number" )) )

    printf "%u" "$hex"
}

grains_on_square() {
    # calculates the number of grains on square
    local square
    local result

    square=$(_unsigned_int "$1")
    result=$(( 2**$(( square - 1 )) ))
    _unsigned_int "$result"
}

grains_on_board() {
    # calculates the total number of grains on the board up to square
    local square
    local result

    square=$(_unsigned_int "$1")
    result=$(( (2**"$square") - 2**0 ))
    _unsigned_int "$result"
}

main() {

    input=$1
    if [[ $input == "total" ]]; then
        grains_on_board 64
        return 0
    elif [[ 10#$input -gt 0 ]] 2>/dev/null && [[ 10#$input -le 64 ]]; then
        grains_on_square "$input"
        return 0
    else
        echo "Error: invalid input"
        return 1
    fi

}

main "$@"

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?