The Python Challenge – 3

This one was not so hard.  (Or so I thought).  I could see that it was a regular expression problem right away.  The hardest bit was getting the regex right.

Python 2.7 Solution

import re
data = ''.join([line.rstrip() for line in open('3_message.txt')])
# Looking for the pattern lUUUlUUUl (U = UPPER, l = lower)
regex = r'(?the_answer = re.findall(regex, data)
if len(the_answer) == 1:
    print the_answer[0]
else:
    print "Sorry, your regex is crap.  There were %i matches " % len(the_answer)
    from pprint import pprint
    pprint(the_answer)

But the evil setters were much cleverer than that.  I got:

Sorry, your regex is crap.  There were 10 matches
[‘IQNlQSL’,
 ‘OEKiVEY’,
 ‘ZADnMCZ’,
 ‘ZUTkLYN’,
 ‘CNDeHSB’,
 ‘OIXdKBF’,
 ‘XJVlGZV’,
 ‘ZAGiLQZ’,
 ‘CJAsACF’,
 ‘KWGtIDC’]

This made me sad, even though it was a message to myself.  I pondered the problem and then looked at the middle letters.  They said “linked list”.  wtf?!

I looked up linked lists and was none the wiser.  I then thought I would just try typing linkedlist in to the url, and it worked.  I wonder if Linked Lists are key to the next one.  I rewrote the code so it outputted this.

Better Python 2.7 Solution

import re
data = ''.join([line.rstrip() for line in open('3_message.txt')])
regex = r'(?the_answer = re.findall(regex, data)
print 'You are regex GOD. The answer is %s' % ''.join(word[3] for word in the_answer)

Python 3.2 Solution

import re
data = ''.join([line.rstrip() for line in open('3_message.txt')])
regex = r'(?the_answer = re.findall(regex, data)
print ('You are regex GOD. The answer is %s' % ''.join(word[3] for word in the_answer))

I think I am learning slowly.  But there were about 50 ways of doing this in the solutions and my regex was very basic.  The next one looks pretty basic given my experience with php.