-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
159 lines (145 loc) · 4.75 KB
/
Main.java
File metadata and controls
159 lines (145 loc) · 4.75 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package recursion;
public class Main {
public static int fact(int num){
if (num==1)
return 1;
return num*fact(--num);
}
public static int fab(int num){
if (num<=2)
return 1;
return fab(num-1) + fab(num - 2);
}
public static int sum(int num){
if (num==0)
return 0;
return num+sum(num-1);
}
public static int power(int base,int os){
if (os==1)
return base;
return base*power(base,os-1);
}
public static int fastPower(int base,int os){
if (os==1)
return base;
if (os%2==0)
return fastPower(base*base,os/2);
else
return base*fastPower(base,os-1);
}
public static void inverseString(String word ){
if (word != null && word.length() != 0) {
inverseString(word.substring(1));
System.out.println(word.charAt(0));
}
}
public static int binSearch(int []arr,int left,int right,int value){
int mid;
if (right>=left){
mid=(right+left)/2;
if (value > arr[mid])
return binSearch(arr,mid+1,right,value);
else if(value < arr[mid])
return binSearch(arr,left,mid-1,value);
else
return 1;
}
return 0;
}
public static void towerOfHanoi(int n,char fromRod,char toRod,char tempRod){
if (n==1) {
System.out.println("you must move desk 1 from " + fromRod + " to " + toRod);
} else {
towerOfHanoi((n-1),fromRod,tempRod,toRod);
System.out.println("move dest "+n+" from "+fromRod+" to "+toRod);
towerOfHanoi((n-1),tempRod,toRod,fromRod);
}
}
public static void main(String[] args) {
towerOfHanoi(3,'a','b','c');
System.out.println(fab(9));
System.out.println(fact(5));
System.out.println(sum(3));
System.out.println(power(2,3));
System.out.println(fastPower(2,5));
inverseString("mohamed") ;
int [ ]arr={2,3,5,11,22,33};
System.out.println(myb(arr, 0, 5, 0));
System.out.println(parenBit("abc(ans)def"));
System.out.println(stringClean("yyyzzaam"));
System.out.println(countHi("iam mo hi pro hiandxhitow"));
System.out.println(stringDist("catcowcat","cow"));
}
public static int myb(int []arr,int low,int high,int value){
int mid;
if (high >= low) {
mid=(low+high)/2;
if (arr[mid]<value)
return myb(arr,(mid+1),high,value);
else if (arr[mid]>value)
return myb(arr,low,(mid-1),value);
else
return 1;
}
return 0;
}
// Examples ...
public static String parenBit(String word){ // "abc(ans)def" -> "(ans)"
if (word.charAt(0) != '(')
return parenBit(word.substring(1));
if (word.charAt(word.length()-1)!=')')
return parenBit(word.substring(0,word.length()-1));
return word;
// return word.substring(word.indexOf('('),(word.lastIndexOf(')')+1));
}
public static int bunnyEar(int num){
if (num==0)
return 0;
else
return 2+bunnyEar(num-1 );
}
public static int bunnyEar2(int num){
if (num==0)
return 0;
else if (num%2==0)
return 3+bunnyEar2(num-1 );
else
return 2+bunnyEar2(num-1);
}
public static String nox(String word) {
if (word.length()==0)
return "";
if (word.charAt(0)=='x')
return nox(word.substring(1));
return word.charAt(0)+nox(word.substring(1));
}
public static String stringClean(String word){
if (word.length()==1)
return word;
if (word.charAt(0)==word.charAt(1))
return stringClean(word.substring(1));
return word.charAt(0)+stringClean(word.substring(1));
}
public static int countHi(String word){
if (word.length()==1)
return 0 ;
if ((word.charAt(0) == 'h'&& word.charAt(1)=='i')){
return 1+countHi(word.substring(2));
}
if (word.charAt(0)=='x')
return countHi(word.substring(2));
return countHi(word.substring(1));
}
public static int stringDist(String word,String test){// "catcowcat" , "cat" -> 9
if (word.length()==0)
return 0;
if (word.charAt(0)!= test.charAt(0))
return stringDist(word.substring(1),test);
if (word.endsWith(test)){
return word.length();
}else
return stringDist(word.substring(0,(word.length()-1)),test);
// complete video at min 29:00 .
}
}