Cody

Problem 44484. Separate even from odd numbers in a vector - with a loop

Solution 2414595

Submitted on 29 May 2020 by Shahar Kalfon
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
filetext = fileread('oddEven.m'); assert(isempty(strfind(filetext, 'regexp')),'regexp hacks are forbidden')

2   Pass
filetext = fileread('oddEven.m'); loopUsed = ~isempty(strfind(filetext, 'while')) || ~isempty(strfind(filetext, 'for')); assert(loopUsed, 'Must use at least one loop')

3   Pass
v = []; w_correct = []; assert(isequal(oddEven(v),w_correct))

w = []

4   Pass
v = [2; 7; 0; 3; 2]; w_correct = [7; 3; 2; 0; 2]; assert(isequal(oddEven(v),w_correct))

v = 2 7 0 3 2 a = 1.0000 3.5000 0 1.5000 1.0000 c = 1×5 logical array 0 1 0 1 0 e = 7 3 w = 7 3 2 0 2 w = 7 3 2 0 2

5   Pass
v = [1, 0, 2, 9, 3, 8, 8, 4]; w_correct = [1, 9, 3, 0, 2, 8, 8, 4]; assert(isequal(oddEven(v),w_correct))

a = 0.5000 0 1.0000 4.5000 1.5000 4.0000 4.0000 2.0000 c = 1×8 logical array 1 0 0 1 1 0 0 0 e = 1 9 3 w = 1 9 3 0 2 8 8 4

6   Pass
odd = 2 * randi([-4, 4], 1, randi([4,10])) - 1; even = 2 * randi([-4, 4], 1, randi([4,10])); v = [even, odd]; w_correct = [odd, even]; assert(isequal(oddEven(v),w_correct))

a = 0 -1.0000 -4.0000 -2.0000 1.0000 -3.0000 3.0000 1.5000 -3.5000 -1.5000 -0.5000 -3.5000 -0.5000 -2.5000 -3.5000 c = 1×15 logical array 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 e = 3 -7 -3 -1 -7 -1 -5 -7 w = 3 -7 -3 -1 -7 -1 -5 -7 0 -2 -8 -4 2 -6 6

Suggested Problems

More from this Author25

Community Treasure Hunt

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

Start Hunting!