-
Notifications
You must be signed in to change notification settings - Fork 4
/
recursion.txt
53 lines (42 loc) · 1.01 KB
/
recursion.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
A -- plain english
1. base case: when number matches target
2. induction step: calculate lb, up & call function with lb, ub
B -- pseudo code
lb = 1
ub = 100
target = rand(lb, ub)
guess_number(lb, ub) {
number = input(lb, ub)
// base case
if (number == target) {
// done
return
} else {
// induction step
if (number < target) {
ub = lb + (ub - lb)/2
} else {
lb = lb + (ub - lb)/2
}
guess_number(lb, ub)
}
}
C -- write code! (leave as an exercise to my lovely students)
---
A. -- plain English
1. base case: when left == right || char(left) != char(right)
2. induction step: calculate left and right, call fn with left & right
B. -- pseudo code
palindrone(left, right) {
// base case
if (left == right || char_at(left) != char_at(right) {
// done (with appropriate message)
return
} else {
// induction step
left++; right--;
palidrone(left,right)
}
}
-- anagrams
http://www.toves.org/books/java/ch18-recurex/