-
Notifications
You must be signed in to change notification settings - Fork 3
/
LogicOne.java
573 lines (483 loc) · 17.9 KB
/
LogicOne.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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
/**
*
*/
package coding.bat.solutions;
/**
* @author Aman Shekhar
*
*/
public class LogicOne {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
// When squirrels get together for a party, they like to have cigars. A squirrel
// party is successful when the number of cigars is between 40 and 60,
// inclusive. Unless it is the weekend, in which case there is no upper bound on
// the number of cigars. Return true if the party with the given values is
// successful, or false otherwise.
//
//
// cigarParty(30, false) → false
// cigarParty(50, false) → true
// cigarParty(70, true) → true
public boolean cigarParty(int cigars, boolean isWeekend) {
if (isWeekend)
return cigars >= 40;
return (cigars >= 40 && cigars <= 60);
}
// --------------------------------------------------------------------------------------------
// You and your date are trying to get a table at a restaurant. The parameter
// "you" is the stylishness of your clothes, in the range 0..10, and "date" is
// the stylishness of your date's clothes. The result getting the table is
// encoded as an int value with 0=no, 1=maybe, 2=yes. If either of you is very
// stylish, 8 or more, then the result is 2 (yes). With the exception that if
// either of you has style of 2 or less, then the result is 0 (no). Otherwise
// the result is 1 (maybe).
//
//
// dateFashion(5, 10) → 2
// dateFashion(5, 2) → 0
// dateFashion(5, 5) → 1
public int dateFashion(int you, int date) {
if (you <= 2 || date <= 2)
return 0;
if (you >= 8 || date >= 8)
return 2;
return 1;
}
// --------------------------------------------------------------------------------------------
// The squirrels in Palo Alto spend most of the day playing. In particular, they
// play if the temperature is between 60 and 90 (inclusive). Unless it is
// summer, then the upper limit is 100 instead of 90. Given an int temperature
// and a boolean isSummer, return true if the squirrels play and false
// otherwise.
//
//
// squirrelPlay(70, false) → true
// squirrelPlay(95, false) → false
// squirrelPlay(95, true) → true
public boolean squirrelPlay(int temp, boolean isSummer) {
if (isSummer)
return (temp >= 60 && temp <= 100);
return (temp >= 60 && temp <= 90);
}
// --------------------------------------------------------------------------------------------
// You are driving a little too fast, and a police officer stops you. Write code
// to compute the result, encoded as an int value: 0=no ticket, 1=small ticket,
// 2=big ticket. If speed is 60 or less, the result is 0. If speed is between 61
// and 80 inclusive, the result is 1. If speed is 81 or more, the result is 2.
// Unless it is your birthday -- on that day, your speed can be 5 higher in all
// cases.
//
//
// caughtSpeeding(60, false) → 0
// caughtSpeeding(65, false) → 1
// caughtSpeeding(65, true) → 0
public int caughtSpeeding(int speed, boolean isBirthday) {
if (isBirthday)
speed -= 5;
if (speed <= 60)
return 0;
else if (speed <= 80)
return 1;
else
return 2;
}
// --------------------------------------------------------------------------------------------
// Given 2 ints, a and b, return their sum. However, sums in the range 10..19
// inclusive, are forbidden, so in that case just return 20.
//
//
// sortaSum(3, 4) → 7
// sortaSum(9, 4) → 20
// sortaSum(10, 11) → 21
public int sortaSum(int a, int b) {
int mResult = a + b;
if (mResult >= 10 && mResult <= 19)
return 20;
return mResult;
}
// --------------------------------------------------------------------------------------------
// Given a day of the week encoded as 0=Sun, 1=Mon, 2=Tue, ...6=Sat, and a
// boolean indicating if we are on vacation, return a string of the form "7:00"
// indicating when the alarm clock should ring. Weekdays, the alarm should be
// "7:00" and on the weekend it should be "10:00". Unless we are on vacation --
// then on weekdays it should be "10:00" and weekends it should be "off".
//
//
// alarmClock(1, false) → "7:00"
// alarmClock(5, false) → "7:00"
// alarmClock(0, false) → "10:00"
public String alarmClock(int day, boolean vacation) {
if (vacation) {
if (day == 0 || day == 6)
return "off";
return "10:00";
} else {
if (day == 0 || day == 6)
return "10:00";
return "7:00";
}
}
// --------------------------------------------------------------------------------------------
// The number 6 is a truly great number. Given two int values, a and b, return
// true if either one is 6. Or if their sum or difference is 6. Note: the
// function Math.abs(num) computes the absolute value of a number.
//
//
// love6(6, 4) → true
// love6(4, 5) → false
// love6(1, 5) → true
public boolean love6(int a, int b) {
if (a == 6 || b == 6)
return true;
return ((a + b) == 6 || Math.abs(a - b) == 6);
}
// --------------------------------------------------------------------------------------------
// Given a number n, return true if n is in the range 1..10, inclusive. Unless
// outsideMode is true, in which case return true if the number is less or equal
// to 1, or greater or equal to 10.
//
//
// in1To10(5, false) → true
// in1To10(11, false) → false
// in1To10(11, true) → true
//
public boolean in1To10(int n, boolean outsideMode) {
if (outsideMode)
return (n <= 1 || n >= 10);
return (n >= 1 && n <= 10);
}
// --------------------------------------------------------------------------------------------
// We'll say a number is special if it is a multiple of 11 or if it is one more
// than a multiple of 11. Return true if the given non-negative number is
// special. Use the % "mod" operator -- see Introduction to Mod
//
//
// specialEleven(22) → true
// specialEleven(23) → true
// specialEleven(24) → false
public boolean specialEleven(int n) {
return (n % 11 == 0) || (n % 11 == 1);
}
// --------------------------------------------------------------------------------------------
// Return true if the given non-negative number is 1 or 2 more than a multiple
// of 20. See also: Introduction to Mod
//
//
// more20(20) → false
// more20(21) → true
// more20(22) → true
public boolean more20(int n) {
return ((n % 20 == 1) || (n % 20 == 2));
}
// --------------------------------------------------------------------------------------------
// Return true if the given non-negative number is a multiple of 3 or 5, but not
// both. Use the % "mod" operator -- see Introduction to Mod
//
//
// old35(3) → true
// old35(10) → true
// old35(15) → false
public boolean old35(int n) {
if ((n % 3 == 0) && (n % 5 == 0))
return false;
return (n % 3 == 0) || (n % 5 == 0);
}
// --------------------------------------------------------------------------------------------
// Return true if the given non-negative number is 1 or 2 less than a multiple
// of 20. So for example 38 and 39 return true, but 40 returns false. See also:
// Introduction to Mod
//
//
// less20(18) → true
// less20(19) → true
// less20(20) → false
//
public boolean less20(int n) {
return (n + 1) % 20 == 0 || (n + 2) % 20 == 0;
}
// --------------------------------------------------------------------------------------------
// Given a non-negative number "num", return true if num is within 2 of a
// multiple of 10. Note: (a % b) is the remainder of dividing a by b, so (7 % 5)
// is 2. See also: Introduction to Mod
//
//
// nearTen(12) → true
// nearTen(17) → false
// nearTen(19) → true
//
public boolean nearTen(int num) {
return num % 10 >= 8 || num % 10 <= 2;
}
// --------------------------------------------------------------------------------------------
// Given 2 ints, a and b, return their sum. However, "teen" values in the range
// 13..19 inclusive, are extra lucky. So if either value is a teen, just return
// 19.
//
//
// teenSum(3, 4) → 7
// teenSum(10, 13) → 19
// teenSum(13, 2) → 19
//
public int teenSum(int a, int b) {
return (a >= 13 && a <= 19 || b >= 13 && b <= 19) ? 19 : a + b;
}
// --------------------------------------------------------------------------------------------
// Your cell phone rings. Return true if you should answer it. Normally you
// answer, except in the morning you only answer if it is your mom calling. In
// all cases, if you are asleep, you do not answer.
//
//
// answerCell(false, false, false) → true
// answerCell(false, false, true) → false
// answerCell(true, false, false) → false
public boolean answerCell(boolean isMorning, boolean isMom, boolean isAsleep) {
return !isAsleep && !(isMorning && !isMom);
}
// --------------------------------------------------------------------------------------------
// We are having a party with amounts of tea and candy. Return the int outcome
// of the party encoded as 0=bad, 1=good, or 2=great. A party is good (1) if
// both tea and candy are at least 5. However, if either tea or candy is at
// least double the amount of the other one, the party is great (2). However, in
// all cases, if either tea or candy is less than 5, the party is always bad
// (0).
//
//
// teaParty(6, 8) → 1
// teaParty(3, 8) → 0
// teaParty(20, 6) → 2
public int teaParty(int tea, int candy) {
if (candy < 5 || tea < 5)
return 0;
if (candy >= 5 && tea >= 5 && (candy >= 2 * tea || tea >= 2 * candy))
return 2;
return 1;
}
// --------------------------------------------------------------------------------------------
// Given a string str, if the string starts with "f" return "Fizz". If the
// string ends with "b" return "Buzz". If both the "f" and "b" conditions are
// true, return "FizzBuzz". In all other cases, return the string unchanged.
// (See also: FizzBuzz Code)
//
//
// fizzString("fig") → "Fizz"
// fizzString("dib") → "Buzz"
// fizzString("fib") → "FizzBuzz"
public String fizzString(String str) {
boolean fizz = str.charAt(0) == 'f';
boolean buzz = str.charAt(str.length() - 1) == 'b';
if (fizz && buzz)
return "FizzBuzz";
if (fizz)
return "Fizz";
if (buzz)
return "Buzz";
return str;
}
// --------------------------------------------------------------------------------------------
// Given an int n, return the string form of the number followed by "!". So the
// int 6 yields "6!". Except if the number is divisible by 3 use "Fizz" instead
// of the number, and if the number is divisible by 5 use "Buzz", and if
// divisible by both 3 and 5, use "FizzBuzz". Note: the % "mod" operator
// computes the remainder after division, so 23 % 10 yields 3. What will the
// remainder be when one number divides evenly into another? (See also: FizzBuzz
// Code and Introduction to Mod)
//
//
// fizzString2(1) → "1!"
// fizzString2(2) → "2!"
// fizzString2(3) → "Fizz!"
public String fizzString2(int n) {
boolean fizz = n % 3 == 0;
boolean buzz = n % 5 == 0;
if (fizz && buzz)
return "FizzBuzz!";
if (fizz)
return "Fizz!";
if (buzz)
return "Buzz!";
return n + "!";
}
// --------------------------------------------------------------------------------------------
// Given three ints, a b c, return true if it is possible to add two of the ints
// to get the third.
//
//
// twoAsOne(1, 2, 3) → true
// twoAsOne(3, 1, 2) → true
// twoAsOne(3, 2, 2) → false
public boolean twoAsOne(int a, int b, int c) {
return (a + b == c) || (b + c == a) || (a + c == b);
}
// --------------------------------------------------------------------------------------------
// Given three ints, a b c, return true if b is greater than a, and c is greater
// than b. However, with the exception that if "bOk" is true, b does not need to
// be greater than a.
//
//
// inOrder(1, 2, 4, false) → true
// inOrder(1, 2, 1, false) → false
// inOrder(1, 1, 2, true) → true
public boolean inOrder(int a, int b, int c, boolean bOk) {
if (bOk)
return b < c;
return a < b && b < c;
}
// --------------------------------------------------------------------------------------------
// Given three ints, a b c, return true if they are in strict increasing order,
// such as 2 5 11, or 5 6 7, but not 6 5 7 or 5 5 7. However, with the exception
// that if "equalOk" is true, equality is allowed, such as 5 5 7 or 5 5 5.
//
//
// inOrderEqual(2, 5, 11, false) → true
// inOrderEqual(5, 7, 6, false) → false
// inOrderEqual(5, 5, 7, true) → true
//
public boolean inOrderEqual(int a, int b, int c, boolean equalOk) {
if (equalOk)
return a <= b && b <= c;
return a < b && b < c;
}
// --------------------------------------------------------------------------------------------
// Given three ints, a b c, return true if two or more of them have the same
// rightmost digit. The ints are non-negative. Note: the % "mod" operator
// computes the remainder, e.g. 17 % 10 is 7.
//
//
// lastDigit(23, 19, 13) → true
// lastDigit(23, 19, 12) → false
// lastDigit(23, 19, 3) → true
//
public boolean lastDigit(int a, int b, int c) {
return a % 10 == b % 10 || a % 10 == c % 10 || b % 10 == c % 10;
}
// --------------------------------------------------------------------------------------------
// Given three ints, a b c, return true if one of them is 10 or more less than
// one of the others.
//
//
// lessBy10(1, 7, 11) → true
// lessBy10(1, 7, 10) → false
// lessBy10(11, 1, 7) → true
//
public boolean lessBy10(int a, int b, int c) {
return Math.abs(a - c) >= 10 || Math.abs(a - b) >= 10 || Math.abs(b - c) >= 10;
}
// --------------------------------------------------------------------------------------------
// Return the sum of two 6-sided dice rolls, each in the range 1..6. However, if
// noDoubles is true, if the two dice show the same value, increment one die to
// the next value, wrapping around to 1 if its value was 6.
//
//
// withoutDoubles(2, 3, true) → 5
// withoutDoubles(3, 3, true) → 7
// withoutDoubles(3, 3, false) → 6
public int withoutDoubles(int die1, int die2, boolean noDoubles) {
if (!noDoubles)
return die1 + die2;
if (die1 == die2)
return (die1 != 6) ? die1 + die2 + 1 : die1 + 1;
return die1 + die2;
}
// --------------------------------------------------------------------------------------------
// Given two int values, return whichever value is larger. However if the two
// values have the same remainder when divided by 5, then the return the smaller
// value. However, in all cases, if the two values are the same, return 0. Note:
// the % "mod" operator computes the remainder, e.g. 7 % 5 is 2.
//
//
// maxMod5(2, 3) → 3
// maxMod5(6, 2) → 6
// maxMod5(3, 2) → 3
//
public int maxMod5(int a, int b) {
if (a == b)
return 0;
if (a % 5 == b % 5)
return (a < b) ? a : b;
return (a < b) ? b : a;
}
// --------------------------------------------------------------------------------------------
// You have a red lottery ticket showing ints a, b, and c, each of which is 0,
// 1, or 2. If they are all the value 2, the result is 10. Otherwise if they are
// all the same, the result is 5. Otherwise so long as both b and c are
// different from a, the result is 1. Otherwise the result is 0.
//
//
// redTicket(2, 2, 2) → 10
// redTicket(2, 2, 1) → 0
// redTicket(0, 0, 0) → 5
public int redTicket(int a, int b, int c) {
if (a == b && b == c)
return (c == 2) ? 10 : 5;
if (a != b && a != c)
return 1;
return 0;
}
// --------------------------------------------------------------------------------------------
// You have a green lottery ticket, with ints a, b, and c on it. If the numbers
// are all different from each other, the result is 0. If all of the numbers are
// the same, the result is 20. If two of the numbers are the same, the result is
// 10.
//
//
// greenTicket(1, 2, 3) → 0
// greenTicket(2, 2, 2) → 20
// greenTicket(1, 1, 2) → 10
//
public int greenTicket(int a, int b, int c) {
if (a != b && a != c && c != b)
return 0;
return (a == b && b == c) ? 20 : 10;
}
// --------------------------------------------------------------------------------------------
// You have a blue lottery ticket, with ints a, b, and c on it. This makes three
// pairs, which we'll call ab, bc, and ac. Consider the sum of the numbers in
// each pair. If any pair sums to exactly 10, the result is 10. Otherwise if the
// ab sum is exactly 10 more than either bc or ac sums, the result is 5.
// Otherwise the result is 0.
//
//
// blueTicket(9, 1, 0) → 10
// blueTicket(9, 2, 0) → 0
// blueTicket(6, 1, 4) → 10
public int blueTicket(int a, int b, int c) {
if (a + b == 10 || a + c == 10 || c + b == 10)
return 10;
return (a - c == 10 || b - c == 10) ? 5 : 0;
}
// --------------------------------------------------------------------------------------------
// Given two ints, each in the range 10..99, return true if there is a digit
// that appears in both numbers, such as the 2 in 12 and 23. (Note: division,
// e.g. n/10, gives the left digit while the % "mod" n%10 gives the right
// digit.)
//
//
// shareDigit(12, 23) → true
// shareDigit(12, 43) → false
// shareDigit(12, 44) → false
public boolean shareDigit(int a, int b) {
return (a % 10 == b % 10 || a / 10 == b / 10 || a % 10 == b / 10 || b % 10 == a / 10);
}
// --------------------------------------------------------------------------------------------
// Given 2 non-negative ints, a and b, return their sum, so long as the sum has
// the same number of digits as a. If the sum has more digits than a, just
// return a without b. (Note: one way to compute the number of digits of a
// non-negative int n is to convert it to a string with String.valueOf(n) and
// then check the length of the string.)
//
//
// sumLimit(2, 3) → 5
// sumLimit(8, 3) → 8
// sumLimit(8, 1) → 9
//
public int sumLimit(int a, int b) {
String sum = String.valueOf(a + b);
String lengthA = String.valueOf(a);
return (sum.length() == lengthA.length()) ? a + b : a;
}
}