Function Name: bouncyHouse nputs (1): - (cell) a cell array that represents your
ID: 3537618 • Letter: F
Question
Function Name: bouncyHouse
nputs (1): - (cell) a cell array that represents your backyard
% Outputs (1): - (char) a string indicating which size bouncy house will
% fit
%
% Function Description:
% It%u2019s your birthday! And this year, you're going to have the best party
% in school. You'll show up that know-it-all Susie and her pony. Because
% this year, you're getting a bouncy house!
%
% Write a function called 'bouncyHouse' that takes in a cell array
% representing the layout of your backyard and returns a string
% indicating the largest size bouncy house that will fit.
%
% Your input is a cell array describing the layout of your backyard.
%
% The input cell array will represent your yard (meter by meter), with
% each index containing a letter representing either grass ('g'), trees
% ('t'), your house's edge ('h') or the fence ('f'). Obviously, you can%u2019t
% put a bouncy house on top of your house, the fence, or any trees. So
% you need to find the largest open area of grass on which to place the
% bouncy house.
%
% All of the bouncy houses are square and the size options are as
% follows:
% Small = 4x4 meters
% Medium = 5x5 meters
% Large = 6x6 meters
%
% We need to know what size bouncy house will fit. And, you (of course)
% want the biggest bouncy house possible!
%
% To figure out how to solve the problem, let%u2019s look at the algorithm for
% finding the largest possible square area of grass on a smaller level.
%
% If you were given:
%
% f f f f
% f g g f
% f g g f
% h h h h
%
% you would easily be able to see that the biggest square of grass is
% 2x2. How could the computer see this?
%
% First, set all the spots without grass to zeros:
%
% 0 0 0 0
% 0 g g 0
% 0 g g 0
% 0 0 0 0
%
% Then, for every spot of grass, take the minimum value from the
% surrounding positions to the upperleft: the value above, to the left
% of, and diagonally to the left of our current position. For example,
% for the %u201Cg%u201D in row 2, column 2, we would look at the 3 numbers
% surrounding it to its upper left (at positions <2,1>, <1,1>, and
% <1,2>). In this case, each of these numbers are 0, so the minimum is 0.
% We take the minimum plus 1, and assign this value to our current
% position:
%
% 0 0 0 0
% 0 1 g 0
% 0 g g 0
% 0 0 0 0
%
% Following this pattern, you would get:
%
% 0 0 0 0
% 0 1 1 0
% 0 1 2 0
% 0 0 0 0
%
% Note: The value N at an arbitrary (i,j) index is essentially the bottom
% right corner of an NxN open square of grass.
%
% Once you have the array formed, you can find your overall answer by
% finding the maximum number within the array. Your function should
% output either 'Small', 'Medium', 'Large', or 'Sorry. There's not enough
% room for a bouncy house.' depending on the results of the function.
%
% Test Cases:
% yd ={'f' 'f' 'f' 'f' 'f' 'f' 'f' 'f'
'g' 'f' 'f' 'f' 'f' 'f' 'f' 'g'
'g' 'f' 'f' 'f' 'f' 'f' 'f' 'g'
'g' 'f' 'f' 'f' 'f' 'f' 'f' 'g'
'g' 'f' 'f' 'f' 'f' 'f' 'f' 'g'
'g' 'f' 'f' 'f' 'f' 'f' 'f' 'g'
'g' 'f' 'f' 'f' 'f' 'f' 'f' 'g'
'g' 'f' 'f' 'f' 'f' 'f' 'f' 'g'
'h' 'h' 'h' 'h' 'h' 'h' 'h' 'h'
% out1=bouncyHouse(yd);
% out1 => 'Large'
%
% yd{6,6}='t';
% out2 = bouncyHouse(yd);
% out2 => 'Small'
%
% yd{5,2}='t';
% yd{2,4}='t';
% yd{4,6}='t';
% out3 = bouncyHouse(yd);
% out3 => 'Sorry. There''s not enough room for a bouncy house.'
Explanation / Answer
% the sample input output you have given is wrong. Please check manually before testing input in this program
function output = bouncyHouse(backyard)
val_logic = cellfun(@(x) x=='g',backyard);
val= val_logic +0;
cols= length(val(1,:));
rows = length(val(:,1));
for i=2:rows
for j=2:cols
if(val(i,j)==1)
val(i,j) = min([val(i-1,j) val(i-1,j-1) val(i,j-1)]) +1;
end
end
end
size=max(max(val));
if(size < 4)
output = 'Sorry. There''s not enough room for a bouncy house.';
elseif(size ==4 )
output='Small';
elseif(size == 5)
output='Medium';
elseif(size>=6)
output='Large';
end
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.