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

katrinleinweber's solution

to Grains in the R Track

Published at Jul 13 2018 · 3 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?

Installation

See this guide for instructions on how to setup your local R environment.

How to implement your solution

In each problem folder, there is a file named <exercise_name>.R containing a function that returns a NULL value. Place your implementation inside the body of the function.

How to run tests

Inside of RStudio, simply execute the test_<exercise_name>.R script. This can be conveniently done with testthat's auto_test function. Because exercism code and tests are in the same folder, use this same path for both code_path and test_path parameters. On the command-line, you can also run Rscript test_<exercise_name>.R.

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.

test_grains.R

source("./grains.R")
library(testthat)

context("grains")

test_that("Case 1", {
  expect_equal(square(1), 1)
})

test_that("Case 2", {
  expect_equal(square(2), 2)
})

test_that("Case 3", {
  expect_equal(square(3), 4)
})

test_that("Case 4", {
  expect_equal(square(4), 8)
})

test_that("Case 16", {
  expect_equal(square(16), 32768)
})

test_that("Case 32", {
  expect_equal(square(32), 2147483648)
})

test_that("Case 1", {
  expect_equal(square(64), 9223372036854775808)
})

test_that("square 0 raises an exception", {
  expect_error(square(0))
})

test_that("negative square raises an exception", {
  expect_error(square(-1))
})

test_that("square greater than 64 raises an exception", {
  expect_error(square(65))
})

test_that("returns the total number of square on the board", {
  expect_equal(total(), 18446744073709551615)
})

message("All tests passed for exercise: grains")
# chess board min & max

square <- function(n) {
  if (1 <= n & n <= 64)
    2 ^ (n - 1)
  else
    stop("That's not a proper chess board!")
}

total <- function() {
  sum(2 ^ ((1:64) - 1))
}

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of katrinleinweber

Optimised to ca +12% speed by omitting the variables for 1 & 64.

Avatar of jejones3141

Wow. That gives me a flashback to the bad old days of the primitive Microsoft BASIC interpreters that you had to write around.

Avatar of katrinleinweber

Sic transit gloria mundi! ;-D

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?