Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public boolean nearBase(int num, int base, int deviation) {
- // ..TODO check guards, normalize input or throw exceptions
- // - base and deviation are expected strict positive integers
- // - deviation is expected smaller than base
- // - num is expected a positive integer
- var congruent = num % base;
- var inverted = base - congruent;
- var distance = Math.min(congruent, inverted);
- return distance <= deviation;
- }
- // your method rewritten ->
- public boolean nearTen(int num) {
- return nearBase(num, 10, 2);
- }
- 7 % 10 = 7
- 8 % 10 = 8
- 9 % 10 = 9
- 10 % 10 = 0
- 11 % 10 = 1
- 12 % 10 = 2
- 13 % 10 = 3
- public boolean nearTen(int num) {
- int modulo = num % 10;
- return modulo <= 2;
- }
- public boolean nearTen(int num) {
- int modulo = num % 10;
- return modulo <= 2 || num - modulo <= 2;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement