File tree Expand file tree Collapse file tree 3 files changed +34
-30
lines changed
Expand file tree Collapse file tree 3 files changed +34
-30
lines changed Original file line number Diff line number Diff line change 33 */
44public 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
Original file line number Diff line number Diff line change 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}
Original file line number Diff line number Diff line change 1- /**
2- * Examples from Chapter 8.
3- */
41public class Series {
52
63 public static int factorial (int n ) {
You can’t perform that action at this time.
0 commit comments