Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Homework 09 - Structures

Function Name: ​medievalFootball

Inputs:
1. (​struct​) A 1xN structure representing the "field" and the players on it

Outputs:
1. (​char​) A short sentence describing what happened.

Topics: ​(​structures​), (​conditionals)​, (​iteration​), (​cell arrays​)

Background:
As you are backpacking through Europe during your "​life changing study abroad
experience​", you stop in a small town for a bite to eat and hear about an interesting local
tradition similar to a game of football. Only it's not American football, it's Medieval Football.
They're exactly the same, except Medieval Football has no limit on team size, no set playing
field, and no rules on aggression. Instead of joining the locals, you decide to sit this one out and
model it in MATLAB instead.

Function Description:
Given a 1xN structure array representing the "game" and only containing the field
"player", determine which goal area (the first and last structures) has a player in it. Then count
how many yards (absolute value of the amount of structures moved) that player can move
across the field without running into another player who politely forces them to stop. Output
what happened in a string with the format:
'<Moving player> madeth it <# of yards they ran> rod(s) ere <blocking
player> stove in <Moving player>'s kneecap!'

Example:
strc →
player: [] 'Helmut 'Bob' [] [] 'Jim'
Wulf'

sout = medievalFootball(strc)
​ Jim madeth it 3 rod(s) ere Bob stove in Jim's kneecap!'
sout​ → '

Notes:
● The player who is in the goal area (either first or last structure) will be the runner
● There will always be ​only​ 1 valid runner and at ​least​ 1 blocker
● Each structure will have only 1 field with name "player"
● The yards ran will be the absolute value of the index difference between the running and
blocking player
Homework 09 - Structures

Function Name: ​quicksandDiving

Inputs:
1. (​struct​) 1xN structure vector representing pits of quicksand

Outputs:
1. (​cell)​ 1xM cell array containing the items found

Topics: ​(​structure nesting​), (​cell arrays​), (​iteration​), (​conditionals​)

Background:
For years, you have been perfecting your craft. It was your goal to be the best quicksand
diver of the east of Mississippi. Ever since you went to your first live match, there has been
nothing else in life that you wanted to do. In high school, you were regarded as the best
quicksand diver that the world has ever seen. It was no surprise when you were admitted into
Georgia Tech, the home of the best Quicksand Diving program in the country under a full ride
scholarship. When you arrived on campus however, you soon began to realize why GT was
ranked #1 year after year. You had been told of the long hours of training and conditioning, but
all of that paled in comparison to the secret weapon that Tech used. They had harnessed the
power of MATLAB in order to make training more efficient and productive.
Up and until now, you have not been allowed to train on the MATLAB machine. They
don’t typically allot time on the machine to rookies, but you have convinced Coach Rodgers,
Coach Stallworth, and Coach Landry that you’re ready. Now all you have to do is prove to them
that you were right.

Function Description:
The goal of Quicksand Diving is to grab as many objects as you can while swimming
deep underwater. Given a 1xN structure array of nested structures, you must output a cell array
that contains the objects you found during your dive. Every structure array is guaranteed to
have 2 fields whose field names may differ between test cases but will remain the same within a
test case's structure array.

If both fields present are ​sorted in ascending alphabetical order​, ​the first field will always
pertain to the nested sand structures being dug through and the ​second field will always contain
the item located in that segment of sand (if there is any). If there is no item in the current
segment of sand (denoted by the items field being equal to ​'sand'​), you should dig deeper by
going further into the field corresponding to nested structures. Repeat the process until you
either find an object (a different char vector) which you should then add onto your output cell
array, or until you reach the bottom of the sand column (denoted by the nested structures field
being equal to ​'end' instead of a new nested structure). If an object is reached before you
reach the bottom of the sand column, you should move onto the next nested structure in the
structure array and stop searching in that specific nested structure for other objects. Once you
obtain all the items you dove for, sort your items cell array before returning it.
Homework 09 - Structures

Example:
st →

depth
depth depth 'end'
depth 'end' item 'beats solo 3
rose gold'
item 'sand'

item 'bouncy ball'

item 'old alien ufo' 'sand'

out = quicksandDiving(st)
out → [{'beats solo 3 rose gold' , 'old alien ufo'}]

Notes:
● The ​field names are not consistent across test cases (but are consistent within a test
case's structure array) and must be found in your code. However, the alphabetical order
of the field names as they correspond to nested structures or items is consistent.
● The final output cell array should be sorted in alphabetical order.
● There may be no items in a nested structure; do not add any item to the output if this is
the case.
● If there are multiple items in a nested structure, you should only grab the first one you
find and then move on to the next nested structure without further diving.
● Items found may repeat and if they do they should still be added to the cell array as
duplicates.

Hints:
● The ​fieldnames() ​function may be useful for you in deciphering what the fieldnames
for nested structures and objects are in the structure array.
Homework 09 - Structures

Function Name: ​nascondino

Inputs:
1. (​structure)​ A 1xM structure which represents a city

Outputs:
1. (​char​) A phrase with all the landmarks that you search, the name of the person you find,
and the landmark at which you find the person.

Topics: ​(​structure arrays​), (​iteration)​ , (​conditional)​

Background:
Nascondino means "hide and seek" in Italian and the Nascondino World Championship
is a major hide and seek competition held in Bergamo, Italy. As part of the competition, a
member of every team hides while the searching team tries to search for these members.
You've been recruited to be on this searching team! Determined to find these team members as
fast as you can, you turn to MATLAB for help to search for them.

Function Description:
You'll be given an 1xM structure array containing three fields. One of the fields will be
Name​. There will be a value in the ​Name ​field only if there is a person hiding there. The next
field will be ​Landmark ​which corresponds to the landmark you are searching. The final field is
Clue ​which will indicate where to search next. The data contained in the ​Clue ​field will have
a direction (​'East' or ​'West'​) followed by a single space and a positive number. The number
represents the number of positions you should go in the given direction before checking the
fields again.

If the direction is ​'East'​, the next location you should check will be ​right​ in the structure array
If the direction is ​'West'​, the next location you should check will be ​left​ in the structure array

Keep searching until the ​Name ​field is not empty. If there is a value in the ​Name ​field, stop
there and output a phrase in the following format:

'I searched <first location>, <second location>, <third location>,


[...], <n-1 location>, and found <Person name> at <Location found>'

Begin your search from the farthest west of the map (index 1).
Homework 09 - Structures

Example:

>> map =

Name: [] 'Veronica' [] []
Landmark: 'Museum' 'Fort' 'Piazza' 'Ragione'
Clue: 'East 2' 'South 1' 'East 1' 'West 2'

>> outStr = nascondino(map)

>> outStr → 'I searched ​Museum, Piazza, Ragione​, and found


Veronica at Fort​'

Notes:
● Include the first index as your first search location as the person may be located there. If
there is a person located there, your output should be, ​'I searched and found
<Person name> at <Location found>'
● You are guaranteed to find only one person.
● You will not be told to go out of bounds of the structure array

Hints:
● Think about concatenating char vectors inside your loop to build your output statement.
Homework 09 - Structures

Function Name: ​surfLifeRuffLife

Inputs:
1. (​structure)​ A 1xN structure containing stats on the dogs in the competition
2. (structure) A 1xM structure containing stats on the people in the competition
3. (structure) A 1xL structure containing stats on potential surfboards

Outputs:
1. (​cell array)​ A Qx3 cell array containing the optimized matchups

Topics: ​(​structures​), (​cell arrays)​ , (​iteration​)​, (​ ​conditionals)​ ​, (​ ​masking​)

Background:
You’ve been recruited by your local surf shop to optimize the performance of the dogs,
surfers, and board matchups for the dog surfing competition they are hosting in 5 hours.
Because you have so little time, and no actual knowledge on the logistics of dog surfing, you
decide to write a MATLAB function to have the best showing possible to increase the clout of
the surf shop.

Function Description:
Given 3 structure vectors, one representing dogs, one representing humans, and one
representing boards, attempt to match up the dogs with the best matchup of surfers and boards.
The structures will have the following field names holding the accompanying data type:

● Dog
○ Name: ​'name'​ (​char)​
○ Weight: ​'weight'​ (​double)​
○ Size: ​'size'​ (​double)​
○ Special talent: ​'spTalent'​ (​char​)
○ Personality type: ​'pType'​ (​char​)
● Human
○ Name: ​'name'​ (​char)​
○ Weight: ​'weight'​ (​double)​
○ Stance size: ​'stanceSz'​ (​double)​
○ Special talent: ​'spTalent'​ (​char​)
○ Personality type: ​'pType'​ (​char​)
○ Experience level: ​'epLevel'​ (​double)​
● Board
○ Name: ​'name'​ (​char)​
○ Size: ​'size'​ (​double)​
○ Optimal Weight:​ 'optWt'​ (​double)​
Homework 09 - Structures

Dog and Human Match Qualifications​ (any qualification makes a match; ordered by
match priority)
- If a human and a dog have the same name they are a perfect match
- If a human and a dog possess the same personality type ​and special talent they are a
great match
- Dogs with personality type ​'C' ​need their human to have 3-5 years of experience
(inclusive); if so, they are a decent match

Matches to Boards​ (all conditions must be met for a match; one match is guaranteed)
1. The summed weights of the dog and human must be within the following inclusive
bounds: [board optimal weight - 5, board optimal weight]
2. The summed sizes of the dog and human’s stance must not exceed the size of the board

The dogs and people should not be assigned more than once (and may not be assigned
at all) but boards can be assigned more than one time. If a dog has no potential matches, they
will not appear in the output cell array. You should ​assign the dogs in the sequential order they
appear in from the dogs structure array. If there are multiple potential matches per dog, the
match priority should first be considered​; if there are still multiple potential matches after match
priority, ​then assign the match based on the lowest match human index​. Although dogs and
humans can be assigned only once, boards can be assigned multiple times. Once you have the
right combinations of a single dog, human, and board, put them in a cell array according to the
following template:

[{<dog name>}{<human name>}{<board name>};


{<dog name>}{<human name>}{<board name>};
...etc]

Once you have the final matches between dogs, humans, and boards, ​sort the rows of the cell
array so that the dog names appear in alphabetical order​, and output this cell array.

Example:
dogs →
name: 'Milo' 'Buddy' 'Prim' 'Maxie'
weight: 8 15 25 12
size: 10 8 18 8
pType: 'A' 'C' 'B' 'A'
spTalent: 'Sleeping' 'Hiking' 'Barking' 'Sniffing'
Homework 09 - Structures

humans →
name: 'Emma' 'Delaney' 'Maxie' 'Hannah' 'Danny'
epLevel: 7 4 .5 8 14
weight: 170 142 156 130 215
stanceSz: 39 30 20 38 42
spTalent: 'Sleeping 'Hiking' 'Running' 'Sleeping 'Coding'
pType: ' 'C' 'B' ' 'A'
'B' 'A'

boards →

name: 'Xtreme' 'WaveRide 'BoardBoi 'Surf 2k'


size: 68 ' ' 59
optWt: 140 62 60 172
158 195

function [matches] = surfLifeRuffLife(dog,human,board)


matches → [{'Buddy'}, {'Delaney'}, {'WaveRide'};
{'Maxie'}, {'Maxie' }, {'Surf 2k' };
{'Milo' }, {'Hannah' }, {'Xtreme’ }]

Notes:
● Assign the dogs in the sequential order they appear​. The first dog gets assigned first, the
second dog gets assigned second, etc.
● If multiple humans can be assigned to a dog, the match priority should take preference.
Otherwise, assign the lowest indexed potential match as should be the final match.
● You are guaranteed to have only a single valid surfboard match per dog/human match
but surf boards can be used for more than one dog/human match.
Hints:
● Once you assign a human to a dog, deleting the matched human is a good way to
ensure they are not used twice. Be mindful of out of bounds indexing from structure
deletion.
Homework 09 - Structures

Function Name: ​tvGuide

Inputs:
1. (​struct​) 1xN structure array of sports programs

Outputs:
1. (​struct​) 1xM structure array of an ordered TV guide

Topics: ​(​structures​), (​arrays​), (​iteration​)

Background:
You are finally getting to live your dreams. Your favorite part of your favorite movie,
Dodgeball​, is finally entering the real world. ESPN has turned fiction into reality, adding ESPN8:
The Ocho​, home to obscure sports and other non mainstream competitions into its family of
sports channels. You have been hired to help ESPN develop this new channel, so you turn to
your favorite tool, MATLAB, to help you schedule the broadcasts and fill up the TV guide.

Function Description:
You are given a 1xN structure array containing sports programs that will fill up a TV
program guide. The structures will be in no particular order, and will each have the following
fields:

● 'Name'​, containing the name of the program (​char)​


● 'Start'​, containing the program’s start time in the form ​'hh:mm am/pm'​ (​char)​
● 'Runtime'​, containing the program’s runtime in minutes (​double​)

Your job is to create a TV guide by placing the programs in start time order, with the
earliest programs first. Add in the ending time for each program in the field ​'End'​, following the
same ​'hh:mm am/pm' format as the start time. Since the ending times are now included in the
guide, you must also remove the ​'Runtime' field. Make sure to sort the structures in the order
of start time. Finally, you must stretch the TV guide so that it is separated into 30 minute
segments with each program occupying every segment for its complete run time. For example, if
you have a program that starts at 12:30 pm and runs for 90 minutes, your output should have 3
duplicate structures at 12:30-1:00, 1:00-1:30, and 1:30-2:00.

(​continued…​)
Homework 09 - Structures

Example:
progs →

Name: 'Foosball 'Poker'


Start: '00:00 '10:30 am'
Runtime: pm' 90
60

guide = tvGuide(progs);

guide →

Name: 'Poker' 'Poker' 'Poker' 'Foosball' 'Foosball'


Start: '10:30 '10:30 am' '10:30 '00:00 pm' '00:00 pm'
End: am' '00:00 pm' am' '01:00 pm' '01:00 pm'
'00:00 '00:00
pm' pm'

Notes:
● You are guaranteed that each program will start and stop on a 30 minute boundary and
all programs will be contiguous (there won't be any empty slots and no two programs will
on at the same time)
● By definition, if you have two programs, A followed by B, and program A ends at time
XX:YY, program B will also start at time XX:YY (see the example)
● The ​hours follow the same rules as ​timePiece() (HW 02) where the hours will
range from ​00-11
● Your start times will never cross a 24 hour boundary (i.e. you will not have a program
that starts at 11:00 pm followed by one that starts at 1:00 am) however, you may have a
program that starts at 11:00 pm and ends at 1:00 am

Hints:
● You can turn numbers into zero-padded strings by doing ​sprintf('%02.0f', num)
● The r ​ epelem​ function may be useful. Here is a ​quick video​ on how it works.
● Think about how you worked with hours and minutes in ​timePiece() from HW02. If
you don’t think your solution is applicable, look at the solution code on Canvas.
● Helper functions are your friend. If you find yourself saying “I wish there was a function
for that”, then make one (Just make sure the helper functions are in the same file as the
main function).
● How could you tell whether a time flips from am to pm?
Homework 09 - Structures

● Remember that am start times come before pm start times. Think about how you can get
the start time into a value you can sort by.

You might also like