Test | Status | Code Input and Output |
---|---|---|
1 | Pass |
%%
user_solution = fileread('hungry_snake.m');
assert(isempty(strfind(user_solution,'regexp')));
assert(isempty(strfind(user_solution,'num2str')));
assert(isempty(strfind(user_solution,'interp')));
assert(isempty(strfind(user_solution,'fprintf')));
assert(isempty(strfind(user_solution,'assert')));
|
2 | Pass |
%%
fprintf('Testing...\n')
for a = 0:8,
% Get the matrix
M = hungry_snake(a);
%
% Check that all the numbers exist once in 2^a x 2^a matrix
assert(isequal(size(M),[2^a,2^a]),'Bad Size');
assert(isequal(1:numel(M),sort(M(:))'),'Not all numbers exist!');
%
% Find the locations of the numbers
[I,J] = arrayfun(@(x)find(M==x,1),1:numel(M));
%
% Check that the numbers form indeed a snake
assert(all((abs(diff(I))==1&diff(J)==0) | (abs(diff(J))==1&diff(I)==0)),'Not a Snake!');
%
% Check that there isn't a straight line longer than 4
msl = max(cellfun('length',regexp(sprintf('%d',[diff(I) NaN diff(J)]),'0+','match')));
if a>0, assert( msl < 4,'More than 4 consecutive numbers!'); end
fprintf('\ta=%d : OK!\n',a);
end
%
fprintf('\n.\nChuck Norris would be proud!\n')
%
%
Testing...
a=0 : OK!
a=1 : OK!
a=2 : OK!
a=3 : OK!
a=4 : OK!
a=5 : OK!
a=6 : OK!
a=7 : OK!
a=8 : OK!
.
Chuck Norris would be proud!
|
The Hitchhiker's Guide to MATLAB
2696 Solvers
Determine Whether an array is empty
561 Solvers
Square Digits Number Chain Terminal Value (Inspired by Project Euler Problem 92)
146 Solvers
97 Solvers
1495 Solvers