Tag Archives: 2009

CodeJam 2009 Qualification A

This was my first Code Jam problem ever, and I was quite thrilled with the competition at the time! ūüėÄ So I figured it would be the perfect introduction problem. You can access through the following link:


It is Alien Language, and it says:

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.

A pattern consists of exactly L tokens. Each token is either a single lowercase letter (the scientists are very sure that this is the letter) or a group of unique lowercase letters surrounded by parenthesis ( and ). For example: (ab)d(dc) means the first letter is either a or b, the second letter is definitely d and the last letter is either d or c. Therefore, the pattern (ab)d(dc) can stand for either one of these 4 possibilities: add, adc, bdd, bdc.

So, it means there is a translation expression and words that we must match, in order to know how many words belong to the  test case language. Ring any bells? Yes, regular expressions! So, all we need to do is write the text reader for the input rules, read the words and build the regular expression from the test case line, and count the matches!

I/O rules:


The first line of input contains 3 integers, L, D and N separated by a space. D lines follow, each containing one word of length L. These are the words that are known to exist in the alien language. N test cases then follow, each on its own line and each consisting of a pattern as described above. You may assume that all known words provided are unique.


For each test case, output:

Case #X: K

where X is the test case number, starting from 1, and K indicates how many words in the alien language match the pattern.

So, the following was produced:

import re, string, sys

from array import *


state =ST_HEADER
cases = 0
casen = 1
wordnum = 0
wordlen = 0

words = list()
patterns = list()

while state != ST_EXIT :
	line = raw_input()

	if state == ST_HEADER:
		header = line.split(" ")
		wordlen = int(header[0])
		wordnum = int(header[1])
		cases = int(header[2])

		state = ST_WORDS
	elif state == ST_WORDS:
		if len(words) == wordnum:
			state = ST_PATTERNS

	elif state == ST_PATTERNS:
		expr = ""
		insor = False
		justin = False

		for i in range(len(line)):
			if line[i] == '(':
				expr += '('
				insor = True
				justin = True
			elif line[i] == ')':
				expr += ')'
				insor = False
				justin = False
				if insor == True:
					if justin != True:
						expr += '|'
					justin = False

				expr += line[i]

		p = re.compile(expr)
		matches = 0

		for i in words:
			if p.match(i) != None:

		print "Case #%d: %d" % (casen,matches)

		if casen-1 == cases:
			state = ST_EXIT
		state = ST_EXIT


Some comments on the code:

  • I like reading the file through a simple FSM, which tells me when to stop reading headers, test cases and when I should be done processing. Not all problemas can be solved this way (some inputs are more complex).
  • There must be a better way for building the regular expression based on the test case. You can review the submitted versions on the codejam page and look for alternatives (including other languages).
  • Assembling the expression is the “hard” work here ūüôā

Happy coding! ūüėÄ