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)
"""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
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.