Problem 54. Maximum running product for a string of numbers
Solution Stats
Problem Comments

2 Comments
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.
I would suggest adding '9909911111' as a test case. That would weed out all people who took a shortcut by using a moving sum, instead of a moving product.
Solution Comments

1 Comment
function i = running_product(s)
L=length(s);
L1=L4;
tmp=0;
i=0;
for j=1:L1
m=str2num(s(j))*str2num(s(j+1))*str2num(s(j+2))*str2num(s(j+3))*str2num(s(j+4));
if m>tmp
tmp=m;
i=j;
end
end
end

1 Comment
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).

2 Comments
By taking log, the moving sum computed via convolution is indeed the moving product.
It's a very good idea!

1 Comment
nice

2 Comments
Goodddd
Interesting idea. But the code can be simplified, as demonstrated in Solution 1373422, which cuts the size by 13.

1 Comment
A case of not enough tests in the Test Suite?

2 Comments
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.

1 Comment
It's only because the test set is inadequate that this solution, which uses the running sum not the running product, works. My size23 solution exploits the same loophole, but to see it done properly using convolution see my solution 19416, of size 25.

1 Comment
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.

1 Comment
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).
Problem Recent Solvers1416
Suggested Problems

Find the two most distant points
1969 Solvers

Make a random, nonrepeating vector.
4630 Solvers

Change the sign of even index entries of the reversed vector
355 Solvers

340 Solvers

419 Solvers
More from this Author95
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!