-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathFancyTribonacci.java
More file actions
250 lines (219 loc) · 6.18 KB
/
FancyTribonacci.java
File metadata and controls
250 lines (219 loc) · 6.18 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import acm.program.ConsoleProgram;
public class FancyTribonacci extends ConsoleProgram {
/* The special symbol that breaks the input stream */
private static final int DELIMITER = -1;
public void run() {
// Receive first 3 elements of the sequence A
int A0 = readNumber("A", 0);
int A1 = readNumber("A", 1);
int A2 = readNumber("A", 2);
// For debugging purposes:
printSequence(A0, A1, A2, 20);
// Read the first element of the sequence B
int B0 = readNumber("B", 1);
// Is the sequence A made of only zeros?
if(A0 == 0 && A1 == 0 && A2 == 0) {
solveZeroSpecialCase(B0);
} else {
solveProblem(A0, A1, A2, B0);
}
}
/**
* Solves the special case when every member of A is 0
* Precondition: A0 = A1 = A2 = 0
*/
private void solveZeroSpecialCase(int currentElement) {
int currentIndex = 1;
boolean isResultPositive = true;
while(true) {
if(currentElement != 0) {
isResultPositive = false;
}
currentElement = readNumber("B", currentIndex);
if(currentElement == DELIMITER) {
break;
}
currentIndex++;
}
if(isResultPositive) {
println("Yes 0");
} else {
println("No");
}
}
/**
* Precondition: A0, A1, A2 are not all zeros
*/
private void solveProblem(int A0, int A1, int A2, int B0) {
int B1 = readNumber("B", 1);
// The sequence B only consists of one element
if(B1 == DELIMITER) {
solveProblemForOneElement(A0, A1, A2, B0);
} else {
int B2 = readNumber("B", 2);
// The sequence B is a pair
if(B2 == DELIMITER) {
solveProblemForPair(A0, A1, A2, B0, B1);
} else {
// The sequence B is at least a triplet
solveExtendedProblem(A0, A1, A2, B0, B1, B2);
}
}
}
/**
* Solves the problem when the length of the sequence B is 1
*/
private void solveProblemForOneElement(int A0, int A1, int A2, int B0) {
int firstIndexOfElement = findFirstIndexOfElement(A0, A1, A2, B0);
if(firstIndexOfElement == -1) {
println("No");
} else {
println("Yes " + firstIndexOfElement);
}
}
/**
* Solves the problem when the length of the sequence B is 2
*/
private void solveProblemForPair(int A0, int A1, int A2, int B0, int B1) {
int firstIndexOfPair = findFirstIndexOfPair(A0, A1, A2, B0, B1);
if(firstIndexOfPair == -1) {
println("No");
} else {
println("Yes " + firstIndexOfPair);
}
}
/**
* Solves the problem when the length of the sequence B >= 3
*/
private void solveExtendedProblem(int A0, int A1, int A2, int B0, int B1, int B2) {
// Now we have B0, B1, B2
int indexOfTriplet = findIndexOfTriplet(A0, A1, A2, B0, B1, B2);
boolean resultIsPositive = true;
if(indexOfTriplet == -1) {
resultIsPositive = false;
}
int nextIndex = 3;
int numberOfElements = 3;
while(true) {
int nextElement = readNumber("B", nextIndex);
if(nextElement == DELIMITER) {
break;
}
int nextElementIndex = findNth(A0, A1, A2, indexOfTriplet + numberOfElements);
if(nextElementIndex != nextElement) {
resultIsPositive = false;
}
numberOfElements++;
nextIndex++;
}
if(resultIsPositive) {
println("Yes " + indexOfTriplet);
} else {
println("No");
}
}
/**
* Reads the number from the user
*/
private int readNumber(String sequenceName, int index) {
return readInt(sequenceName + index + ": ");
}
/**
* Finds the Nth element of the given Tribonacci Sequence
* Precondition: A0, A1 and A2 should be passed as first 3 arguments
* Postcondition: Returns the Nth element of the given sequence
*/
private int findNth(int prevPrev, int prev, int current, int n) {
if(n == 0) {
return prevPrev;
}
if(n == 1) {
return prev;
}
if(n == 2) {
return current;
}
for(int currentIndex = 2; currentIndex < n; currentIndex++) {
int nextElement = prevPrev + prev + current;
prevPrev = prev;
prev = current;
current = nextElement;
}
return current;
}
/**
* Finds where the first occurence of the element is located in the given sequence
* Precondition: len(B) == 1
* Postcondition: Returns -1 if B is not a subsequence of A, otherwise returns n
* that satisfies A_n = B_0
*/
private int findFirstIndexOfElement(int A0, int A1, int A2, int element) {
int currentIndex = 0;
while(true) {
int currentElement = findNth(A0, A1, A2, currentIndex);
if(element == currentElement) {
return currentIndex;
}
if(currentElement > element) {
break;
}
currentIndex++;
}
return -1;
}
/**
* Finds where the first occurence of the pair is located in the given sequence
* Precondition: len(B) == 2
* Postcondition: Returns -1 if B is not a subsequence of A, otherwise returns n
* that satisfies A_n = B_0
*/
private int findFirstIndexOfPair(int A0, int A1, int A2, int firstElement, int secondElement) {
int currentIndex = 0;
while(true) {
int currentElement = findNth(A0, A1, A2, currentIndex);
int nextElement = findNth(A0, A1, A2, currentIndex + 1);
if(firstElement == currentElement && nextElement == secondElement) {
return currentIndex;
}
if(currentElement > secondElement) {
break;
}
currentIndex++;
}
return -1;
}
/**
* Finds where the subsequence B is located in the sequence A
* Precondition: len(B) >= 3
* Postcondition: Returns -1 if B is not a subsequence of A, otherwise returns n
* that satisfies A_n = B_0
*/
private int findIndexOfTriplet(int A0, int A1, int A2, int firstElement, int secondElement, int thirdElement) {
int currentIndex = 0;
while(true) {
int currentElement = findNth(A0, A1, A2, currentIndex);
int nextElement = findNth(A0, A1, A2, currentIndex + 1);
int nextNextElement = findNth(A0, A1, A2, currentIndex + 2);
if(firstElement == currentElement &&
nextElement == secondElement &&
nextNextElement == thirdElement)
{
return currentIndex;
}
if(currentElement > secondElement) {
break;
}
currentIndex++;
}
return -1;
}
/**
* For debugging purposes only, prints first numberOfElements members of A
*/
private void printSequence(int A0, int A1, int A2, int numberOfElements) {
for(int currentIndex = 0; currentIndex < numberOfElements; currentIndex++) {
print(findNth(A0, A1, A2, currentIndex) + " ");
}
println();
}
}