Cody

# Problem 44405. Back to basics: adding comments to your code

Solution 1423628

Submitted on 24 Jan 2018 by margreet docter
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({'rng', ... By the way, with an ellipsis (signalling line continuation) '!', 'assert', ... all subsequent material on that line is ignored. But syntactically they 'evalin', 'assignin', ... are _not_ comments (even if they are "effectively" similar). 'system', 'dos', ... E.g. they would not be treated as comments by an autodoc (automatic documentation) tool. 'unix'}, 'FileName', 'myFunction.m') assert( isempty( regexp(fileread('myFunction.m'),'(?# By the way, comments can also be inserted in regexp "regular expression" arguments.)assert','match') ) , 'No "assert" allowed in your code!')

2   Pass
commentType1 = regexp(fileread('myFunction.m'),'(?<![\f\n\r][^'']*''[^''\f\n\r]*)(?<!\o45)\o45[^\o45][^\f\n\r]{10,}','match'); commentType1(end) = [] assert( ~isempty( commentType1 ) , 'Basic comment type missing or too short.') commentType2 = regexp(fileread('myFunction.m'),'\o45\o45[^\f\n\r]{10,}','match') assert( ~isempty( commentType2 ) , 'Delimiting comment type missing or too short.') commentType3open = regexp(fileread('myFunction.m'),'(^|[\f\n\r]*)\x{20}*\o45\o173\x{20}*[\f\n\r]') commentType3close = regexp(fileread('myFunction.m'),'(^|[\f\n\r]*)\x{20}*\o45\o175\x{20}*[\f\n\r]') if isempty(commentType3open) || isempty(commentType3open) assert(false, 'Block comment type missing.') else assert( length(commentType3close) == length(commentType3open) , 'Block comment tokens unbalanced.') assert( max(commentType3close - commentType3open) > 20 , 'Block comment type too short.') end;

commentType1 = 1×4 cell array {'% calulcate X with infitinite preci…'} {'% round to increasing factors of 10'} {'% find the absolute difference'} {'% quit if the right precision is fo…'} commentType2 = 1×1 cell array {'%% rounding x'} commentType3open = 29 commentType3close = 138

3   Pass
A = 1; x_correct = 1.3572; assert( isequal(myFunction(A), x_correct) )

ans = 0 1.2247 ans = 1.0000 0.1153 ans = 2.0000 0.0077 ans = 3.0000 0.0006 ans = 4.0000 0.0000 xr = 1.3572

4   Pass
A = 2; x_correct = 1.7652; assert( isequal(myFunction(A), x_correct) )

ans = 0 0.5495 ans = 1.0000 0.0813 ans = 2.0000 0.0113 ans = 3.0000 0.0004 ans = 4.0000 0.0001 xr = 1.7652

5   Pass
A = 3; x_correct = 2.1898; assert( isequal(myFunction(A), x_correct) )

ans = 0 0.4505 ans = 1.0000 0.0246 ans = 2.0000 0.0006 ans = 3.0000 0.0006 ans = 4.0000 0.0001 xr = 2.1898

6   Pass
A = 4; x_correct = 2.59625; assert( isequal(myFunction(A), x_correct) )

ans = 0 1.0498 ans = 1.0000 0.0095 ans = 2.0000 0.0095 ans = 3.0000 0.0006 ans = 4.0000 0.0001 ans = 5.0000 0.0000 xr = 2.5962

7   Pass
A = 5; x_correct = 2.9814; assert( isequal(myFunction(A), x_correct) )

ans = 0 0.0498 ans = 1.0000 0.0498 ans = 2.0000 0.0036 ans = 3.0000 0.0010 ans = 4.0000 0.0001 xr = 2.9814

8   Pass
A = 6; x_correct = 3.3472; assert( isequal(myFunction(A), x_correct) )

ans = 0 0.9502 ans = 1.0000 0.1317 ans = 2.0000 0.0079 ans = 3.0000 0.0005 ans = 4.0000 0.0001 xr = 3.3472

9   Pass
A = 7; x_correct = 3.6963; assert( isequal(myFunction(A), x_correct) )

ans = 0 0.9057 ans = 1.0000 0.0109 ans = 2.0000 0.0109 ans = 3.0000 0.0008 ans = 4.0000 0.0001 xr = 3.6963

10   Pass
A = 8; x_correct = 4.031; assert( isequal(myFunction(A), x_correct) )

ans = 0 0.0943 ans = 1.0000 0.0943 ans = 2.0000 0.0031 ans = 3.0000 0.0000 xr = 4.0310

11   Pass
A = 9; x_correct = 4.3533; assert( isequal(myFunction(A), x_correct) )

ans = 0 1.0943 ans = 1.0000 0.1479 ans = 2.0000 0.0104 ans = 3.0000 0.0009 ans = 4.0000 0.0000 xr = 4.3533

12   Pass
A = 10; x_correct = 4.6647; assert( isequal(myFunction(A), x_correct) )

ans = 0 1.1131 ans = 1.0000 0.1155 ans = 2.0000 0.0153 ans = 3.0000 0.0010 ans = 4.0000 0.0001 xr = 4.6647

13   Pass
% Test added 2019-06-24 A = 481; x_correct = 61.39; assert( isequal(myFunction(A), x_correct) )

ans = 0 4.5763 ans = 1.0000 0.1175 ans = 2.0000 0.0000 xr = 61.3900

14   Pass
% Test added 2019-06-24 A = 922; x_correct = 94.73; assert( isequal(myFunction(A), x_correct) )

ans = 0 3.9447 ans = 1.0000 0.4379 ans = 2.0000 0.0000 xr = 94.7300

15   Pass
% Test added 2019-06-24 A = 3981; x_correct = 251.185635; assert( isequal(myFunction(A), x_correct) )

ans = 0 4.4123 ans = 1.0000 0.3415 ans = 2.0000 0.1038 ans = 3.0000 0.0087 ans = 4.0000 0.0008 ans = 5.0000 0.0001 ans = 6.0000 0.0000 xr = 251.1856

16   Pass
% Test added 2019-06-24 A = 3988; x_correct = 251.48; assert( isequal(myFunction(A), x_correct) )

ans = 0 11.4123 ans = 1.0000 0.4758 ans = 2.0000 0.0001 xr = 251.4800

17   Pass
% Test added 2019-06-24 A = 3989; x_correct = 251.522035; assert( isequal(myFunction(A), x_correct) )

ans = 0 11.3758 ans = 1.0000 0.5242 ans = 2.0000 0.0484 ans = 3.0000 0.0008 ans = 4.0000 0.0008 ans = 5.0000 0.0001 ans = 6.0000 0.0000 xr = 251.5220

18   Pass
% Test added 2019-06-24 A = 7997; x_correct = 399.9; assert( isequal(myFunction(A), x_correct) )

ans = 0 2.9999 ans = 1.0000 0.0001 xr = 399.9000

19   Pass
% Test added 2019-06-24 A = 8000; x_correct = 400; assert( isequal(myFunction(A), x_correct) )

ans = 1.0e-04 * 0 0.9375 xr = 400

20   Pass
% Test added 2019-06-24 A = 8003; x_correct = 400.1; assert( isequal(myFunction(A), x_correct) )

ans = 0 3.0001 ans = 1.0000 0.0001 xr = 400.1000

21   Pass
threshold = 0.0001; for j = 1 : 10 A = randi(10) + rand(); x_candidate = myFunction(A); A_approx = exp( log(x_candidate * x_candidate * x_candidate - (1 + 1/2)) / 2); assert( abs(A - A_approx) <= threshold ) x_rough = round(x_candidate, 6); if abs(x_rough - x_candidate) > 10*eps(x_candidate) A_rough = exp( log(x_rough * x_rough * x_rough - (1 + 1/2)) / 2); assert( abs(A - A_rough) > threshold ) end; x_rough = round(x_candidate, 5); if abs(x_rough - x_candidate) > 10*eps(x_candidate) A_rough = exp( log(x_rough * x_rough * x_rough - (1 + 1/2)) / 2); assert( abs(A - A_rough) > threshold ) end; x_rough = round(x_candidate, 4); if abs(x_rough - x_candidate) > 10*eps(x_candidate) A_rough = exp( log(x_rough * x_rough * x_rough - (1 + 1/2)) / 2); assert( abs(A - A_rough) > threshold ) end; x_rough = round(x_candidate, 3); if abs(x_rough - x_candidate) > 10*eps(x_candidate) A_rough = exp( log(x_rough * x_rough * x_rough - (1 + 1/2)) / 2); assert( abs(A - A_rough) > threshold ) end; x_rough = round(x_candidate, 2); if abs(x_rough - x_candidate) > 10*eps(x_candidate) A_rough = exp( log(x_rough * x_rough * x_rough - (1 + 1/2)) / 2); assert( abs(A - A_rough) > threshold ) end; x_rough = round(x_candidate, 1); if abs(x_rough - x_candidate) > 10*eps(x_candidate) A_rough = exp( log(x_rough * x_rough * x_rough - (1 + 1/2)) / 2); assert( abs(A - A_rough) > threshold ) end; end;

ans = 0 0.5317 ans = 1.0000 0.0825 ans = 2.0000 0.0105 ans = 3.0000 0.0012 ans = 4.0000 0.0000 xr = 4.1734 ans = 0 1.1350 ans = 1.0000 0.0452 ans = 2.0000 0.0039 ans = 3.0000 0.0010 ans = 4.0000 0.0000 xr = 1.5184 ans = 0 0.0291 ans = 1.0000 0.0291 ans = 2.0000 0.0056 ans = 3.0000 0.0009 ans = 4.0000 0.0000 xr = 1.9876 ans = 0 0.5382 ans = 1.0000 0.0618 ans = 2.0000 0.0025 ans = 3.0000 0.0005 ans = 4.0000 0.0001 xr = 3.8208 ans = 0 0.9704 ans = 1.0000 0.0272 ans = 2.0000 0.0056 ans = 3.0000 0.0009 ans = 4.0000 0.0000 xr = 4.7083 ans = 0 0.2880 ans = 1.0000 0.0478 ans = 2.0000 0.0143 ans = 3.0000 0.0010 ans = 4.0000 0.0000 xr = 4.9143 ans = 0 1.0519 ans = 1.0000 0.1136 ans = 2.0000 0.0063 ans = 3.0000 0.0009 ans = 4.0000 0.0001 xr = 1.5526 ans = 0 0.8966 ans = 1.0000 0.0645 ans = 2.0000 0.0092 ans = 3.0000 0.0006 ans = 4.0000 0.0001 xr = 2.3738 ans = 0 0.2402 ans = 1.0000 0.0616 ans = 2.0000 0.0015 ans = 3.0000 0.0015 ans = 4.0000 0.0000 xr = 3.9205 ans = 0 1.1075 ans = 1.0000 0.1013 ans = 2.0000 0.0018 ans = 3.0000 0.0007 ans = 4.0000 0.0000 xr = 2.4593