Cody

# Problem 201. Mimic foldl in functional programming

Solution 1837411

Submitted on 4 Jun 2019
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Pass
assert(isequal(foldl(@plus, 0, 1:100), 5050));

[Warning: Function assert has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In unix (line 32) In foldl (line 2) In ScoringEngineTestPoint1 (line 1) In solutionTest (line 3)]

2   Pass
r = rand(1, 100); y = foldl(@plus, 0, r); yc = sum(r); assert(abs(y - yc)/abs(y) < 1e-6);

[Warning: Function assert has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In unix (line 32) In foldl (line 2) In ScoringEngineTestPoint2 (line 2) In solutionTest (line 5)]

3   Pass
assert(isequal(foldl(@times, 1, 1:15), factorial(15)));

[Warning: Function assert has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In unix (line 32) In foldl (line 2) In ScoringEngineTestPoint3 (line 1) In solutionTest (line 7)]

4   Pass
r = rand(1, 1000); assert(isequal(foldl(@max, -Inf, r), max(r)));

[Warning: Function assert has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In unix (line 32) In foldl (line 2) In ScoringEngineTestPoint4 (line 2) In solutionTest (line 9)]

5   Pass
assert(isequal(foldl(@(x_, y_) 2*x_ + y_, 4, 1:3), 43));

[Warning: Function assert has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In unix (line 32) In foldl (line 2) In ScoringEngineTestPoint5 (line 1) In solutionTest (line 11)]

6   Fail
sumplusfive = foldl(@plus, 5); assert(isequal(sumplusfive(1:100), 5055));

[Warning: Function assert has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In unix (line 32) In foldl (line 2) In ScoringEngineTestPoint6 (line 1) In solutionTest (line 13)]

Index exceeds the number of array elements (1). Error in Test6 (line 2) assert(isequal(sumplusfive(1:100), 5055));

7   Fail
concat = foldl(@(x_, y_) [x_ y_]); r1 = rand(1, 10); r2 = rand(1, 10); assert(isequal(concat(r1, r2), [r1 r2]));

[Warning: Function assert has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In unix (line 32) In foldl (line 2) In ScoringEngineTestPoint7 (line 1) In solutionTest (line 15)]

Index in position 1 is invalid. Array indices must be positive integers or logical values. Error in Test7 (line 4) assert(isequal(concat(r1, r2), [r1 r2]));