阿里巴巴集團2016校園招聘-Python工程師筆試題(附加題+部分答案)

前言

第一次網上筆試,被虐的很慘。一是不太習慣,最主要的是仍是本身對Python的掌握,還不夠熟練。下面是此次阿里筆試相關信息python

筆試時間是,2015年8月23日,10:00——12:00安全

對於筆試題,20道單選題,40分鐘。因爲時間緊張,沒有記錄完整多線程

對於附加題,5道題,80分鐘。答題時,沒看明白怎麼答題。多是除程序以外的內容和程序結果圖,須要截圖上傳;程序應該寫在網頁上。app

個人附加題只寫了2行,程序是在附加題規定時間(80分)以外,完成的。下面是附加題及部分答案(僅供參考)。dom

內容

一、請儘量列舉python列表的成員方法,並給出一下列表操做的答案:性能

(1) a=[1, 2, 3, 4, 5], a[::2]=?, a[-2:] = ?spa

(2) 一行代碼實現對列表a中的偶數位置的元素進行加3後求和?線程

(3) 將列表a的元素順序打亂,再對a進行排序獲得列表b,而後把a和b按元素順序構造一個字典d。rest

二、用python實現統計一篇英文文章內每一個單詞的出現頻率,並返回出現頻率最高的前10個單詞及其出現次數,並解答如下問題?(標點符號可忽略)code

(1) 建立文件對象f後,解釋f的readlines和xreadlines方法的區別?

(2) 追加需求:引號內元素須要算做一個單詞,如何實現?

三、簡述python GIL的概念, 以及它對python多線程的影響?編寫一個多線程抓取網頁的程序,並闡明多線程抓取程序是否可比單線程性能有提高,並解釋緣由。

四、用python編寫一個線程安全的單例模式實現。

五、請回答一下問題:

(1) 闡述一下裝飾器,描述符(property)、元類的概念,並列舉其應用場景;

(2) 如何動態獲取和設置對象的屬性。

參考答案

 一、

(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

二、統計一篇英文文章內每一個單詞的出現頻率,並返回出現頻率最高的前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, 
相關文章
相關標籤/搜索