星期三, 二月 28, 2007

Code Kata:Anagram

CodeKata上有一些很值得一做的题目(并不一定需要你操一门语言写代码。有些只是design,乃至estimate)。有一个题目(Kata 6)是Anagram,就是找出一个单词表里的所有形变词,比如:

enlist inlets listen silent
boaster boaters borates
fresher refresh
sinks skins

网站作者给了一个单词表。作者说他的解答只用了25行Ruby代码,在他的1GHZ PPC上运行时间1.5s。看到这个数据我很高兴,因为我写了个27行的Python程序,运行时间0.53s。不排除我的电脑比他的快,更可能是因为Python实现本身就比Ruby实现快。幸亏前辈科学家发明了一种伟大的数据结构:散列表,而一些巨牛无比的programmer写出了质量极高的实现,我们才能用这种简洁得足以自我吹嘘的方式解决这个问题。

在blogger上贴代码,缩进老是会乱掉,我不知道有什么解决办法。为了美观起见(我认为这很重要),我把代码转成HTML放到googlepage上(3.11 Update:不久前终于弄明白了怎么贴代码,所以把代码也一块弄上来了)。我的解答:

Anagram.py

import datetime

def load_word(file_name):
file_handler = open(file_name)
table = {}
for line in file_handler.readlines():
w = line.strip(' \n').lower()
w_sorted = ''.join(sorted(w))
if w_sorted not in table:
table[w_sorted] = []
table[w_sorted].append(w)
file_handler.close()
return table


if __name__ == '__main__':
t1 = datetime.datetime.today()
output = open('result.txt','w')
anagram_sets = ['']
for word_list in load_word("wordlist.txt").values():
if len(word_list) > 1:
anagram_sets.extend(word_list)
anagram_sets.append('\n')
output.write(' '.join(anagram_sets))
t2 = datetime.datetime.today()
print str(t2 - t1)

唯一让我有点担心的是,网站作者说他给的list里面包含了2530组形变词,总共5680个单词。而我得到的结果是2531组,5683个单词。为此还专门写了一个脚本,检查我得到的结果是否都是形变词而且没有重复,好像也没什么问题。不知道是谁弄错了。

星期一, 二月 26, 2007

回校前最后一博

明天就要回学校了。今天要去把头发理一理,下一次理发最乐观估计是在五一劳动节,也有可能是暑假。
修改了一下首页的sidebar,加了一个del.icio.us的javascript,并且非常无耻地安上一个"recommended reading"的标题。del.icio.us确实考虑周到,提供了js代码和css供参考。然而这段css在blogger上明显水土不服,不管怎么调整各个margin和padding显示效应总是有些怪异,最后不得不舍弃了那个边框(自我安慰说边框和整个页面的风格不怎么协调,拿了更好)。
下学期课还是不少。都说大三下没什么课,我们系好像是个例外。统计物理,固体物理,近代物理实验(又是物理实验……我对做实验缺乏兴趣)。除此之外,课程表还塞满了各种专业选修课。就我自己来说,可能还要去听听别的专业方向的课(我报的是理论物理,相对课最少)。一转眼都已经是大三学生了。下学期也是我们在浦口的最后一学期,大四就要搬到住宿条件相对凄惨,其他条件都不错的鼓楼校区去。

星期一, 二月 19, 2007

电视剧中的量子力学

来自于李淼老师的blog。据说是从《我们无处安放的青春》里截出来的。一听这名字就觉得这电视剧很装逼。





我个人认为曾谨言老师的量子书并不适合作为旅途消遣。曾老师如果看到这个片子肯定很欣慰,电视剧里想要弄一本道具量子力学课本立马就想到了曾老师……

第二句话很实在:“也没看懂”。所有学过量子的同学都会对这句话产生共鸣。

电视剧的第一集里面还拿出一句台词的篇幅对量子力学作了一个宏观的,高屋建瓴的概括性的讨论:“在量子力学中,只有变量,没有常量。”有非常浓厚的宿命,无常之感。李老师非常善良地认为这说的是uncertainty principle。

星期四, 二月 15, 2007

Problems with latex2html

Today I tried to install latex2html on my computer and I failed after a painful struggle with the unreadble Perl Scripts.To get a latex2html package to work,first you have to check that LaTeX2e,a Perl distribution,Ghostscript interpreter(always distributed together with TeX system) and NetPbm are all OK and add the corresponding 'bin' directory to the PATH environment variable.Then download a latex2html compressed package(the latest version seems to be released in 2002) and extract it to $TeX$\localtexmf\($TeX$ is the directory your LaTeX lies).Open prefs.pm in the directory and find the line "$prefs{'PREFIX'} = '...';" Replace the content between quotes by the path where your latex2html is going to be located.Note that the path must contain no spaces.Then run 'config.bat' and 'install.bat'.You will have a nice LaTeX2HTML to enjoy.

But,in fact even you strictly follow the steps I describe above,you will probably get a failure message when running test.bat.What the hell is that?Many reasons.I suggest you go to google.com.The problem I encountered is that the netpbm version I downloaded is not portable with latex2html.I guess(yes,it still remains a postulate.I am not able to prove it) only netpbm earlier than version 10 can work with latex2html.I have made a lot of efforts in looking for a netpbm 9.xx version but still,I failed.Is there anybody who has netpbm 9.25 or earlier?

回家过年

在上海待了两个星期,这期间近乎与世隔绝:手机被盗,不能上网。离开了网络,虽然不能说什么生活崩溃了不能继续等等, 但也觉得人生不完整了。说到手机,我到上海第一天,拎着包挤地铁,人民广场站下车的时候一个不留神,手机gone with crowd,再也找不回来。明知如此,还是要聊尽人事,跑到警务处登记了一下,警察同志态度和蔼,很详细的跟我说如果想要报案要提供哪些东西,我一听,没一样是我知道的,只能化悲痛为力量,继续上路了。活了20年,人生第一次遭遇小偷,我认为2007年1月31日是小学作文题中所说的“难忘的一天”。

昨天下午回家,一路打起了十二分精神防盗。已经到了年关,无论是地铁上还是车站里都是人山人海,正是小偷猖獗时。可能是过于敏感,下了地铁我居然感觉背上的包轻了不少……这一路倒是平安,不过晚上车到天台的时候,公路上排起了长队,据说是前面出了事故,大概堵了整整一小时。司机不停的骂娘,说“要撞也要等我开过去再撞……”虽然到现在我还不知道具体出了什么事故,我也认为这个说法不怎么厚道。然而实事求是的说,车上的大多数人心里转的差不多都是这个念头,我也不例外。

后天就要过年了。