-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecursion.java
120 lines (112 loc) · 2.84 KB
/
Recursion.java
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
/*
* Chapter 8.3
*
* Connected to this class is a Test2 Class in the
* HowSubstringsWork folder!
*/
import java.util.Scanner;
//import java.util.*;
public class Recursion
{
public static void main(String[] args)
{
System.out.println("Pick a number to factorial!");
Scanner in = new Scanner(System.in);
int userNum = in.nextInt();
System.out.println(factorial(userNum) + "\n");
System.out.println("Type in any number to see it in Binary!");
int userBin = in.nextInt();
binary(userBin);
System.out.println();
in.nextLine();
System.out.println("Please provide a String!");
String userStr = in.nextLine();
String userLow = userStr.toLowerCase();
System.out.println(noX(userLow));
}
//"stub"
/*public static int factorial(int n)
{
return 0;
}*/
public static int factorial(int n)
{
if(n == 0)
return 1;
int sum = n*factorial(n-1);
return sum;
//return n*factorial(n-1);
}
public static void binary(int num)
{
//(1)WRONG includes the base case BUT prints normal
//Because the f(n) is done before everything but printed after!
//(1)int numToBinary = num % 2;
if(num != 0)
{
//(2)WRONG prints correctly just backwards because
//it prints before the recursion
//(2)System.out.print(num%2);
binary(num/2);
System.out.print(num%2);
}
//(1)System.out.print(numToBinary);
//return numToBinary;
/*
int numToBinary = 0;
int mod = 0;
//bad thinking because dividing will bring any
//number to 1 or 2 NOT 0 or 1 and that last 1 or 0
//division is important for the one's place!!!
if(num == 0)
return 0;
else if(num == 1)
return 1;
else
{
mod = num % 2;
System.out.println(mod);
numToBinary = binary(num/2);
System.out.println(mod + "\n\n");
System.out.println(mod);
}
return numToBinary;*/
}
public static String noX(String sent)
{
if(sent.length() == 0)
return "";
//Why can't I use this in the return and why can it be used
//for loop without being initialized 1st??????
String noX;
String abNoX = "";
for(int i = 0; i < sent.length(); i++)
{
if(sent.charAt(i) == 0 && sent.charAt(i) == 'x')
sent.substring(i+1);
if(sent.charAt(i) == 'x')
{
//System.out.println(sent);
String notX = sent.substring(i+1);
//System.out.println(notX);
noX = sent.substring(0, i) + notX;
//Why does this print out the answer and not our return?
System.out.println(noX);
abNoX = noX(noX);
}
}
//System.out.println(noX); ?
return abNoX;
}
//How many times the No. 11 appears in an array
public static int array11(int[] nums, int index)
{
if(index >= nums.length)
return 0;
int recurse = array11(nums, index+1);
if(nums[index] == 11)
return (recurse + 1);
else
return recurse;
}
}