ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# webmandman's solution

## to Largest Series Product in the CFML Track

Published at Dec 12 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
``````

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' );
}

}``````
``````/**
* Your implmentation of the LargestSeriesProduct exercise
*/
component {

/**
* @returns
*/
function largestProduct( digits, span ) {
// reject invalid params
if(span gt len(digits)) return -1;
if(len(digits) eq 0) return 1;
if(span eq 0) return 1;
if(not isNumeric(digits)) return -1;
if(span lt 0) return -1;

var largestProduct = 0;
// continuously evaluate all series, as long the series is of the correct span length
// (span-1) will make sure the last iteration is not less than the span
for(var i = 1; i <= len(digits)-(span-1); i++){
// get the series based on the span size
var series = listToArray(mid(digits,i,span),"");
// build the multiplication problem
var expression = ""; // n*n*n*
series.each(function(digit,index){ expression = expression & digit & "*"});
// using the expression do the math and get the product, remove trailing *
var product = precisionEvaluate(left(expression,len(expression)-1));
// save only the largestProduct
if( product gt largestProduct){
largestProduct = product;
}
}
return largestProduct;
}

}``````