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.

Monday, February 12, 2007

Google Founders' Awards... another Googlegate?

Disclaimer: This is a response that I gave to Dare Obasanjo's post about the his view of the Google Founders' Awards. Dare is an engineer at Microsoft and he maintains one of the most educating and entertaining blog. Highly recommended.

While there are always people who will exploit the system for their own greeds, the idea is actually pretty neat. You award the people (and hopefully engineer) who create a successfully product with a large sum of money, just as the way you would buy from a start-up company. The amount is of course considerably less, but that is compensated by the job security the company has already guaranteed. Not a bad idea.

I don't know all of those who actually won the award but it has been given to some brilliant engineer (i.e. Niniane Wang, a formerly Softie herself). And it also gives you the impression that the company is willing to share its wealth with the employer.

One of the reason there are so much grief in MSFT is that the executives got the perks while the engineers are not earning their due. I don't know how much of it is true, but after reading a bit of Mini you get the sense that employees are treated better at Google. That's how you generate the morale in the team.

And I don't know how you compare it with the notorious Ship'it award at MSFT. But while the GFA certainly sounds better, I believe the GFA also carries some monetary values while the Ship It is just a trophy. Which one would you choose? Go figure....

Tuesday, February 06, 2007

Saving Principal Ponosov

A teacher in middle Russia is prosecuted just because he cannot afford a real Microsoft Windows...

As much I dislike raising Bill Gates in reddit yet again, I do hope that we can generate enough awareness such that Mr. Gates will take some action...

Yes, piracy is not to be discouraged...but when you live in the mountain of Ural, living off $5 US per month, how can you afford a Windows Vista! Especially when MS has raised the price so much that most ordinary folks like me cannot afford it...

The MS PR has already said that it won't intervene! Are we going to sit back and let this happen?

I posted the BlueJ post on reddit and realized that once enough people agree with you, the giant would take action. So please, I beg you, please raise some awareness to the issue.