|
| 1 | +package medium; |
| 2 | + |
| 3 | +/** |
| 4 | + * Have the function EvenPairs(str) take the str parameter being passed |
| 5 | + * and determine if a pair of adjacent even numbers exists anywhere |
| 6 | + * in the string. If a pair exists, return the string true, |
| 7 | + * otherwise return false. |
| 8 | + * --- |
| 9 | + * For example: if str is "f178svg3k19k46" |
| 10 | + * then there are two even numbers at the end of the string, "46" |
| 11 | + * so your program should return the string true. |
| 12 | + * --- |
| 13 | + * Another example: if str is "7r5gg812" then the pair is "812" (8 and 12) |
| 14 | + * so your program should return the string true. |
| 15 | + */ |
| 16 | +public class EvenPairs { |
| 17 | + |
| 18 | + /** |
| 19 | + * Strips a string from non-numerical characters |
| 20 | + * and splits the chunks into the array of strings |
| 21 | + * e.g."5678dd12y" -> ["5678, "12"] |
| 22 | + * |
| 23 | + * @param str input string |
| 24 | + * @return an array containing number groups |
| 25 | + */ |
| 26 | + private static String[] splitNumbers(String str) { |
| 27 | + return str |
| 28 | + .toLowerCase() |
| 29 | + .replaceAll("([^0-9])", " ") |
| 30 | + .replaceAll(" +", " ") |
| 31 | + .trim().split(" "); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Appends N following zeroes to 1 where N = number of digits |
| 36 | + * e.g. 1 -> 1, 2 -> 100, 3 -> 1000 |
| 37 | + * |
| 38 | + * @param digits number of digits |
| 39 | + * @return expanded number |
| 40 | + */ |
| 41 | + private static int addFollowingZeroes(int digits) { |
| 42 | + int result = 1; |
| 43 | + if (digits == 1) { |
| 44 | + return result; |
| 45 | + } |
| 46 | + for (int i = 0; i < digits; i++) { |
| 47 | + result *= 10; |
| 48 | + } |
| 49 | + return result; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Parses a number to determine if digits can form a pair of even number. |
| 54 | + * |
| 55 | + * @param num input number |
| 56 | + * @return true if it's a pair of even numbers |
| 57 | + */ |
| 58 | + private static boolean isEvenPair(Integer num) { |
| 59 | + // get a number of digits |
| 60 | + int len = (int) (Math.log10(num) + 1); |
| 61 | + for (int i = len - 1; i > 0; i--) { |
| 62 | + int num1 = num / addFollowingZeroes(i); |
| 63 | + int num2 = num - num1; |
| 64 | + if (num1 % 2 == 0 && num2 % 2 == 0) { |
| 65 | + return true; |
| 66 | + } |
| 67 | + } |
| 68 | + return false; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Even Pairs function. |
| 73 | + * |
| 74 | + * @param str input string |
| 75 | + * @return "true" if a pair exists |
| 76 | + */ |
| 77 | + public static String evenPairs(String str) { |
| 78 | + String[] coll = splitNumbers(str); |
| 79 | + for (String item : coll) { |
| 80 | + if (item.length() > 1 && isEvenPair(Integer.parseInt(item))) { |
| 81 | + return "true"; |
| 82 | + } |
| 83 | + } |
| 84 | + return "false"; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Entry point. |
| 89 | + * |
| 90 | + * @param args command line arguments |
| 91 | + */ |
| 92 | + public static void main(String[] args) { |
| 93 | + String result1 = evenPairs("7r5gg812"); |
| 94 | + System.out.println(result1); |
| 95 | + String result2 = evenPairs("f178svg3k19k46"); |
| 96 | + System.out.println(result2); |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments