Wednesday, February 28, 2007

Thoughts on Python Challenge

This afternoon was supposed to be action-packed since I needed to submit a paper for AOSD and finis my last piece of work at NCS but I got myself totally addicted to Python Challenge.

After almost 2 hours of playing around with it I have reached level 4. Of course you need to go pass the previous 3 levels to get the url of level 4 so I will not post it here. But man, it took me time.

One of the most interesting things I have observed is that my code is too "big". There are some very elegant solutions out there. Each time after I finished a level I went to check how others crack the problem and discovered some very elegant solutions. It's especially true with Level 3 when I did not realize it was a regular expression problem and went my own way to check the characters manually. I had multiple solutions and none of them were correct. So I got frustrated and checked the re solution. Only after I played with the "elegant" solution, did I realize that I had the solution all along, just that I didn't know how to use it. How stupid.

Belows are my solutions for the first three levels. Don't look if you plan to solve it in python or your favorite language.

[Begin spoiler]

def caesar_decipher(str, shift):

"""Python Challenge Level 1 solution"""

ret_str = ''
for x in str:
if x in string.ascii_lowercase:
ret_str += chr((ord(x)-ord('a')+shift)%26 + ord('a'))
elif x in string.ascii_uppercase:
ret_str += chr((ord(x)-ord('A')+shift)%26 + ord('A'))
else:
ret_str += x
return ret_str


def find_alpha_in_file(file):
"""Python Challenge Level 2 solution"""
f = open(file, 'r')
return ''.join(ch for line in f for ch in line if ch in string.ascii_letters)


def find_bodyguard_char_in_file(file):

"""Python Challenge Level 3 solution"""
ret_str = ""
f = open(file, 'r')
for line in f:
for index in range(len(line)):
try:
if line[index] in string.ascii_lowercase
and index-3 > 0
and len(line[index-3:index])==3 and len(line[index+1:index+4])==3
and reduce(lambda x, y: x and y in string.ascii_uppercase, line[index-3:index], True)
and reduce(lambda x, y: x and y in string.ascii_uppercase, line[index+1:index+4], True)
and line[index-4] in string.ascii_lowercase and line[index+4] in string.ascii_lowercase:
ret_str += line[index]
except (IndexError):
pass
return ret_str


[End spoiler]

However, a solution that uses regular expression for level 3 has only 3 line!!! And luckily there is generator expression and lambda function in python or the solution would be much more verbose.

I was also tempted to solve the FizzBuzz problem in codinghorror.com in python too, just to demonstrate my new prowess in python but, Jeff posted a rebuke subsequently and seeing how stupid it is, I dropped the idea. But nevertheless I got to admit that I couldn't quite solve swap-without-temp problem (I didn't know how to use XOR) and I'm quite ashamed of it. It tells me that even though I have a master degree (almost) and going to work at a very big software company later, there are still things I need to learn,and the fundamental arithmetics and bit operations remain as relevant as ever.

Update: My code got complained to be too unreadable but I blame it on the formatting. I spent hours trying to get the indentation to work with no result. If blogger keeps doing that I will switch back to Window Live Spaces.

7 comments:

Belita said...

Interesting to know.

Anonymous said...

Good Afternoon!!! yuenyandaily.blogspot.com is one of the best informational websites of its kind. I take advantage of reading it every day. yuenyandaily.blogspot.com rocks!

Anonymous said...

Reoccur Our Intolerable Prices at www.Pharmashack.com, The Chief [b][url=http://www.pharmashack.com]Online Chemist's [/url][/b] To [url=http://www.pharmashack.com]Buy Viagra[/url] Online ! You Can also Suspect Life-and-death Deals When You [url=http://www.pharmashack.com/en/item/cialis.html]Buy Cialis[/url] and When You You [url=http://www.pharmashack.com/en/item/levitra.html]Buy Levitra[/url] Online. We Also Combine a Chief Generic [url=http://www.pharmashack.com/en/item/phentermine.html]Phentermine[/url] In subordinate of Your Regimen ! We Torment with away Pre-eminence sort [url=http://www.pharmashack.com/en/item/viagra.html]Viagra[/url] and Also [url=http://www.pharmashack.com/en/item/generic_viagra.html]Generic Viagra[/url] !

Anonymous said...

Making money on the internet is easy in the hush-hush world of [URL=http://www.www.blackhatmoneymaker.com]blackhat money[/URL], You are far from alone if you have no clue about blackhat marketing. Blackhat marketing uses little-known or misunderstood ways to produce an income online.

Anonymous said...

torebki damskie batycki
to torebki kazar sklep internetowy , torebki , kazar torebki sklep internetowy . torebki damskie włoskie , torebki online ?

Anonymous said...

unlock iphone 4
how to unlock iphone 4


unlock iphone 4 unlock iphone 4 unlock iphone 4
unlock iphone 4

how to unlock iphone 4 unlock iphone 4 [url=http://theunlockiphone4.com]unlock iphone 4 [/url] how to unlock iphone 4

Anonymous said...

top [url=http://www.c-online-casino.co.uk/]c-online-casino.co.uk[/url] hinder the latest [url=http://www.casinolasvegass.com/]free casino[/url] autonomous no set aside bonus at the best [url=http://www.baywatchcasino.com/]casino online
[/url].