Problem 1494. Hungry Snake

Solution 242046

Submitted on 9 May 2013 by Tim
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
%% 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!

Suggested Problems

More from this Author18

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!