Spring 2001 CS100M Solutions to Exercise E3 Suppose you know that $x$ is a vector with 3 numbers but you don't know the numbers. Write code, subject to the specified constraints, to print the numbers in descending order. [out of order, for convenience] 3. Bonus: Do so without $sort$, conditionals, loops, or assignment statements. Answer: $[max(x) sum(x)-max(x)-min(x) min(x)]$ or $[max(x) median(x) min(x)]$ 1. Do so without conditionals, loops, or assignment statements. Answer: $fliplr(sort(x))$ or $flipud(sort(x))$ or $-sort(-x)$ ($dsort(x)$ works, but is weird because it is for eignenvalues) Note: $fliplr$ or $flipud$ aren't helpful if vector is the wrong shape (column versus row) Note: a number of people used assignment statements despite them not being allowed for this question 2. Do so without $sort$ or loops. HINT: Use conditionals to swap any 2 numbers that are out of order. Questions: + are we allowed to modify $x$? if unclear, seek clarification! frequently, it is a bad idea to modify the original values, e.g. suppose when you go to the ATM to ask for your bank balance it shows you your balance while simultaneously wiping it out. you'd be justifiably upset! + remember: don't forget boundary conditions! in this case, don't forget that duplicate values are allowed (since they are not disallowed). + what would be a good set of test data? Answers and non-answers -- try to figure out which ones work and which don't! Attempt 1 y = x; if y(1)= y(2) if y(2)= y(3), i.e. y(3) is in final place if y(1)= y(2) >= y(3), i.e. entirely sorted y Attempt 2 if x(1)>x(2) & x(2)>x(3) [x(1) x(2) x(3)] elseif x(1)>x(3) & x(3)>x(2) [x(1) x(3) x(2)] elseif x(2)>x(1) & x(1)>x(3) [x(2) x(1) x(3)] elseif x(2)>x(3) & x(3)>x(1) [x(2) x(3) x(1)] elseif x(3)>x(1) & x(1)>x(2) [x(3) x(1) x(2)] elseif x(3)>x(2) & x(2)>x(1) [x(3) x(2) x(1)] end Attempt 3 if x(1)>x(2) if x(2)>x(3) [x(1) x(2) x(3)] elseif x(1)>x(3) [x(1) x(3) x(2)] else [x(3) x(1) x(2)] end else % x(2) >= x(1) if x(1)>x(3) [x(2) x(1) x(3)] elseif x(2)>x(3) [x(2) x(3) x(1)] else [x(3) x(2) x(1)] end end _______________________________________________________________________________ good test data: need to cover all possible orders thus [1 2 3], [1 3 2], [2 1 3], [2 3 1], [3 1 2], [3 2 1] for 3 different numbers. also, [1 1 2], [1 2 1], [2 1 1], for exactly 2 numbers the same. and [1 1 1] for all 3 numbers the same. _______________________________________________________________________________ Attempt 1: run the code below to see the test cases where it fails -- if there are none, then Attempt 1 is correct. for x = [1 2 3; 1 3 2; 2 1 3; 2 3 1; 3 1 2; 3 2 1; 1 1 2; 1 2 1; 2 1 1; 1 1 1]' y = x; if y(1)x(2) & x(2)>x(3) y=[x(1) x(2) x(3)]; elseif x(1)>x(3) & x(3)>x(2) y=[x(1) x(3) x(2)]; elseif x(2)>x(1) & x(1)>x(3) y=[x(2) x(1) x(3)]; elseif x(2)>x(3) & x(3)>x(1) y=[x(2) x(3) x(1)]; elseif x(3)>x(1) & x(1)>x(2) y=[x(3) x(1) x(2)]; elseif x(3)>x(2) & x(2)>x(1) y=[x(3) x(2) x(1)]; end if any(y ~= -sort(-x')) disp(x') end end Attempt 3: run the code below to see the test cases where it fails -- if there are none, then Attempt 3 is correct. for x = [1 2 3; 1 3 2; 2 1 3; 2 3 1; 3 1 2; 3 2 1; 1 1 2; 1 2 1; 2 1 1; 1 1 1]' if x(1)>x(2) if x(2)>x(3) y=[x(1) x(2) x(3)]; elseif x(1)>x(3) y=[x(1) x(3) x(2)]; else y=[x(3) x(1) x(2)]; end else % x(2) >= x(1) if x(1)>x(3) y=[x(2) x(1) x(3)]; elseif x(2)>x(3) y=[x(2) x(3) x(1)]; else y=[x(3) x(2) x(1)]; end end if any(y ~= -sort(-x')) disp(x') end end