Python Challenge – Level 3

After a long break, I’ve returned with another Python Challenge solution. This one is for Level 3, which can be found at http://www.pythonchallenge.com/pc/def/equality.html.

Like Level 2, this challenge is concerned with regular expressions. Below the normal HTML source code, you’ll find a long jumble of nonsensical letters. The task is to find all the lower-case letters which are surrounded by three upper-case letters on each side. It is important to note that the number of upper-case letters must be exactly three on each side of the lower-case letter. There should not be more than three, nor less than three. It must be three exactly.

When you’re ready, here is the solution (be aware I’ve had to line-break some code which was too long to display on this page):

import re
import urllib

sourcepage = urllib.urlopen("http://www.pythonchallenge.com/pc/def/
                                                                equality.html")
sourcetext = sourcepage.read()
characters = "".join(re.findall("[^A-Z][A-Z]{3}([a-z])[A-Z]{3}[^A-Z]",
                                                                sourcetext))
print characters

The result should be:

linkedlist

Simply substitute “equality” in the address with “linkedlist”. The resulting page will say “linkedlist.php”, so use the php file extension instead of html. Then you’re onto the next challenge.

Regular expressions: http://www.regular-expressions.info/
Python’s regular expressions library: http://docs.python.org/library/re.html

Advertisement

Comments are closed.

Follow

Get every new post delivered to your Inbox.