-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLetters.java
136 lines (124 loc) · 3.08 KB
/
Letters.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
* Recursion Assignment
* @author
*
* Remove the NotImplemented line from any methods you write
* DO NOT change the method signatures
* You MAY write helper classes
*/
public class Letters {
/*
* #L1 Palindrome (2pt)
* recursion: are the first and last letters the same?
* base case: is the word length <=2?
*/
static boolean isPalindrome(char[] arr) {
throw new NotImplemented();
}
/*
* #L2 Reverse (2pt)
* R(a, i) = a (if i >= |a|/2)
* R(a, i) = R(b, i+1)
* b has the ith and |a|-ith letters swapped
*/
static void reverse(char[] arr) {
throw new NotImplemented();
}
/*
* #L3 Word count (2pt)
* L(a) = 1 (if |a| = 0)
* L(a) = 1 + L(a[1:]) (if a[0] = ' ')
* L(a) = L(a[1:])
*
* you may use the slice function
*/
static int wordCount(char[] arr) {
throw new NotImplemented();
}
/*
* #L4 Capital count (2pt)
* L(a) = 0 (if |a| = 0)
* L(a) = 1 + L(a[1:]) (if a[0] is capital)
* L(a) = L(a[1:])
*
* you may use the slice function
*/
static int capitalCount(char[] arr) {
throw new NotImplemented();
}
/*
* #L5 Letter count (2pt)
* L(a,c) = 0 (if |a| = 0)
* L(a,c) = 1 + L(a[1:], c) (if a[0] = c)
* L(a,c) = L(a[1:], c)
*
* you may use the slice function
*/
static int letterCount(char[] arr, char letter) {
throw new NotImplemented();
}
/*
* #L6 Contains (3pt)
* C(a,s) = false (if |a| = 0)
* C(a,s) = true (if |s| = 0)
* C(a,s) = C(a[1:],s[1:]) || C(a[1:], s) (if a[0] = s[0])
* C(a,s) = C(a[1:],s)
*
* you may use the slice function
*/
static boolean contains(char[] arr, char[] sub) {
throw new NotImplemented();
}
/*
* #L7 Replace (3pt)
* R(a,f,t) = empty (if |a| = 0)
* R(a,f,t) = t JOIN R(a[1:], f, t) (if a[0] = f)
* R(a,f,t) = a[0] JOIN R(a[1:], f, t)
*
* you will need to write a JOIN function
* you may use the slice function
*/
static void replace(char[] arr, char from, char to) {
throw new NotImplemented();
}
/*
* #L8 LetterFrequency (3pt)
* L(a,f,i) ends if i=0, and all values in F divided by |a|
* L(a,f,i) = L(a[1:], f, i+1) and f at letter a[0] increments
* (i starts at |a|)
*/
static void letterFrequency(char[] arr, double[] freq) {
throw new NotImplemented();
}
/**
* Slice an array
* @param a the array to slice
* @param i the starting place
* @param j the ending place
* @return a new array which is a subset of a from i to j
*/
static char[] slice(char[] a, int i, int j) {
char[] ret = new char[j-i];
for(int k = i; k<j; k++) {
ret[k-i] = a[k];
}
return ret;
}
/**
* Slice an array
* @param a the array to slice
* @param i the starting place
* @return a new array which is a subset of a from i to the end
*/
static char[] slice(char[] a, int i) {
return slice(a, i, a.length);
}
/**
* Slice an array
* @param a the array to slice
* @return a new array which is a subset that removes the first item
*/
static char[] slice(char[] a) {
return slice(a, 1, a.length);
}
}