您的位置:首页 > 编程语言 > Python开发

python 笔试题

2016-12-04 17:07 309 查看
1. 编写一个函数求 Fibonacci 数列第 n 项。
    fib=lambda n,x=0,y=1:x if n <2 else fib(n-1,y,x+y)

2. 编写一个函数,判定某个数 n 是否为 2 的整数次幂。
    def judge(n):

          return True if not (n>>1)&n else False

    judge=lambda n:True if not (n>>1)&n else False

3. 用 reduce 函数实现 map 函数。
    def remap(func,src):

         return reduce(lambda x,y:x+[func(y)],src,[])

4. 有一表格,长 6 步,宽 7 步。从左上角至有下角,共有多少种可能的路径?
    C(13,6) =1716

5. 实现下列函数,求给定序列中元素的指定组合:

   def combin(l, n):

       '''

       >>> combin(list(range(1, 5)), 3)

       [[1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]

       >>> list(map(lambda x: ''.join(x), combin("world", 3))))

       ['wor', 'wol', 'wod', 'wrl', 'wrd', 'wld', 'orl', 'ord', 'old', rld']

       '''

       pass

    def comb(l,n): 

         res=[]

         tmp=[0]*n 

         def fill_tmp(start=0,count=0):

             if count==n :

                 res.append(copy.copy(tmp)) 

                 return 

             for j in xrange(start,len(l)):

 if count <= n-1:

                 
tmp[count]=l[j] 

                  fill_tmp(j+1,count+1) 

         fill_tmp() 

         return res;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python