Skip to content

Commit 4ce6193

Browse files
committed
ch08 revisions
1 parent 69c58c2 commit 4ce6193

File tree

3 files changed

+34
-30
lines changed

3 files changed

+34
-30
lines changed

ch08/CodingBat.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,35 @@
33
*/
44
public class CodingBat {
55

6-
public static String noX(String str) {
6+
/**
7+
* See https://codingbat.com/prob/p118230.
8+
*/
9+
public String noX(String str) {
710
if (str.length() == 0) {
811
return "";
912
}
10-
char c = str.charAt(0);
11-
if (c == 'x') {
12-
return noX(str.substring(1));
13+
char first = str.charAt(0);
14+
String rest = str.substring(1);
15+
String recurse = noX(rest);
16+
if (first == 'x') {
17+
return recurse;
1318
} else {
14-
return c + noX(str.substring(1));
19+
return first + recurse;
1520
}
1621
}
1722

23+
/**
24+
* See https://codingbat.com/prob/p135988.
25+
*/
1826
public int array11(int[] nums, int index) {
1927
if (index >= nums.length) {
2028
return 0;
2129
}
30+
int recurse = array11(nums, index + 1);
2231
if (nums[index] == 11) {
23-
return 1 + array11(nums, index + 1);
32+
return recurse + 1;
2433
} else {
25-
return array11(nums, index + 1);
34+
return recurse;
2635
}
2736
}
2837

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,4 @@
1-
public class Recursion {
2-
3-
public static void main(String[] args) {
4-
System.out.println("countdown");
5-
countdown(3);
6-
7-
System.out.println("nLines");
8-
nLines(3);
9-
10-
System.out.println("countup");
11-
countup(3);
12-
13-
System.out.println("displayBinary");
14-
displayBinary(23);
15-
System.out.println();
16-
}
1+
public class Examples {
172

183
public static void countdown(int n) {
194
if (n == 0) {
@@ -24,10 +9,6 @@ public static void countdown(int n) {
249
}
2510
}
2611

27-
public static void newLine() {
28-
System.out.println();
29-
}
30-
3112
public static void nLines(int n) {
3213
if (n > 0) {
3314
System.out.println();
@@ -56,4 +37,21 @@ public static void displayBinary(int value) {
5637
}
5738
}
5839

40+
public static void main(String[] args) {
41+
System.out.println("countdown");
42+
countdown(3);
43+
44+
System.out.println("nLines");
45+
nLines(3);
46+
47+
// forever("Ha!");
48+
49+
System.out.println("countup");
50+
countup(3);
51+
52+
System.out.println("displayBinary");
53+
displayBinary(23);
54+
System.out.println();
55+
}
56+
5957
}

ch08/Series.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
/**
2-
* Examples from Chapter 8.
3-
*/
41
public class Series {
52

63
public static int factorial(int n) {

0 commit comments

Comments
 (0)