2013年2月25日 星期一

某防毒公司的python面試題目

最近有想去找別的工作
不過能力太差,只好自己投奴隸銀行
去的幾間公司都有基本的能力測驗
基本上都不難
考題都是針對資料的操作跟基本觀念
記得某防毒公司的題目是這樣出的
有一資料結構長得如下
people_birthday = {
    "alex": "1982.04.30",
    "andrew": "2008.8.19",
    "diego": "2010/02/20",
    "fanny": "1978/11/29",
    "peggy": "1985/4/10",
}
要求如下:
1. 依月份排序資料
2. 印出完整的英文月份名稱,如一月為January,二月為Febreary
3. 印出當月生日的人數
4. 印出當月生日的人名,字首需大寫,且按字母排序
5. 依格式印出資料,格式為" 月份 (人數) 人名, 人名
6. 將內容寫入一檔案

考試時手寫的程式
回來靠印象寫了一次
發現有不少小地方有錯
改過後的版本如下
PS. 有使用內建的函數,所以程式稍稍短了一些
import calendar
import datetime

stastics = dict(enumerate([[] for x in range(len(calendar.month_name[1:]))], start = 1))

for name, birthday in people_birthday.items():
    sep = "/" if "/" in birthday else "."
    year, month, day = birthday.split(sep)
    stastics[int(month)].append(name)

with open("interview.txt", "w") as fp:
    for month_index, people in stastics.items():
        result = "%10s (%d) %s\n" % (
            calendar.month_name[month_index],
            len(people),
            ", ".join([person.capitalize() for person in sorted(people)])
        )
        print result
        fp.write(result)

個人的錯誤大概有幾個
1. 建立stastics時,不該使用[[] * len(calendar.month_name[1:])],這樣會產生多個相同內容的陣列
2. 記錯寫檔的函數,應該為write,且結尾應該分行符號"\n"

2013年2月1日 星期五

OSX Terminal中使用Home/End/Page Up/Page Down

在OSX Terminal中是無法使用Home/End/Page Up/Page Down的功能
如果很常用到,例如需要用VIM編輯文件,就得靠下面的方式去解決
首先開啟Termianl的偏好設定(通常快捷鍵為Command+,)
切換到右方的"鍵盤"設定
左方的"按鍵"列表,是代表你的按鍵組合
右方的"動作"列表,是代表你按了這個按鍵組合後,要作的動作
依下方的對應,把動作換掉即可(\033這個是按esc的動作)
Home      \033[1~
End       \033[4~
Page Up   \033[5~
Page Down \033[6~