© A+ Computer Science - www.apluscompsci.com
�
Warm-up: 9/1
Given an integer,
a) How do you access the ones digit?
b) How do you access the tens digit?
c) How do you remove the ones digit of an int?
Example: 458
a) 8
b) 5
c) 458 -> 45
© A+ Computer Science - www.apluscompsci.com
Warm-up #1a
iterative implementation
On a whiteboard with your partner:
Given a non-negative int n, compute the count of the occurrences of 8 as a digit, except that an 8 with another 8 immediately to its left counts double, so 8818 yields 4. Note that mod (%) by 10 yields the rightmost digit (126 % 10 is 6), while divide (/) by 10 removes the rightmost digit (126 / 10 is 12).
count8(8) → 1
count8(818) → 2
count8(8818) → 4
public int count8(int n) {
© A+ Computer Science - www.apluscompsci.com
�
Warm-up #1b
recursive implementation
On a whiteboard with your partner:
Given a non-negative int n, compute recursively (no loops) the count of the occurrences of 8 as a digit, except that an 8 with another 8 immediately to its left counts double, so 8818 yields 4. Note that mod (%) by 10 yields the rightmost digit (126 % 10 is 6), while divide (/) by 10 removes the rightmost digit (126 / 10 is 12).
count8(8) → 1
count8(818) → 2
count8(8818) → 4
public int count8(int n) {
© A+ Computer Science - www.apluscompsci.com
�
CodingBat > recursion-1 > count8
Check your code for both implementations. |
© A+ Computer Science - www.apluscompsci.com
�
charAt String method
What is the output? String str = "Bellaire"; System.out.println(str.charAt(0)); System.out.println(str.charAt(str.length()-1)); System.out.println('b' == str.charAt(0)); System.out.println(str.substring(1)); |
© A+ Computer Science - www.apluscompsci.com
�
charAt String method
String str = "Bellaire"; System.out.println(str.charAt(0)); System.out.println(str.charAt(str.length()-1)); System.out.println('b' == str.charAt(0)); System.out.println(str.substring(1)); Output: B e false ellaire |
© A+ Computer Science - www.apluscompsci.com
�
Warm-up #2a
iterative implementation
Given a string, return a new string where all the 'x' chars have been removed. noX("xaxb") → "ab" noX("abc") → "abc" noX("xx") → "" public String noX(String str) { |
© A+ Computer Science - www.apluscompsci.com
�
Warm-up #2b
recursive implementation
Given a string, compute recursively a new string where all the 'x' chars have been removed. noX("xaxb") → "ab" noX("abc") → "abc" noX("xx") → "" public String noX(String str) { |
© A+ Computer Science - www.apluscompsci.com
�
CodingBat > recursion-1 > noX
Check your code for both implementations. |
10
Share implementations for countX and changeXY.
Yesterday, when we called on our recursive method array11, the initial call was made with index 0 and the parameter was incremented by 1 each subsequent call.
For strings, how do we change the parameter str for subsequent recursive calls after the initial call with str?
Go over homework
Why recursion?
Yesterday, we discussed how the recursive implementation of the fibonacci sequence was much less efficient than the iterative implementation.
Why would we use the recursive implementation then?
Why recursion?
Yesterday, we discussed how the recursive implementation of the fibonacci sequence was much less efficient than the iterative implementation.
Why would we use the recursive implementation then?
© A+ Computer Science - www.apluscompsci.com
�
© A+ Computer Science - www.apluscompsci.com
(aka Tower of Brahma or Lucas’ Tower)
Tower of Hanoi
© A+ Computer Science - www.apluscompsci.com
Recall story about an Indian temple which contains a large room with three time-worn posts in it, surrounded by 64 disks.
Brahmin priests, acting out the command of an ancient prophecy, have been moving these disks in accordance with the immutable rules of Brahma since that time. According to the legend, when the last move of the puzzle is completed, the world will end.
How many years will it take to move 64 discs if each priests can move one disc per second?
�
Tower of Brahma
© A+ Computer Science - www.apluscompsci.com
How many years will it take to move 64 discs if each priests can move one disc per second?
�
Tower of Brahma
© A+ Computer Science - www.apluscompsci.com
How many years will it take to move 64 discs if each priests can move one disc per second?
2^64-1 ~ 6*10^11 years �(about 600 billion years - about 42 times the current age of the Universe)
�
Tower of Brahma
© A+ Computer Science - www.apluscompsci.com
How can we think of this algorithm as recursive?
�
Tower of Brahma
© A+ Computer Science - www.apluscompsci.com
Homework