Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. public boolean nearBase(int num, int base, int deviation) {
  2.  
  3. // ..TODO check guards, normalize input or throw exceptions
  4. // - base and deviation are expected strict positive integers
  5. // - deviation is expected smaller than base
  6. // - num is expected a positive integer
  7.  
  8. var congruent = num % base;
  9. var inverted = base - congruent;
  10. var distance = Math.min(congruent, inverted);
  11. return distance <= deviation;
  12. }
  13.  
  14. // your method rewritten ->
  15. public boolean nearTen(int num) {
  16. return nearBase(num, 10, 2);
  17. }
  18.  
  19. 7 % 10 = 7
  20. 8 % 10 = 8
  21. 9 % 10 = 9
  22. 10 % 10 = 0
  23. 11 % 10 = 1
  24. 12 % 10 = 2
  25. 13 % 10 = 3
  26.  
  27. public boolean nearTen(int num) {
  28. int modulo = num % 10;
  29. return modulo <= 2;
  30. }
  31.  
  32. public boolean nearTen(int num) {
  33. int modulo = num % 10;
  34. return modulo <= 2 || num - modulo <= 2;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement