AP Computer Science 4.2 Standard Data Structures

AP Computer Science 4.2 Standard Data Structures. Which implementation will find "k" within the String array alphabet and turn it into "funk"?

APAP Computer Science
AP Computer ScienceStandard Data Structures
Computer ScienceAP Computer Science
LanguageEnglish Language
Object-Oriented Program DesignProgram and Class Design
Test PrepAP Computer Science

Transcript

00:32

we've got a big ol array where each element is a letter of the alphabet and

00:36

we have to dig through it until we find the letter K and turn it into some card [Boy digging up alphabet blocks]

00:41

option one starts with a for loop but where typical loop starts with its

00:45

iterator at zero and works upwards this one starts with the iterator at some

00:49

value alphabet dot length minus one well assuming our alphabet array is 26

00:55

elements long that will make I equal to 25 the loop will terminate when I stops

00:59

being greater than or equal to zero and subtract one from I each time the loop

01:03

cycles well the if statement inside the loop will then check the alphabet array [Finger points to if statement]

01:07

at index I see if it equals the letter K most of the time it won't so the code

01:12

within the if statement won't get executed but on a slight chance that we [Doctor speaking with patient]

01:16

get a positive we create the string fresh and give it a value fun then

01:22

change the value of that K we found to be fun plus K funky clever and on looks

01:27

like option one will work all right let's try option two creates a slightly [Option 2 loop appears]

01:31

more typical loop starting its iterator at zero and working upward until it's no

01:34

longer less than the alphabet arrays length well each time the loop cycles [Loop riding a bicycle]

01:38

the if statement checks to see if we're working with a K NSO gives us the

01:42

strings fresh with the value funky and then uses a substring operation a slice

01:48

off the first K and the KY and replace our K with funk so option two should

01:54

work to some string operations are a lot like slicing that gross little nub off [Person slices end of banana]

01:59

the end of a bananas you know all right so so you've got the springs

02:03

banana and you want to get rid of the B at the beginning in the final a well you

02:07

could substring it with the indices of one and five to swing the knife there [Knife cuts away letters b and a]

02:11

and create a new string well that's exactly what's happening here with funky

02:15

being mutilated to create fun in so many senses of the phrase option three takes

02:20

it another direction using a while loop well here we've been given the iterator

02:26

I with a value of one and while the alphabet element at index I minus 1 is

02:31

not K it adds one to the I and loops again so this will go on over and over

02:37

and over until we get a K which points a while loop will break and we'll finally

02:41

get to move down to changing that array value at index I to funk so a job well

02:45

done let's just print our array to the console and we can admire our handiwork [array printing]

02:50

so oh oh no we didn't change K we changed L instead we must have

02:56

overlooked something and there it is right there in our while loop we're [Man points to while loop]

02:59

checking to see if the string at index I minus 1 is K and when it is we alter a

03:05

different array position entirely I so know option 3 does not work is given

03:11

could we tweak it and fix it sure it would be easy but as the stands now all [Woman tweaking a loop function frame]

03:15

the options 1 & 2 don't fake the funk