前言
第一次網上筆試,被虐的很慘。一是不太習慣,最主要的是還是自己對Python的掌握,還不夠熟練。下面是這次阿里筆試相關信息
筆試時間是,2015年8月23日,10:00——12:00
對於筆試題,20道單選題,40分鍾。由於時間緊張,沒有記錄完整
對於附加題,5道題,80分鍾。答題時,沒看明白怎么答題。可能是除程序之外的內容和程序結果圖,需要截圖上傳;程序應該寫在網頁上。
我的附加題只寫了2行,程序是在附加題規定時間(80分)之外,完成的。下面是附加題及部分答案(僅供參考)。
內容
1、請盡可能列舉python列表的成員方法,並給出一下列表操作的答案:
(1) a=[1, 2, 3, 4, 5], a[::2]=?, a[-2:] = ?
(2) 一行代碼實現對列表a中的偶數位置的元素進行加3后求和?
(3) 將列表a的元素順序打亂,再對a進行排序得到列表b,然后把a和b按元素順序構造一個字典d。
2、用python實現統計一篇英文文章內每個單詞的出現頻率,並返回出現頻率最高的前10個單詞及其出現次數,並解答以下問題?(標點符號可忽略)
(1) 創建文件對象f后,解釋f的readlines和xreadlines方法的區別?
(2) 追加需求:引號內元素需要算作一個單詞,如何實現?
3、簡述python GIL的概念, 以及它對python多線程的影響?編寫一個多線程抓取網頁的程序,並闡明多線程抓取程序是否可比單線程性能有提升,並解釋原因。
4、用python編寫一個線程安全的單例模式實現。
5、請回答一下問題:
(1) 闡述一下裝飾器,描述符(property)、元類的概念,並列舉其應用場景;
(2) 如何動態獲取和設置對象的屬性。
參考答案
1、
(1)a[::2] = [1, 3, 5], a[-2:] = [4, 5]
(2)注意,下面兩種方式都有局限,如下
a = [1, 2, 3, 4, 5] print reduce(lambda x, y: x+y, [(x+3*((a.index(x)+1)%2)) for x in a]) # a中元素均不相同 # 或
print reduce(lambda x, y: x+y, [a[x]+(x+1)%2*3 for x in range(0, 5)]) # 只適用於a中元素有5個情況
(3)參考程序如下:
from random import shuffle a = [1, 2, 3, 4, 5] # 打亂列表a的元素順序
shuffle(a) # 對a進行排序得到列表b
b = sorted(a, reverse=True) # zip 並行迭代,將兩個序列“壓縮”到一起,然后返回一個元組列表,最后,轉化為字典類型。
d = dict(zip(a, b)) print d
2、統計一篇英文文章內每個單詞的出現頻率,並返回出現頻率最高的前10個單詞及其出現次數
def findTopFreqWords(filename, num=1): 'Find Top Frequent Words:' fp = open(filename, 'r') text = fp.read() fp.close() lst = re.split('[0-9\W]+', text) # create words set, no repeat
words = set(lst) d = {} for word in words: d[word] = lst.count(word) del d[''] result = [] for key, value in sorted(d.iteritems(), key=lambda (k,v): (v,k),reverse=True): result.append((key, value)) return result[:num] def test(): topWords = findTopFreqWords('test.txt',10) print topWords if __name__=='__main__': test()
使用的 test.txt 內容如下,
3.1 Accessing Text from the Web and from Disk
Electronic Books
A small sample of texts from Project Gutenberg appears in the NLTK corpus collection. However, you may be interested in analyzing other texts from Project Gutenberg. You can browse the catalog of 25,000 free online books at http://www.gutenberg.org/catalog/, and obtain a URL to an ASCII text file. Although 90% of the texts in Project Gutenberg are in English, it includes material in over 50 other languages, including Catalan, Chinese, Dutch, Finnish, French, German, Italian,
