美文网首页pythonpython3
阿里巴巴集团2016校园招聘-Python工程师笔试题(附加题+

阿里巴巴集团2016校园招聘-Python工程师笔试题(附加题+

作者: 氨基钠 | 来源:发表于2018-07-19 14:04 被阅读146次

    内容

    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个情况
    print sum(map(lambda x: x[1]+(x[0]+1)%2*3, enumerate(a)))
    print sum([a[i]+3 if i%2==0 else a[i] for i in range(len(a))])#最简洁
    

    (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,
    

    相关文章

      网友评论

        本文标题:阿里巴巴集团2016校园招聘-Python工程师笔试题(附加题+

        本文链接:https://www.haomeiwen.com/subject/dektmftx.html