-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWarmUp_Solution_One.java
536 lines (437 loc) · 15.4 KB
/
WarmUp_Solution_One.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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
package coding.bat.solutions;
public class WarmUp_Solution_One {
public static void main(String[] args) {
}
// --------------------------------------------------------------------------------------------
// The parameter weekday is true if it is a weekday, and the parameter vacation
// is true if we are on vacation. We sleep in if it is not a weekday or we're on
// vacation. Return true if we sleep in.
// sleepIn(false, false) → true
// sleepIn(true, false) → false
// sleepIn(false, true) → true
public boolean sleepIn(boolean weekday, boolean vacation) {
return (!weekday || vacation);
}
// --------------------------------------------------------------------------------------------
// We have two monkeys, a and b, and the parameters aSmile and bSmile indicate
// if each is smiling.
// We are in trouble if they are both smiling or if neither of them is smiling.
// Return true if we are in trouble.
// monkeyTrouble(true, true) → true
// monkeyTrouble(false, false) → true
// monkeyTrouble(true, false) → false
public boolean monkeyTrouble(boolean aSmile, boolean bSmile) {
return (aSmile == bSmile);
}
// --------------------------------------------------------------------------------------------
// Given an int n, return the absolute difference between n and 21,
// except return double the absolute difference if n is over 21.
// diff21(19) → 2
// diff21(10) → 11
// diff21(21) → 0
public int diff21(int n) {
if (n <= 21) {
return 21 - n;
} else {
return (n - 21) * 2;
}
}
// --------------------------------------------------------------------------------------------
// We have a loud talking parrot.
// The "hour" parameter is the current hour time in the range 0..23.
// We are in trouble if the parrot is talking and the hour is before 7 or after
// 20.
// Return true if we are in trouble.
//
//
// parrotTrouble(true, 6) → true
// parrotTrouble(true, 7) → false
// parrotTrouble(false, 6) → false
public boolean parrotTrouble(boolean talking, int hour) {
return (talking && (hour < 7 || hour > 20));
}
// --------------------------------------------------------------------------------------------
// Given 2 ints, a and b, return true if one if them is 10 or if their sum is
// 10.
//
//
// makes10(9, 10) → true
// makes10(9, 9) → false
// makes10(1, 9) → true
public boolean makes10(int a, int b) {
if ((a == 10) || (b == 10) || ((a + b) == 10)) {
return true;
}
return false;
}
// --------------------------------------------------------------------------------------------
// Given an int n, return true if it is within 10 of 100 or 200. Note:
// Math.abs(num) computes the absolute value of a number.
//
//
// nearHundred(93) → true
// nearHundred(90) → true
// nearHundred(89) → false
public boolean nearHundred(int n) {
return ((Math.abs(100 - n) <= 10) || (Math.abs(200 - n) <= 10));
}
// --------------------------------------------------------------------------------------------
// Given 2 int values, return true if one is negative and one is positive.
// Except if the parameter "negative" is true, then return true only if both are
// negative.
//
//
// posNeg(1, -1, false) → true
// posNeg(-1, 1, false) → true
// posNeg(-4, -5, true) → true
public boolean posNeg(int a, int b, boolean negative) {
if (negative) {
return (a < 0 && b < 0);
} else {
return ((a < 0 && b > 0) || (a > 0 && b < 0));
}
}
// --------------------------------------------------------------------------------------------
// Given a string, return a new string where "not " has been added to the front.
// However, if the string already begins with "not", return the string
// unchanged. Note: use .equals() to compare 2 strings.
//
//
// notString("candy") → "not candy"
// notString("x") → "not x"
// notString("not bad") → "not bad"
//
public String notString(String str) {
if (str.length() >= 3 && str.substring(0, 3).equals("not")) {
return str;
}
return "not " + str;
}
// --------------------------------------------------------------------------------------------
// Given a non-empty string and an int n, return a new string where the char at
// index n has been removed. The value of n will be a valid index of a char in
// the original string (i.e. n will be in the range 0..str.length()-1
// inclusive).
//
//
// missingChar("kitten", 1) → "ktten"
// missingChar("kitten", 0) → "itten"
// missingChar("kitten", 4) → "kittn"
public String missingChar(String str, int n) {
String front = str.substring(0, n);
String back = str.substring(n + 1, str.length());
return front + back;
}
// --------------------------------------------------------------------------------------------
// Given a string, return a new string where the first and last chars have been
// exchanged.
//
//
// frontBack("code") → "eodc"
// frontBack("a") → "a"
// frontBack("ab") → "ba"
public String frontBack(String str) {
if (str.length() >= 2) {
String mMiddlePart = str.substring(1, str.length() - 1);
return str.charAt(str.length() - 1) + mMiddlePart + str.charAt(0);
} else {
return str;
}
}
// --------------------------------------------------------------------------------------------
// Given a string, we'll say that the front is the first 3 chars of the string.
// If the string length is less than 3, the front is whatever is there. Return a
// new string which is 3 copies of the front.
//
//
// front3("Java") → "JavJavJav"
// front3("Chocolate") → "ChoChoCho"
// front3("abc") → "abcabcabc"
//
public String front3(String str) {
if (str.length() >= 3) {
String mFirstThree = str.substring(0, 3);
return mFirstThree + mFirstThree + mFirstThree;
} else {
return str + str + str;
}
}
// --------------------------------------------------------------------------------------------
// Given a string, take the last char and return a new string with the last char
// added at the front and back, so "cat" yields "tcatt". The original string
// will be length 1 or more.
//
//
// backAround("cat") → "tcatt"
// backAround("Hello") → "oHelloo"
// backAround("a") → "aaa"
public String backAround(String str) {
int length = str.length();
String mLastChar = str.substring(length - 1);
return mLastChar + str + mLastChar;
}
// --------------------------------------------------------------------------------------------
// Return true if the given non-negative number is a multiple of 3 or a multiple
// of 5. Use the % "mod" operator -- see Introduction to Mod
//
//
// or35(3) → true
// or35(10) → true
// or35(8) → false
public boolean or35(int n) {
return ((n % 3 == 0) || (n % 5 == 0));
}
// --------------------------------------------------------------------------------------------
// Given a string, take the first 2 chars and return the string with the 2 chars
// added at both the front and back, so "kitten" yields"kikittenki". If the
// string length is less than 2, use whatever chars are there.
//
//
// front22("kitten") → "kikittenki"
// front22("Ha") → "HaHaHa"
// front22("abc") → "ababcab"
public String front22(String str) {
if (str.length() >= 2) {
String mFirstTwoChar = str.substring(0, 2);
return mFirstTwoChar + str + mFirstTwoChar;
} else {
return str + str + str;
}
}
// --------------------------------------------------------------------------------------------
// Given a string, return true if the string starts with "hi" and false
// otherwise.
//
//
// startHi("hi there") → true
// startHi("hi") → true
// startHi("hello hi") → false
public boolean startHi(String str) {
if (str.length() >= 2) {
String mFirstTwoChar = str.substring(0, 2);
return mFirstTwoChar.equals("hi");
} else {
return false;
}
}
// --------------------------------------------------------------------------------------------
// Given two temperatures, return true if one is less than 0 and the other is
// greater than 100.
//
//
// icyHot(120, -1) → true
// icyHot(-1, 120) → true
// icyHot(2, 120) → false
public boolean icyHot(int temp1, int temp2) {
return ((temp1 < 0 && temp2 > 100) || (temp2 < 0 && temp1 > 100));
}
// --------------------------------------------------------------------------------------------
// Given 2 int values, return true if either of them is in the range 10..20
// inclusive.
//
//
// in1020(12, 99) → true
// in1020(21, 12) → true
// in1020(8, 99) → false
public boolean in1020(int a, int b) {
return ((a >= 10 && a <= 20) || (b >= 10 && b <= 20));
}
// --------------------------------------------------------------------------------------------
// We'll say that a number is "teen" if it is in the range 13..19 inclusive.
// Given 3 int values, return true if 1 or more of them are teen.
//
//
// hasTeen(13, 20, 10) → true
// hasTeen(20, 19, 10) → true
// hasTeen(20, 10, 13) → true
public boolean hasTeen(int a, int b, int c) {
return (a >= 13 && a <= 19) || (b >= 13 && b <= 19) || (c >= 13 && c <= 19);
}
// --------------------------------------------------------------------------------------------
// We'll say that a number is "teen" if it is in the range 13..19 inclusive.
// Given 2 int values, return true if one or the other is teen, but not both.
//
//
// loneTeen(13, 99) → true
// loneTeen(21, 19) → true
// loneTeen(13, 13) → false
public boolean loneTeen(int a, int b) {
boolean aTeen = (a >= 13 && a <= 19);
boolean bTeen = (b >= 13 && b <= 19);
return (aTeen && !bTeen) || (!aTeen && bTeen);
}
// --------------------------------------------------------------------------------------------
// Given a string, if the string "del" appears starting at index 1, return a
// string where that "del" has been deleted. Otherwise, return the string
// unchanged.
//
//
// delDel("adelbc") → "abc"
// delDel("adelHello") → "aHello"
// delDel("adedbc") → "adedbc"
public String delDel(String str) {
// String mSub = str.substring(1,4);
if (str.length() >= 4 && str.substring(1, 4).equals("del")) {
return str.substring(0, 1) + str.substring(4, str.length());
} else {
return str;
}
}
// --------------------------------------------------------------------------------------------
// Return true if the given string begins with "mix", except the 'm' can be
// anything, so "pix", "9ix" .. all count.
//
//
// mixStart("mix snacks") → true
// mixStart("pix snacks") → true
// mixStart("piz snacks") → false
//
public boolean mixStart(String str) {
if (str.length() >= 3 && str.substring(1, 3).equals("ix"))
return true;
return false;
}
// --------------------------------------------------------------------------------------------
// Given a string, return a string made of the first 2 chars (if present),
// however include first char only if it is 'o' and include the second only if
// it is 'z', so "ozymandias" yields "oz".
//
//
// startOz("ozymandias") → "oz"
// startOz("bzoo") → "z"
// startOz("oxx") → "o"
public String startOz(String str) {
if (str.equals(""))
return str;
else if (str.length() >= 2 && str.substring(0, 2).equals("oz"))
return "oz";
else if (str.substring(0, 1).equals("o"))
return "o";
else if (str.substring(1, 2).equals("z"))
return "z";
return "";
}
// --------------------------------------------------------------------------------------------
// Given three int values, a b c, return the largest.
//
//
// intMax(1, 2, 3) → 3
// intMax(1, 3, 2) → 3
// intMax(3, 2, 1) → 3
public int intMax(int a, int b, int c) {
if ((a > b) && (a > c))
return a;
else if ((b > a) && (b > c))
return b;
return c;
}
// --------------------------------------------------------------------------------------------
// Given 2 int values, return whichever value is nearest to the value 10, or
// return 0 in the event of a tie. Note that Math.abs(n) returns the absolute
// value of a number.
//
//
// close10(8, 13) → 8
// close10(13, 8) → 8
// close10(13, 7) → 0
public int close10(int a, int b) {
int aDiff = Math.abs(a - 10);
int bDiff = Math.abs(b - 10);
if (aDiff < bDiff)
return a;
else if (bDiff < aDiff)
return b;
return 0;
}
// --------------------------------------------------------------------------------------------
// Given 2 int values, return true if they are both in the range 30..40
// inclusive, or they are both in the range 40..50 inclusive.
//
//
// in3050(30, 31) → true
// in3050(30, 41) → false
// in3050(40, 50) → true
public boolean in3050(int a, int b) {
return (a >= 30 && a <= 40 && b >= 30 && b <= 40) || (a >= 40 && a <= 50 && b >= 40 && b <= 50);
}
// --------------------------------------------------------------------------------------------
// Given 2 positive int values, return the larger value that is in the range
// 10..20 inclusive, or return 0 if neither is in that range.
//
//
// max1020(11, 19) → 19
// max1020(19, 11) → 19
// max1020(11, 9) → 11
public int max1020(int a, int b) {
if (b > a) {
int temp = a;
a = b;
b = temp;
}
// Knowing a is bigger, just check a first
if (a >= 10 && a <= 20)
return a;
if (b >= 10 && b <= 20)
return b;
return 0;
}
// --------------------------------------------------------------------------------------------
// Return true if the given string contains between 1 and 3 'e' chars.
//
//
// stringE("Hello") → true
// stringE("Heelle") → true
// stringE("Heelele") → false
public boolean stringE(String str) {
int count = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == 'e')
count++;
}
return (count >= 1 && count <= 3);
}
// --------------------------------------------------------------------------------------------
// Given two non-negative int values, return true if they have the same last
// digit, such as with 27 and 57. Note that the % "mod" operator computes
// remainders, so 17 % 10 is 7.
//
//
// lastDigit(7, 17) → true
// lastDigit(6, 17) → false
// lastDigit(3, 113) → true
public boolean lastDigit(int a, int b) {
return (a % 10 == b % 10);
}
// --------------------------------------------------------------------------------------------
// Given a string, return a new string where the last 3 chars are now in upper
// case. If the string has less than 3 chars, uppercase whatever is there. Note
// that str.toUpperCase() returns the uppercase version of a string.
//
//
// endUp("Hello") → "HeLLO"
// endUp("hi there") → "hi thERE"
// endUp("hi") → "HI"
public String endUp(String str) {
if (str.length() >= 3) {
int cut = str.length() - 3;
String front = str.substring(0, cut);
String back = str.substring(cut); // this takes from cut to the end
return front + back.toUpperCase();
}
return str.toUpperCase();
}
// --------------------------------------------------------------------------------------------
// Given a non-empty string and an int N, return the string made starting with
// char 0, and then every Nth char of the string. So if N is 3, use char 0, 3,
// 6, ... and so on. N is 1 or more.
// everyNth("Miracle", 2) → "Mrce"
// everyNth("abcdefg", 2) → "aceg"
// everyNth("abcdefg", 3) → "adg"
public String everyNth(String str, int n) {
String result = "";
for (int i = 0; i < str.length(); i = i + n) {
result = result + str.charAt(i);
}
return result;
}
}