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

damiankorz's solution

to Largest Series Product in the CFML Track

Published at Dec 26 2018 · 0 comments
Instructions
Test suite
Solution

Given a string of digits, calculate the largest product for a contiguous substring of digits of length n.

For example, for the input '1027839564', the largest product for a series of 3 digits is 270 (9 * 5 * 6), and the largest product for a series of 5 digits is 7560 (7 * 8 * 3 * 9 * 5).

Note that these series are only required to occupy adjacent positions in the input; the digits need not be numerically consecutive.

For the input '73167176531330624919225119674426574742355349194934', the largest product for a series of 6 digits is 23520.


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

A variation on Problem 8 at Project Euler http://projecteuler.net/problem=8

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

LargestSeriesProductTest.cfc

component extends="testbox.system.BaseSpec" {

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

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

			it( 'finds the largest product if span equals length', function(){
				expect( SUT.largestProduct( digits='29', span='2' ) ).toBe( '18' );
			});

			it( 'can find the largest product of 2 with numbers in order', function(){
				expect( SUT.largestProduct( digits='0123456789', span='2' ) ).toBe( '72' );
			});

			it( 'can find the largest product of 2', function(){
				expect( SUT.largestProduct( digits='576802143', span='2' ) ).toBe( '48' );
			});

			it( 'can find the largest product of 3 with numbers in order', function(){
				expect( SUT.largestProduct( digits='0123456789', span='3' ) ).toBe( '504' );
			});

			it( 'can find the largest product of 3', function(){
				expect( SUT.largestProduct( digits='1027839564', span='3' ) ).toBe( '270' );
			});

			it( 'can find the largest product of 5 with numbers in order', function(){
				expect( SUT.largestProduct( digits='0123456789', span='5' ) ).toBe( '15120' );
			});

			it( 'can get the largest product of a big number', function(){
				expect( SUT.largestProduct( digits='73167176531330624919225119674426574742355349194934', span='6' ) ).toBe( '23520' );
			});

			it( 'reports zero if the only digits are zero', function(){
				expect( SUT.largestProduct( digits='0000', span='2' ) ).toBe( '0' );
			});

			it( 'reports zero if all spans include zero', function(){
				expect( SUT.largestProduct( digits='99099', span='3' ) ).toBe( '0' );
			});

			it( 'rejects span longer than string length', function(){
				expect( SUT.largestProduct( digits='123', span='4' ) ).toBe( '-1' );
			});

			it( 'reports 1 for empty string and empty product (0 span)', function(){
				expect( SUT.largestProduct( digits='', span='0' ) ).toBe( '1' );
			});

			it( 'reports 1 for nonempty string and empty product (0 span)', function(){
				expect( SUT.largestProduct( digits='123', span='0' ) ).toBe( '1' );
			});

			it( 'rejects empty string and nonzero span', function(){
				expect( SUT.largestProduct( digits='', span='1' ) ).toBe( '-1' );
			});

			it( 'rejects invalid character in digits', function(){
				expect( SUT.largestProduct( digits='1234a5', span='2' ) ).toBe( '-1' );
			});

			it( 'rejects negative span', function(){
				expect( SUT.largestProduct( digits='12345', span='-1' ) ).toBe( '-1' );
			});

		});
		
	}
 
}

SolutionTest.cfc

component extends="LargestSeriesProductTest" {

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

}
component {
	/**
	* @returns
	*/
	public numeric function largestProduct( required string digits, required numeric span ) {
		if( digits == "" && span > 0){
			return -1;
		}
		if( !isNumeric(digits) && digits != ""){
			return -1;
		}
		if( span > len(digits) || span < 0 ){
			return -1;
		}
		if( span == 0 ){
			return 1;
		}
		if( len(replace(digits, "0", "", "all")) == 0){
			return 0;
		}
		digits = digits.split("");
		var count = 1 + span;
		var maxSum = 1;
		var spanCount = 0; 
		var spanZeroCount = 0; 
		while( count <= arrayLen(digits) + 1 ){
			var currentSum = 1;
			array = arraySlice(digits, count - span, span );
			spanCount += 1;
			if( arrayContains(array, 0)){
				spanZeroCount += 1; 
			}
			for( number in array ){
				currentSum *= number;
			}
			if( currentSum > maxSum ){
				maxSum = currentSum;
			}
			count += 1;
		}
		if( spanCount == spanZeroCount ){
			return 0; 
		}
		return maxSum;
	}
}

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?