AP Computer Science 4.2 Standard Data Structures
Recommendation
Want a study guide too?
AP Computer Science 4.2 Standard Data Structures. Which implementation will find "k" within the String array alphabet and turn it into "funk"?
AP | AP Computer Science |
AP Computer Science | Standard Data Structures |
Computer Science | AP Computer Science |
Language | English Language |
Object-Oriented Program Design | Program and Class Design |
Test Prep | AP Computer Science |
Transcript
we've got a big ol array where each element is a letter of the alphabet and
we have to dig through it until we find the letter K and turn it into some card [Boy digging up alphabet blocks]
option one starts with a for loop but where typical loop starts with its
iterator at zero and works upwards this one starts with the iterator at some
value alphabet dot length minus one well assuming our alphabet array is 26
elements long that will make I equal to 25 the loop will terminate when I stops
being greater than or equal to zero and subtract one from I each time the loop
cycles well the if statement inside the loop will then check the alphabet array [Finger points to if statement]
at index I see if it equals the letter K most of the time it won't so the code
within the if statement won't get executed but on a slight chance that we [Doctor speaking with patient]
get a positive we create the string fresh and give it a value fun then
change the value of that K we found to be fun plus K funky clever and on looks
like option one will work all right let's try option two creates a slightly [Option 2 loop appears]
more typical loop starting its iterator at zero and working upward until it's no
longer less than the alphabet arrays length well each time the loop cycles [Loop riding a bicycle]
the if statement checks to see if we're working with a K NSO gives us the
strings fresh with the value funky and then uses a substring operation a slice
off the first K and the KY and replace our K with funk so option two should
work to some string operations are a lot like slicing that gross little nub off [Person slices end of banana]
the end of a bananas you know all right so so you've got the springs
banana and you want to get rid of the B at the beginning in the final a well you
could substring it with the indices of one and five to swing the knife there [Knife cuts away letters b and a]
and create a new string well that's exactly what's happening here with funky
being mutilated to create fun in so many senses of the phrase option three takes
it another direction using a while loop well here we've been given the iterator
I with a value of one and while the alphabet element at index I minus 1 is
not K it adds one to the I and loops again so this will go on over and over
and over until we get a K which points a while loop will break and we'll finally
get to move down to changing that array value at index I to funk so a job well
done let's just print our array to the console and we can admire our handiwork [array printing]
so oh oh no we didn't change K we changed L instead we must have
overlooked something and there it is right there in our while loop we're [Man points to while loop]
checking to see if the string at index I minus 1 is K and when it is we alter a
different array position entirely I so know option 3 does not work is given
could we tweak it and fix it sure it would be easy but as the stands now all [Woman tweaking a loop function frame]
the options 1 & 2 don't fake the funk