# Old code does not bring back memories…

Interesting stuff, was reading some of this old pieces of code (Code Jam solved problems) that I began posting yesterday, and I couldn’t understand much of them. It starts with key things I could easily fix, such as bad variable names (blerg, past me!), and goes until I get to the conclusion that I just can’t remember why I coded them that way. Total nonsense inside text files!

It’s being a nice effort to just read some of those pieces of code, looking like they were made by a different person. Perhaps that’s part of evolution, you rebuild the way you think and solve problems. Always getting simpler.

Weird. # 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:

Input

The first line of input contains 3 integers, LD 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.

Output

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 *

ST_WORDS=2
ST_PATTERNS=3
ST_EXIT=4

cases = 0
casen = 1
wordnum = 0
wordlen = 0

words = list()
patterns = list()

while state != ST_EXIT :
line = raw_input()

state = ST_WORDS
elif state == ST_WORDS:
words.append(line)
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
else:
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:
matches+=1

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

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

exit()
```

• 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! 😀

# Code Jammin’

Always been a fan of Google Code Jam (http://code.google.com/codejam/), tried competing once (2009 with Java, I guess), passed the first stage, didn’t made after the second. 😀 Still, was a great experience on how to code and understand problems in efficient ways, using small amounts of time and confident solutions. The problems I faced through the competition were pushing language and I/O processing boundaries… categories that I’ve improved much through professional experience.

So, as I intend to participate in this year’s competition, I’ll start to upload everyday a solution to a given codejam problem, usually written in Python (great code development speed) and some comments about it. 🙂

So let the Jammin’ begin!

# Took a while…

But here we are. New things to start popping in tonight. 🙂