As you probably know, for a long time (about 5 weeks) I started to learn Java from scratch. Today, I have decided to take a look at one of the exam problems at the Software University and I have spent some time on it, getting more of the QA tests were passed 🙂
Anyway, if you can come up with a better result, using my code, consider it a challenge.
Thus, let me show you what was the problem about – you have some input like this –
(3) (22) (-18) (55) (44) (3) (21) |
And your task is to give an output of the count of the numbers, which are sequential. In our case “sequential” has another meaning – it means “odd-even sequence” or “even-odd sequence”. E.g. in the given example, the highest count of the sequential numbers is “4”:
(-18) (55) (44) (3)
To make it more interesting, ZERO is counted as odd and even in the same time.
Ok, this is what I have come up with:
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 |
import java.util.*; public class LongestOddEvenSequence { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String inputLine = scanner.nextLine(); String[] numbers = inputLine.split("[ ()]+"); int[] iArray = new int[numbers.length - 1]; for (int i = 0; i < iArray.length; i++) { iArray[i] = Integer.parseInt(numbers[i + 1]); } int Counter = 0; int CurrentCounter = 0; int CounterA = 0; int CounterB = 0; boolean EvenNeeded = true; // Start with Odd for (int i = 0; i < iArray.length; i++) { if (EvenNeeded) { if (iArray[i] == 0) { CurrentCounter++; EvenNeeded = false; } else if (iArray[i] % 2 == 0) { CurrentCounter++; EvenNeeded = false; } else if (iArray[i] % 2 == 1) { if (CurrentCounter > Counter) { Counter = CurrentCounter; } EvenNeeded = true; CurrentCounter = 0; } } else { if (iArray[i] == 0) { CurrentCounter++; EvenNeeded = true; } else if (iArray[i] % 2 == 1) { CurrentCounter++; EvenNeeded = true; } else if (iArray[i] % 2 == 0) { if (CurrentCounter > Counter) { Counter = CurrentCounter; } EvenNeeded = false; CurrentCounter = 0; } } } if (CurrentCounter > Counter) { Counter = CurrentCounter; } CounterA = Counter; Counter = 0; CurrentCounter = 0; for (int i = 0; i < iArray.length; i++) { if (EvenNeeded == false) { if (iArray[i] == 0) { CurrentCounter++; EvenNeeded = true; } else if (iArray[i] % 2 == 1) { CurrentCounter++; EvenNeeded = true; } else if (iArray[i] % 2 == 0) { if (CurrentCounter > Counter) { Counter = CurrentCounter; } EvenNeeded = false; CurrentCounter = 0; } } else { if (iArray[i] == 0) { CurrentCounter++; EvenNeeded = false; } else if (iArray[i] % 2 == 0) { CurrentCounter++; EvenNeeded = false; } else if (iArray[i] % 2 == 1) { if (CurrentCounter > Counter) { Counter = CurrentCounter; } EvenNeeded = true; CurrentCounter = 0; } } } if (CurrentCounter > Counter) { Counter = CurrentCounter; } CounterB = Counter; if (CounterA > CounterB) { System.out.println(CounterA + 1); } else { System.out.println(CounterB + 1); } } } |
Quite a long one, isn’t it?
The original code of the solution looks a little better and shorter:
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 |
import java.util.Scanner; public class LongestOddEvenSequence { public static void main(String[] args) { // Read and parse the input numbers Scanner scanner = new Scanner(System.in); String inputLine = scanner.nextLine(); String[] numbers = inputLine.split("[ ()]+"); int[] nums = new int[numbers.length-1]; for (int i = 0; i < nums.length; i++) { nums[i] = Integer.parseInt(numbers[i+1]); } // Find the longest alternating sequence int bestLen = 0; int len = 0; boolean shouldBeOdd = (nums[0] % 2 != 0); for (int num : nums) { boolean isOdd = num % 2 != 0; if (isOdd == shouldBeOdd || num == 0) { len++; } else { shouldBeOdd = isOdd; len = 1; } shouldBeOdd = !shouldBeOdd; if (len > bestLen) { bestLen = len; } } System.out.println(bestLen); } } |
What is worthy in the original code:
- Usage of a regular expression for the splitting of the inputLine into array;
- A boolean is declared with the evaluation of an equation! Quite a good approach (line 19)!
- The for each loop – quite a good idea, indeed, I have used the standard for;
- In the for each loop we have no usage of TRUE and FALSE (check my code for the mess). Indeed, this way the code looks much better and understandable!
Takeaway message – Use the following boolean declaration, whenever possible:
1 2 3 |
boolean isOdd = num % 2 != 0; boolean shouldBeOdd = (nums[0] % 2 != 0); shouldBeOdd = !shouldBeOdd; |
Enjoy it!