and I'm wondering why this solution is smaller in size than this: http://www.mathworks.com/matlabcentral/cody/problems/2312-how-many-solutions-has-this-problem/solutions/440185 O_o
Well, when you submit a solution, correct answer changes a little bit, isn't it? :-P
but my solution is also a general solution and has less operations in it. O_o
Test | Status | Code Input and Output |
---|---|---|
1 | Pass |
%%
% please ''ignore'' this first test, this code is here to put function
% urlfilter known from matlabcentral/trendy to the path for this problem
% (method taken from Doug's Hull problem Steal, Share, or Catch)
fh=fopen('urlfilter.m','wt');
fprintf(fh, '%s \n', 'function out = urlfilter(url, target, numNumbers, direction)');
fprintf(fh, '%s \n', ' if nargin < 3, numNumbers = 1; end');
fprintf(fh, '%s \n', ' if nargin < 4 direction = ''forward''; end');
fprintf(fh, '%s \n', ' % If url is not an actual URL, then treat it as a string');
fprintf(fh, '%s \n', ' if strcmp(url(1:4),''http''), textStr = urlread(url); else textStr = url; end');
fprintf(fh, '%s \n', ' % Handle special case where two numbers are given as part of a range');
fprintf(fh, '%s \n', ' % Example: "annual rainfall = 20-40 inches"');
fprintf(fh, '%s \n', ' % Solution is a pre-processing step that replaces the dash with a space');
fprintf(fh, '%s \n', ' % Example: "annual rainfall = 20 40 inches"');
fprintf(fh, '%s \n', ' textStr = regexprep(textStr,''(\d+)-(\d+)'',''$1 $2'');');
fprintf(fh, '%s \n', ' strIndex = strfind(textStr,target);');
fprintf(fh, '%s \n', ' if isempty(strIndex),');
fprintf(fh, '%s \n', ' error( ''trendy:urlfilter:TargetStringNotFound'', ...');
fprintf(fh, '%s \n', ' [''Target string '' target '' does not appear''])');
fprintf(fh, '%s \n', ' end');
fprintf(fh, '%s \n', ' % Start looking after the first appearance of the target');
fprintf(fh, '%s \n', ' if strcmp(direction,''forward'')');
fprintf(fh, '%s \n', ' strIndex = strIndex(1) + length(target);');
fprintf(fh, '%s \n', ' elseif strcmp(direction,''backward'')');
fprintf(fh, '%s \n', ' strIndex = strIndex(1) - 1;');
fprintf(fh, '%s \n', ' else');
fprintf(fh, '%s \n', ' error( ''trendy:urlfilter:InvalidDirectionFlag'', ...');
fprintf(fh, '%s \n', ' ''DIRECTION must be either ''''forward'''' or ''''backward''''.'' );');
fprintf(fh, '%s \n', ' end');
fprintf(fh, '%s \n', ' out = zeros(1,numNumbers);');
fprintf(fh, '%s \n', ' for i = 1:numNumbers');
fprintf(fh, '%s \n', ' [out(i),strIndex] = getNextNumber(textStr,strIndex,direction);');
fprintf(fh, '%s \n', ' end');
fprintf(fh, '%s \n', 'end');
fprintf(fh, '%s \n', '% =========================');
fprintf(fh, '%s \n', 'function [nextNumber,strIndex] = getNextNumber(textStr,strIndex,direction)');
fprintf(fh, '%s \n', ' % Use a state machine to sift through the HTML for numbers.');
fprintf(fh, '%s \n', ' if strcmp(direction,''forward'')');
fprintf(fh, '%s \n', ' openTagSymbol = ''<'';');
fprintf(fh, '%s \n', ' closeTagSymbol = ''>'';');
fprintf(fh, '%s \n', ' moveIndexFcn = @(x) x+1;');
fprintf(fh, '%s \n', ' concatenateFcn = @(a,b) [a b];');
fprintf(fh, '%s \n', ' else');
fprintf(fh, '%s \n', ' openTagSymbol = ''>'';');
fprintf(fh, '%s \n', ' closeTagSymbol = ''<'';');
fprintf(fh, '%s \n', ' moveIndexFcn = @(x) x-1;');
fprintf(fh, '%s \n', ' concatenateFcn = @(a,b) [b a];');
fprintf(fh, '%s \n', ' end');
fprintf(fh, '%s \n', ' urlStrLen = length(textStr);');
fprintf(fh, '%s \n', ' state = ''notnumber'';');
fprintf(fh, '%s \n', ' while true');
fprintf(fh, '%s \n', ' ch = textStr(strIndex);');
fprintf(fh, '%s \n', ' switch state');
fprintf(fh, '%s \n', ' case ''notnumber''');
fprintf(fh, '%s \n', ' if isDigitDotDashOrComma(ch)');
fprintf(fh, '%s \n', ' state = ''number'';');
fprintf(fh, '%s \n', ' numStr = ch;');
fprintf(fh, '%s \n', ' elseif (ch == openTagSymbol)');
fprintf(fh, '%s \n', ' state = ''tagbody'';');
fprintf(fh, '%s \n', ' end');
fprintf(fh, '%s \n', ' case ''tagbody''');
fprintf(fh, '%s \n', ' % Throw away anything inside the tag markup area');
fprintf(fh, '%s \n', ' if (ch == closeTagSymbol)');
fprintf(fh, '%s \n', ' state = ''notnumber'';');
fprintf(fh, '%s \n', ' end ');
fprintf(fh, '%s \n', ' case ''number''');
fprintf(fh, '%s \n', ' if isDigitDotDashOrComma(ch)');
fprintf(fh, '%s \n', ' numStr = concatenateFcn(numStr,ch);');
fprintf(fh, '%s \n', ' else');
fprintf(fh, '%s \n', ' % We are transitioning out of a number.');
fprintf(fh, '%s \n', ' % Note that STR2DOUBLE handles commas in the string.');
fprintf(fh, '%s \n', ' nextNumber = str2double(numStr);');
fprintf(fh, '%s \n', ' if ~isnan(nextNumber)');
fprintf(fh, '%s \n', ' % The number is valid. We''re all done.');
fprintf(fh, '%s \n', ' break');
fprintf(fh, '%s \n', ' else');
fprintf(fh, '%s \n', ' % The number is bogus. Throw it away and continue.');
fprintf(fh, '%s \n', ' if (ch == openTagSymbol), state = ''tagbody''; else state = ''notnumber''; end');
fprintf(fh, '%s \n', ' end');
fprintf(fh, '%s \n', ' end');
fprintf(fh, '%s \n', ' otherwise');
fprintf(fh, '%s \n', ' error( ''trendy:urlfilter:InvalidState'', ...');
fprintf(fh, '%s \n', ' [''Encountered unknown state '' state])');
fprintf(fh, '%s \n', ' end');
fprintf(fh, '%s \n', ' strIndex = moveIndexFcn(strIndex);');
fprintf(fh, '%s \n', ' if (strIndex == 0) || (strIndex > urlStrLen)');
fprintf(fh, '%s \n', ' % We ran off the end of the string.');
fprintf(fh, '%s \n', ' disp(''End of file reached.'')');
fprintf(fh, '%s \n', ' break');
fprintf(fh, '%s \n', ' end');
fprintf(fh, '%s \n', ' end');
fprintf(fh, '%s \n', 'end');
fprintf(fh, '%s \n', '% ==================');
fprintf(fh, '%s \n', 'function tf = isDigitDotDashOrComma(ch)');
fprintf(fh, '%s \n', ' tf = ((ch >= ''0'') && (ch <= ''9'')) || ...');
fprintf(fh, '%s \n', ' (ch == ''.'') || (ch == ''-'') || (ch == '','');');
fprintf(fh, '%s \n', 'end');
fclose(fh);
|
2 | Pass |
%%
rehash path
|
3 | Pass |
%%
y=solutions();
assert(isequal(mod(y,1),0))
|
4 | Pass |
%%
y=solutions();
assert(y>0);
|
5 | Pass |
%%
y=solutions();
% yes, yes solution hidden in the test suite... try to code adifferent one! :-)
url='http://www.mathworks.co.uk/matlabcentral/cody/problems/2312'
y_correct=max([0 cellfun(@(S)str2num(cell2mat(S)),regexp(urlread(url),'<span class="solution_statistic">(\d*)</span><span class="text"> Solutions</span>','tokens'))]) + 1; % don't forget your solution
t = mtree('solutions.m','-file');
size = ceil(length(t.nodesize)/13);
feval(@assignin,'caller','score',abs(y-y_correct)+size);
% (... and solutions will be rescored from time to time :-D)
url =
http://www.mathworks.co.uk/matlabcentral/cody/problems/2312
|
3074 Solvers
4597 Solvers
177 Solvers
292 Solvers
260 Solvers
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!