Christian Harms's picture

Google code jam solution for alien language

Problem

In the 2009 qualification round there was a simple problem with a nice background story:

After years of study, scientists at Google Labs have discovered an alien language transmitted from a faraway planet. The alien language is very unique in that every word consists of exactly L lowercase letters. Also, there are exactly D words in this language.

Once the dictionary of all the words in the alien language was built, the next breakthrough was to discover that the aliens have been transmitting messages to Earth for the past decade. Unfortunately, these signals are weakened due to the distance between our two planets and some of the words may be misinterpreted. In order to help them decipher these messages, the scientists have asked you to devise an algorithm that will determine the number of possible interpretations for a given pattern.Read more

Christian Harms's picture

The rotate google contest in 15 lines

The rotate example nico last week reported was funny to solve: No rotating needed! Read the complete problem description in nicos article or in the google code contest page. Here my complete python solution with file handling and solution printing.

  1. import sys
  2. from re import search
  3.  
  4. fp = file(sys.argv[1])
  5. for case in range(1, 1+int(fp.next())):
  6.     n, k = [int(x) for x in fp.next().split()]
  7.     lines = [fp.next() for x in range(n)]
  8.    
  9.     #right-gravitation and joining to one line (delim is a line)
  10.     s = "#".join(map(lambda x:"%%%ds"%n%(x.strip().replace(".","")), lines))
  11.     result = 0
  12.  
  13.     #find one of the 4 variants: horiz, verti, slash, backslash for Blue
  14.     reB = "(B.{%%d}){%d}B" % (k-1)
Read more

Nico Heid's picture

Google Code Jam - Rotate

It's time for some basic finger exercise. The Google Code Jam Rotate is very trivial, so relax and fire up your IDE.

I was a bit lazy, so there is no reading of the input sets, just a two-dimensional array and two functions

Rotating

As the Google solution pointed out, there is actually no need to really rotate the 2dim array. Just push everything to the right, as if gravity would be to the right. That's the same as rotating everything and keeping gravity towards the bottom. But we save a few lines this way.

So here is the "gravity from the right" code

  1. public static void fakeRotate(char[][] board) {
  2.  
  3.     for (int i = 0; i < N; i++) {
  4.         for (int j = N - 1; j >= 0; j--) {
  5.             if (board[i][j] != '.') {
  6.                 // push to right
  7.                 int m = 1;
  8.                 while ((j + m) < N && board[i][j + m] == '.') {
Read more

Nico Heid's picture

Google Code Jam - Theme Park

Theme Park is another good example of a problem which can be solved with a fairly simple algorithm. The simple version will solve the small input set without a problem, but will run almost infinitely with the large dataset.

You can read the problem on the Google Code Jam site: Theme Park

simple solution

The simple solution is to put the groups in a static array, use a pointer that "wraps around" using modulo and fill the roller coaster until it's so full, that the next group does not fit in, then let it ride earning income accordingly to the seats taken.

This is a correct solution, but unfortunately a bit slow. The input set can have a roller coaster with up to 109 seats and 108 rides and groups as large as 107. Read more

Christian Harms's picture

3 caching steps to boost your webservice by x10

I am running a IP-Geo-location demo in my App Engine. After many silent months I got some traffic on it (and hitting the daily CPU limit). Some one would kick the "traffic monster", after all it's starts only as a demo, other ones (like me) will try to optimize the GEO-location script.

App Engine overquotaRead more

Phillip Steffensen's picture

Android: Dealing with ListActivities, customized ListAdapters and custom-designed items

Due to its ListActivity class the Android SDK helps developers to create List-GUIs easily. Therefore the ListActivity uses a ListView. But what about customized lists? What about own list-designs? In this post I'll try to show how to extend a standard ListView to create a custom list-design. To create your own list-design, you should define how an item in your list should look like. As an example I'll use a list of Twitter-updates (also called „tweets“).

Read more

Nico Heid's picture

Google Code Jam - The Snapper Chain

The Google problems are always nice, sometimes even a bit too tricky. This years entry problem was quite nice with some difficulties you could run in.

You need to read the problem first, to follow the article, so here's the link: http://code.google.com/codejam/contest/dashboard?c=433101#s=p0&a=0

If you're a hands on programmer first you might just want implement the chain without looking at the problem more extensively. So you might end up with something like this:

  1.                         // snapping
  2.                         boolean toggle = true;
  3.                         for (int j = 0; j < snaps; j++) {
  4.                                 toggle = true;
  5.                                 for (int k = 0; k < snapperCount; k++) {
  6.                                         if(toggle == false){
  7.                                                 break;
  8.                                         }
  9.                                         if (toggle == true) {
  10.                                                 snapper[k] = !snapper[k];
  11.                                         }
  12.                                         if (snapper[k] == true) {
  13.                                                 toggle = false;
  14.                                         }
  15.  
  16.                                 }
  17.                         }
  18.  
  19.                         // we need power on all snappers
  20.                         boolean power = true;
  21.                         for (int j = 0; j < snapperCount; j++) {
  22.                                 if (snapper[j] == false) {
Read more

Christian Harms's picture

What are yoda conditions?

Yoda was a great teacher except for his word sequence. For programmer exists the yoda condition. But was is it? Here a normal if-code-snipplet :

  1. if (value == 42) {
  2.     ...
  3. }

As yoda condition written:

  1. if (42 == value) {
  2.     ...
  3. }

I found some discussions on stackoverflow.com and collected some pros and no really contras.Read more

Nico Heid's picture

Are you producing banana software?

Bananas are picked from the tree green and unripe. Their ripening process continues while they are transported to the markets and end up at the customer. Let us just ignore the fact, that the ripening process is initiated by an artifical condition.

So Banana-Software is software, that "ripes" while in production at the customer. Some people might call it Beta-Software. I've seen plenty of examples where software went into production when it was still Banana-Software.

There's nothing bad in developing software in close contact with the customer. Eg. using Extreme Programming, Rapid Prototyping or some other agile method you prefer. But when it comes to putting software into production, there are some things to consider.Read more

Christian Harms's picture

JavaScript Object property getter benchmarks - part 2

The object access benchmarks last weeks was not surprising but a factor 5 between access "obj.a" and "obj.getA()" is interesting. In this second part I check the access time for properties on objects, created with ECMAScript 3 getter/setter and with the ECMAScript 5 Object.defineProperty variant (works on IE8+, FF3.7+, newest Webkit and Chrome 5) - see more on ECMAScript 5 compatibility table. Oliver asked to add getter tests - and the results produce differences with factor 15-100.Read more

Syndicate content