Cody

Problem 44545. "Percentages may not total 100 due to rounding"

Solution 1470915

Submitted on 25 Mar 2018 by William
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
assessFunctionAbsence({'regexp', 'regexpi'}, 'FileName','checkForMistake.m')

2   Pass
xVec = round( 100*[5 1 5]/11 ); containsMistake = false; assert( isequal(checkForMistake(xVec), containsMistake) )

3   Pass
xVec = round( 100*[5 1 5]/11 )'; containsMistake = false; assert( isequal(checkForMistake(xVec), containsMistake) )

4   Pass
xVec = [42 20 45]; containsMistake = true; assert( isequal(checkForMistake(xVec), containsMistake) )

5   Pass
xVec = [100]; containsMistake = false; assert( isequal(checkForMistake(xVec), containsMistake) , 'Failed test a') xVec = round([100.5]); containsMistake = true; assert( isequal(checkForMistake(xVec), containsMistake) , 'Failed test b') xVec = round([99.49]); containsMistake = true; assert( isequal(checkForMistake(xVec), containsMistake) , 'Failed test c')

6   Pass
xVec = [50 50]; containsMistake = false; assert( isequal(checkForMistake(xVec), containsMistake) , 'Failed test a') xVec = round([49.5 50.5]); containsMistake = false; assert( isequal(checkForMistake(xVec), containsMistake) , 'Failed test b') xVec = round([50.5 50.5]); containsMistake = true; assert( isequal(checkForMistake(xVec), containsMistake) , 'Failed test c') xVec = round([49.49 50.49]); containsMistake = true; assert( isequal(checkForMistake(xVec), containsMistake) , 'Failed test d')

7   Pass
for j = [2:250 1000:1000:10000] xVec = round( repelem(100/j, j) ); containsMistake = false; assert( isequal(checkForMistake(xVec), containsMistake) ) end;

8   Pass
for j = 10:30 xVec = round( 10 * (9/10).^[0:j] ); containsMistake = j < 21; assert( isequal(checkForMistake(xVec), containsMistake) ) end;

9   Pass
for j = 2:40 xVec = round( 10 * (9/10).^[0:j] ); xVec = xVec( randperm(j+1) ); containsMistake = j < 21; assert( isequal(checkForMistake(xVec), containsMistake) ) end;

10   Pass
for j = 2:100 xVec = round( (99/100).^[0:j] ); xVec = xVec( randperm(j+1) ); containsMistake = j < 66; assert( isequal(checkForMistake(xVec), containsMistake) ) end;

11   Pass
for j = 2:30 xVec = round( 20 * (4/5).^[0:j] ); xVec = xVec( randperm(j+1) ); containsMistake = j < 12; assert( isequal(checkForMistake(xVec), containsMistake) ) end;

12   Pass
for j = 2:20 xVec = round( 25 * (3/4).^[0:j] ); xVec = xVec( randperm(j+1) ); containsMistake = j < 10; assert( isequal(checkForMistake(xVec), containsMistake) ) end;

13   Pass
for j = 2:20 xVec = round( 50 * (1/2).^[0:j] ); xVec = xVec( randperm(j+1) ); containsMistake = j < 5; assert( isequal(checkForMistake(xVec), containsMistake) ) end;

14   Pass
for j = 2:100 num = randi(round(100/j)+1) - 1; xRaw = repelem(num+0.5, j); % cf. https://oletus.github.io/float16-simulator.js/ sm = sum(xRaw); xRaw = [xRaw max(100-sm, 0)]; if sm > 100.5, % Not sm>100, because need to account for extra zero added. containsMistake = true; else containsMistake = false; end; xVec = round( xRaw ); xVec = xVec( randperm(j+1) ); assert( isequal(checkForMistake(xVec), containsMistake) , ['Failed with xRaw = ' num2str(xRaw)] ) end;

15   Pass
for j = 2:100 num = randi(round(100/j)+1) - 1; xRaw = repelem(num+0.499755859375, j); % cf. https://oletus.github.io/float16-simulator.js/ & https://au.mathworks.com/help/matlab/matlab_prog/floating-point-numbers.html cs = cumsum(xRaw); if cs(end) > 100, xRaw( cs > 100 ) = []; containsMistake = true; else xRaw = [xRaw (100-cs(end))]; containsMistake = false; end; xVec = round( xRaw ); assert( isequal(checkForMistake(xVec), containsMistake) , ['Failed with xRaw = ' num2str(xRaw)] ) end;