Cody

# Problem 201. Mimic foldl in functional programming

Solution 2961724

Submitted on 18 Sep 2020
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   Fail
assert(isequal(foldl(@plus, 0, 1:100), 5050));

Assertion failed.

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

Assertion failed.

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

Assertion failed.

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

Assertion failed.

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

Assertion failed.

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

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]));

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]));

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!