Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

isAnBn(w) { if (w is empty string) return true; else if (w begins w/ the charact

ID: 441941 • Letter: I

Question

isAnBn(w) { if (w is empty string) return true; else if (w begins w/ the character A and ends w/ the character B) return isAnBn(w minus its first and last characters); else return false; }

Explanation / Answer

isAnBn(w) We check to see if the word passed in to the function is an empty string.... if it is an empty string we return true. If the word isn't empty then we proceed by checking to see if the first character is an A and the last character is a B. If the first character is an a AND the last character is a b then we chop those characters off the existing word and pass the newly shortened string sequence back into the recursive function and repeat the process. lastly we return false as if we reach the end of the code then we haven't encountered the conditions that made this function return true. SO TRACING THE EXACT AAAB being passed into your function ----> isAnBn(AAAB) 1. is it empty string no keep going 2. is the first character an A last a B ? yes .... chop the first A and last B from the sequence and pass the shortened string sequence AA back into the function (this is the recursive step) 1. is it an empty string ? no 2. Is the first character an A and last B ? NO first is A however last is also A hence we return false .... cheers _/