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

damiankorz's solution

to Grains in the CFML Track

Published at Dec 14 2018 · 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. 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?


To run the code in this exercise, you will only need to have CommandBox CLI installed. This binary runs CFML code from the command line.

To run the tests, cd into the exercise folder and run the following:

box task run TestRunner
# Or start up a test watcher that will rerun when files change
box task run TestRunner --:watcher

The tests leverage a library called TestBox which supports xUnit and BDD style of testing. All test suites will be written in the BDD style which uses closures to define test specs. You won't need to worry about installing TestBox. The CLI test runner will take care of that for you. You just need to be connected to the internet the first time you run it. You can read more about it here:

https://testbox.ortusbooks.com/content/

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.

GrainsTest.cfc

component extends="testbox.system.BaseSpec" {

	function beforeAll(){
	  SUT = createObject( 'Grains' );
	}

	function run(){
	
		describe( "My Grains class", function(){			

			describe( 'returns the number of grains on the square', function(){

				it( '1', function(){
					expect( SUT.square( input='1' ) ).toBe( '1' );
				});

				it( '2', function(){
					expect( SUT.square( input='2' ) ).toBe( '2' );
				});

				it( '3', function(){
					expect( SUT.square( input='3' ) ).toBe( '4' );
				});

				it( '4', function(){
					expect( SUT.square( input='4' ) ).toBe( '8' );
				});

				it( '16', function(){
					expect( SUT.square( input='16' ) ).toBe( '32768' );
				});

				it( '32', function(){
					expect( SUT.square( input='32' ) ).toBe( '2147483648' );
				});

				it( '64', function(){
					expect( SUT.square( input='64' ) ).toBe( '9223372036854775808' );
				});

				it( 'square 0 raises an exception', function(){
					expect( SUT.square( input='0' ) ).toBe( '-1' );
				});

				it( 'negative square raises an exception', function(){
					expect( SUT.square( input='-1' ) ).toBe( '-1' );
				});

				it( 'square greater than 64 raises an exception', function(){
					expect( SUT.square( input='65' ) ).toBe( '-1' );
				});

			});

			it( 'returns the total number of grains on the board', function(){
				expect( SUT.total() ).toBe( 18446744073709551615 );
			});

		});
		
	}
 
}

SolutionTest.cfc

component extends="GrainsTest" {

	function beforeAll(){
	  SUT = createObject( 'Solution' );
	}

}
component {
	public numeric function square( required numeric input ) {
		var wheat = 1;
		if( input < 1 || input > 64 ){
			return -1;
		}else{
			for( var i = 2; i<=input; i++){
				wheat *= 2;
			} 
			return wheat;
		}
	}

	public numeric function total() {
		var wheat = 1;
		var totalWheat = wheat;
		for( var i = 2; i<65; i++){
			wheat *= 2;
			totalWheat += wheat;
		}return totalWheat;
	}
}

damiankorz's Reflection

Generated test case for 64 fails...output is off by a value of 1... rest of test cases pass