Regexp fun
November 25, 2009 on 11:57 pm | In IT, fun | No CommentsReading the post about useful regular expressions, remembered what my favourite solution is to one of the questions of the test we give to junior Java developers.
The task is to write a method that takes a string as a parameter and returns the acronym of the string in uppercase made up of the first letters of the words in the string. The acronym must ignore the words “the”, “of” and “and”.
The usual solutions are either to sequentially step through the string (Yuck!) or split it up or use a StringTokenizer class. The people usually overlook the fact, that the input strings can be padded with whitespace, or contain multiple spaces, and they usually ignore, that the keywords that are to be omitted might be found on the begining of a valid word. Thus my test ” United States of Andorra” string breaks most of the methods. The ones who have time to write the answer down, usually forget to return the value from the method, or to change it to uppercase and sometimes even ignore that it should be a method to start with! This is my favourite question, as it can really show how the applicant can handle stressful situations.
I was tired after several interviews one day and tried to come up with the shortest possible solution. Naturally it contains regular expressions.
My solution looked something like this (OK I just reproduced it for the sake of the article, using nano and javac, so it might have overlooked flaws in it):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public class Acronym { private static String toAcronym(String str) { return str.toUpperCase(). replaceAll("(THE|OF|AND)(\\W+|$)",""). replaceAll("(\\w)\\w*\\W*","$1"); } public static void main (String args[]) { if(args.length>0) { System.out.println(toAcronym(args[0])); } } } |
New trends in Linux desktop UIs
November 24, 2009 on 11:14 pm | In IT | No Comments
As I see there is a solid movement to reform the user interfaces that are in use in our computers. After all the menu bars with a start menu are on the mainstream desktops since the win95/OS2 era. And the programs can be started using icons on the desktop and the start menu. For the ones who know what they are looking for there is also a command line. And there is a task bar to switch your running applications. But things ought to change with time, shouldn’t they?
Continue reading New trends in Linux desktop UIs…
