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

Homework 3  asks you to provide all subsequences of a word, in the same order in which they appear. 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