CS 46B - Lecture 8

Cover page image

Pre-class reading

Fibonacci Sequence

The Efficiency of Recursion

Call Tree for Computing fib(6)

The Efficiency of Recursion

The Efficiency of Recursion

Iterative isPalindrome Method

public boolean isPalindrome() 
{ 
   int start = 0; 
   int end = text.length() - 1; 
   while (start < end) 
   { 
      char first = Character.toLowerCase(text.charAt(start)); 
      char last = Character.toLowerCase(text.charAt(end)); 
      if (Character.isLetter(first) && Character.isLetter(last)) 
      { 
         // Both are letters. 
         if (first == last) 
         { 
            start++; 
            end--; 
         } 
         else 
         { 
            return false; 
         } 
      } 
      if (!Character.isLetter(last)) { end--; } 
      if (!Character.isLetter(first)) { start++; } 
   } 
   return true; 
}

Lecture 8 Clicker Question 1

Consider the triangle number computation in the Triangle.getArea method.

Permutations

To Generate All Permutations

To Generate All Permutations

To Generate All Permutations

Lecture 8 Clicker Question 2

What are all permutations of the four-letter word beat?

  1. beat,  eatb, atbe, tbea
  2. beat, beta, baet, bate, btea, btae
  3. beat, beta, baet, bate, btea, btae, eatb, etab, aetb, ateb, teab, taeb
  4. b followed by the six permutations of eat, e followed by the six permutations of bat, a followed by the six permutations of bet, and t followed by the six permutations of bea

Lecture 8 Clicker Question 3

Permutations, shmermutations. Let's talk about subsequences instead. In a subsequence, you don't use all the letters, but you must use them in the same order in which they appear in the original. For example, "brat" -> [, a, at, b, ba, bat, br, bra, brat, brt, bt, r, ra, rat, rt, t]

If the word has n letters, how many subsequences are there?

  1. n
  2. n!
  3. n2
  4. 2n

Lecture 8 Clicker Question 4

Now we are supposed to find all subsequences of the word turn. And let's say that by good fortune we already know all subsequences of the word urn.

What do we do?

  1. For each of the subsequences of urn, put t in front
  2. For each of the subsequences of urn, put t in all possible positions. For example, if we have un, produce tun, utn, and unt.
  3. For each of the subsequences of urn, either put t in front or don't.
  4. That's no good. We also need the subsequences of tur, tun, and trn.

Lecture 8 Clicker Question 5

We now have some pseudocode:

subsequences(word)
   result = empty list
   first = first character in word
   subs = subsequences(word without the first character)
   for each s in subs
      add first + s and s to result
   return result

But we still need a base case so that the recursion can come to an end.

What is the minimal base case that we can use?

  1. If word has only one character, return a list containing word
  2. If word has only one character, return a list containing word and the empty string
  3. If word is empty , return an empty list
  4. If word is empty , return a list containing the empty string

Lecture 8 Clicker Question 6

Now it's your turn. Code it up here.

What do you get for subsequence #148?

  1. San Jos
  2. SaJé
  3. anoé
  4. Something else

Before Next Class