I’m in college, and I’ve recently started competing in programming competitions with my friends. These competitions involve solving algorithmic problems quickly. It’s a lot of fun, but there’s one problem: I’m forced to use Java. (My teammates use Java.)
Background: I’m a self-taught JavaScript programmer, and it hurts to write Java code. I find it very verbose and inflexible, and I feel slowed down when having to declare types and decide which of the eighty list data structure to use. I’m also frustrated about the lack of functional programming features and how verbose using regular expressions, arrays, and dictionaries are.
As an example, consider the problem of finding the length of the longest string of consecutive characters in a given string. So the string XX22BBBBccXX222
would give 4
, for the string of four B
s. In Java, I’d have to loop through and manually count characters and manually keep track of the maximum. (That’s at least as far as I’m aware — I’m not as familiar with Java as I am with JavaScript.) In JavaScript, I’d find it like this:
var max = Math.max.apply(Math, str.match(/(.)1*/g).map(function (s) { return s.length; }));
Much quicker and simpler, in my book.
The question: what are some Java features, techniques, or patterns well-suited for fast, algorithmic coding?
3
I used to do a few programming competitions, and found that the most important things to know were:
- The full APIs of your collection classes. Stacks, Sets, Maps and Queues are pretty critical to many algorithms.
- All of the String processing tricks (regexes, string joining, parsing etc.). This is because Strings are often used as inputs / data types to manipulate
- All of the variations of looping / iteration constructs – again these come up so frequently, you want to be able to select the right one and use it pretty much automatically.
- Features of your IDE. Code completion, type checking, unit testing etc are very useful if you want to write code quickly while also being 99% confident that it is correct. The compiler/IDE can help you a lot.
The core of the problem is that you’re not as familiar with Java as your friends.
Rather than try to look for a generic solution, I’d say the best solution here is the same as when trying to learn a language or codebase in other situations: More exposure.
In this case, the simplest thing would be to look at your friend’s solutions to see what patterns they used, but might not be the most effective.
Another approach would be to take your Javascript and break it down into a more iterative form. For example:
Math.max.apply(Math, str.match(/(.)1*/g).map(function (s) { return s.length; }));
There’s 3 core conceptual pieces to this:
- The regex for finding groupings
- Converting each group to a length
- Finding the maximum
Individually they’re all pretty easy. Java has support for regex groups, all you need is a simple loop to get the lengths, and finding the maximum is just as easy. It can even be made simpler by combining those last two in Java – since all you need for output is the number, all you need is a single variable to hold the end result, rather than an entire array for all the string lengths.