Given a string s representing a list of numbers, find the five consecutive numbers that multiply to form the largest number. Specifically, given s return the index i to the first of those five numbers. You can assume the maximum product is unique.
Example:
Input s = '123454321' Output i = 3
since the product of [3 4 5 4 3] is larger than any of the alternatives.
Inspired by Problem 8 from Project Euler
It's a pity the test set does not include a case such that the largest running sum is not also the largest running product. Some solutions, including my solution of size 23, should fail because they take the sum, not the product.
Moving sum of logarithm gives the moving product. Also, to reduce the code size, simply convolves log(+s) with any positive
constant, such as 32 (the ASCII code of space).
By taking log, the moving sum computed via convolution is indeed the moving product.
nice
Goodddd
Interesting idea. But the code can be simplified, as demonstrated in Solution 1373422, which cuts the size by 13.
A case of not enough tests in the Test Suite?
This solution also computes the running sum instead the product. The interesting thing to show here is that convn also works with char input
That's interesting. The documentation says it works only for double and single.
It's only because the test set is inadequate that this solution, which uses the running sum not the running product, works. My size-23 solution exploits the same loophole, but to see it done properly using convolution see my solution 19416, of size 25.
This should not be a solution, because it takes the sum, not the product. It's a pity the test set does not include a case such that the largest running sum is not also the largest running product.
This should use prod, not sum. As for my own solution 19425, the test cases fail to detect this error (at the time of writing).
852 Solvers
1057 Solvers
Replace NaNs with the number that appears to its left in the row.
1712 Solvers
Get the elements of diagonal and antidiagonal for any m-by-n matrix
199 Solvers
Generate a random matrix A of (1,-1)
162 Solvers