Problem 1494. Hungry Snake

Solution 290447

Submitted on 26 Jul 2013
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   Fail
%% 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')));

Error: Assertion failed.

2   Fail
%% 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') % %

Error: Maximum recursion limit of 500 reached. Use set(0,'RecursionLimit',N) to change the limit. Be aware that exceeding your available stack space can crash MATLAB and/or your computer.