python 笔试题

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;


  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值