% Function Name: timeWarp % Inputs (4): - (double) A 1x2 vector representing a r
ID: 3537369 • Letter: #
Question
% Function Name: timeWarp
% Inputs (4): - (double) A 1x2 vector representing a row and column
% - (char) A nxm string array representing directions
% - (double) A nxm array representing number of spaces to
% travel
% - (char) A nxm string array of jumbled letters
% Outputs (1): - (char) A string found after following the directions
% through the array of letters.
%
% Function Description:
% Riff Raff and Magenta have left Earth in their castle/rocket/spaceship.
% They're heading back to their home planet, where they can do the Time
% Warp for as long as they wish. There is just one problem. Riff Raff
% forgot the way and can't fingure out the code for the autopilot!
% Luckily, Magenta found Frankenfurter's hint for the code. It's up to
% you to decipher it. Hurry! Once the castle is on autopilot, we can do
% the time warp!
%
% Write a function called timeWarp that follows the pattern described by
% the clues in the given arrays to output a hidden string, found in the
% fourth array, a string array. The first array given is a 1x2 vector
% representing the starting index. This is the location of the first
% letter and where you should begin. The next step to follow will be
% given by the second input array(direction) and the third input. The
% direction will be designated in the second array by 'u','d','l', or 'r',
% for up, down, left, and right, respectively. The number of steps to be
% taken in the direction found will be found in the third array. The
% number of steps taken will always be positive. Out put the letter found
% in each step into a string for the final answer. The letter to output
% will be the letter at the new index in the fourth input array. Include
% the letter in the starting location. Stop when the direction is 'e'
% (for end) and the number of spaces is 0.
%
%
% Hints:
% - The while() switch() functions should prove useful.
%
% Test Cases:
% start1 = [3,4];
% dir1 = ['rlrdeul'; 'rlddrrr'; 'dudllud'; 'drrldlr';...
% 'uullrrd'; 'drluddd'];
% space1 = [4 5 8 2 0 3 18; 20 19 72 1001 4 9 30; 2 7 5 3 6 2 1; ...
% 1 1 1 1 1 1 1; 4 10 21 18 19 17 6; 0 0 0 0 0 0 0];
% letter1 = ['kbcdyfg';'hijklmn';'opqRstu';'vwxyzaq';...
% 'csrkmpb';'aefgiwn'];
%
% out1 = timeWarp(start1,dir1,space1,letter1);
% => 'Rocky'
%
% start2 = [2,4];
% dir2 = ['rdlrudll';'dudrdurl';'udrrureu';'urrllurl'];
% space2 = [7 2 18 42 12 1 2 6; 3 4 1 1 2 20 19 5;...
% 30 1 4 25 15 2 0 1; 3 4 10 9 4 1 8 7];
% letter2 = ['cocdefgr';'ijelunoe';'qudlus!p';'isabd ef'];
%
% out1 = timeWarp(start2,dir2,space2,letter2);
% => 'ludicrous speed!'
EXPLANATIONS TO SOLUTION PLEASE!
Explanation / Answer
function[out1]= timeWarp(start1,dir1,space1,letter1)
i=start1(1);
j=start1(2);
out1='';
dir='A';
while 1==1
out1=strcat(out1,letter1(i,j));
dir= dir1(i,j);
space=space1(i,j);
switch(dir)
case 'u'
i=i-space;
case 'd'
i=i+space;
case 'l'
j=j-space;
case 'r'
j=j+space;
case 'e'
break;
end
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.