-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursionTest.java
More file actions
120 lines (94 loc) · 3.17 KB
/
RecursionTest.java
File metadata and controls
120 lines (94 loc) · 3.17 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* this class demos recursive methods.
* @ Author: Maricel Vicente
*/
public class RecursionTest
{
/*
* This function calcuated bunny years.
* We have a number of bunnies and each bunny has two big floppy ears.
* We want to compute the total number of ears across all the bunnies recursively
* (without loops or multiplication).
*
*Example input/output:
*bunnyEars(0) → 0
*bunnyEars(1) → 2
*bunnyEars(2) → 4
*
*/
public static int bunnyEars(int bunnies)
{
//COMPLETE THIS
// Base case: if bunnies==0, just return 0.
if (bunnies == 0) {
return 0;
}
// Recursive case: otherwise, make a recursive call with bunnies-1
// (towards the base case), and fix up what it returns.
else {
return bunnies * bunnyEars(bunnies-1);
}
}
/*
* This method is a recursive method
*
*Example input/output:
*countA("anastasia") → 4
*countA("bob") → 0
*countA("") → 0
*
*/
public static int countA(String str)
{
//COMPLETE THIS
// Base case -- return simple answer
if (str.length() == 0) {
return 0;
}
// Deal with the very front of the string (index 0) -- count "A" there.
int count = 0;
if (str.substring(0, 1).equals("A")) {
count = 1;
}
return count + countA(str.substring(1));
// Make a recursive call to deal with the rest of string (the part beyond the front).
// Add count to whatever the recursive call returns to make the final answer.
// Note that str.substring(1) starts with char 1 and goes to the
// end of the string.
}
/*
* The fibonacci sequence is a famous bit of mathematics, and it happens to have
* a recursive definition. The first two values in the sequence are 0 and 1
* (essentially 2 base cases). Each subsequent value is the sum of the previous two
* values, so the whole sequence is: 0, 1, 1, 2, 3, 5, 8, 13, 21 and so on.
* Define a recursive fibonacci(n) method that returns the nth fibonacci number,
* with n=0 representing the start of the sequence.
*
* fibonacci(0) → 0
* fibonacci(1) → 1
* fibonacci(2) → 1
*
*/
public static int fibonacci(int n)
{
//COMPLETE THIS
// No direction provided. Think and come up with a solution.
if (n <= 1) {
return n;
}
else {
return fibonacci(n-1) + fibonacci(n-2);
}
}
public static void main(String[] args)
{
//uncomment the following to test bunnyEars() method
// System.out.println("bunnyEars(10) = " + bunnyEars(10)); // output should be 20
//uncomment the following to test countA() method
//System.out.println("countA(anastasia)" + " = " + countA("anastasia")); // output should be 4
//System.out.println("countA()" + " = " + countA("")); // output should be 0
//uncomment the following to test fibonacci() method
//System.out.println("fibonacci(2) = " + fibonacci(2)); // output should be 1
//System.out.println("fibonacci(9) = " + fibonacci(9)); // output should be 34
}
}