Avatar of sabomi

sabomi's solution

to Grains in the PL/SQL Track

Published at Jul 13 2018 · 1 comment
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

Go through the setup instructions for PL/SQL to get ready to code:

http://exercism.io/languages/plsql

Running the Tests

Execute the tests by calling the run method in the respective ut_<exercise># package. The necessary code should be contained at the end of the test package. As an example, the test for the hamming exercise would be run using

begin
  ut_hamming#.run;
end;
/

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.

ut_grains#.plsql

create or replace package ut_grains#
is
  procedure run;
end ut_grains#;
/
 
create or replace package body ut_grains#
is
  procedure test (
    i_descn                                       varchar2
   ,i_exp                                         number
   ,i_act                                         number
  )
  is
  begin
    if i_exp = i_act then
      dbms_output.put_line('SUCCESS: ' || i_descn);
    else
      dbms_output.put_line('FAILURE: ' || i_descn || ' - expected ' || nvl('' || i_exp, 'null') || ', but received ' || nvl('' || i_act, 'null'));
    end if;
  end test;
 
  procedure run
  is
  begin
    test(i_descn => 'test_square_1'    , i_exp => 1                   , i_act => grains#.at_square(1 ));
    test(i_descn => 'test_square_2'    , i_exp => 2                   , i_act => grains#.at_square(2 ));
    test(i_descn => 'test_square_3'    , i_exp => 4                   , i_act => grains#.at_square(3 ));
    test(i_descn => 'test_square_4'    , i_exp => 8                   , i_act => grains#.at_square(4 ));
    test(i_descn => 'test_square_16'   , i_exp => 32768               , i_act => grains#.at_square(16));
    test(i_descn => 'test_square_32'   , i_exp => 2147483648          , i_act => grains#.at_square(32));
    test(i_descn => 'test_square_64'   , i_exp => 9223372036854775808 , i_act => grains#.at_square(64));
    test(i_descn => 'test_total_grains', i_exp => 18446744073709551615, i_act => grains#.total        );
  end run;
end ut_grains#;
/

begin
  ut_grains#.run;
end;
/
create or replace package grains#
is
  --+--------------------------------------------------------------------------+
  -- Calculates the number of grains of wheat on a chessboard given that 
  -- the number on each square doubles (and starts with 1 grain on the first
  -- square).
  -- There are 64 squares on a chessboard
  --+--------------------------------------------------------------------------+
  
  
  --+--------------------------------------------------------------------------+
  -- Calculates the number of grains of wheat on a chessboard on a given square
  --
  -- @param n        natural number indicating the n-th square
  -- returns         the amount of grains on this square
  --+--------------------------------------------------------------------------+  
  function at_square (
    n                                 pls_integer
  ) return number;
  
  --+--------------------------------------------------------------------------+
  -- Calculates the number of grains of wheat on a whole chessboard 
  --
  -- returns         the amount of grains on a whole chessboard
  --+--------------------------------------------------------------------------+  
  function total return number;
  
  --+--------------------------------------------------------------------------+ 
  -- Global exeception if not a valid square
  --+--------------------------------------------------------------------------+ 
  not_a_valid_square                  exception;
    
end grains#;
/

create or replace package body grains#
is
   function at_square (
    n                                pls_integer
  ) return number
  as
  begin  
     if n < 1 or n > 64
     then
        raise not_a_valid_square;
     end if;   
     
     return power(2,(n-1));
  
  exception 
     when others 
       then raise;
  end at_square;
  
--------------------------------

    function total return number
    is  
    begin
    
      return power(2,64)-1;    
    
      exception 
     when others 
       then raise;
    
    end total; 

end grains#;
/

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of sabomi
sabomi
Solution Author
commented over 4 years ago

I got rid of the unnecessary local variables. Not much code left, but it still works :-)

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?