Java – Exam – Longest Odd-Even Sequence – Challenge!

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.

imagesimagesimagesimagesimagesimagesimagesimagesimagesimages

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:

Quite a long one, isn’t it?

The original code of the solution looks a little better and shorter:

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:

Enjoy it!

About

VBA Developer

Tagged with: ,