今日头条2018春季 算法实习生编程题 python

1.

#lst 为n个整数,k为差值

def diff(lst, k):

   newlist = [i + k for i in lst]

   return len(set(lst) & set(newlist))

 

2.

 

3.

#n为算式的结果,eval()可以将字符串算式转化为算式结果

def draw(n):

   lib = ["66666", "6...6", "....6","6...."]

   dic = {}

   target = str(n)

   length = len(target)

   res = ""

   dic[0] = [0,1,1,1,0]

   dic[1] = [2,2,2,2,2]

   dic[2] = [0,2,0,3,0]

   dic[3] = [0,2,0,2,0]

   dic[4] = [1,1,0,2,2]

   dic[5] = [0,3,0,2,0]

   dic[6] = [0,3,0,1,0]

   dic[7] = [0,2,2,2,2]

   dic[8] = [0,1,0,1,0]

   dic[9] = [0,1,0,2,0]

   for i in range(5):

       for j in range(length):

           num = int(target[j])

           if j<length-1:

                res +=lib[dic[num][i]]+".."

           else:

                res += lib[dic[num][i]]

       print(res)

       res = ""

4. 

def put(la,lb):

   m1 = sum(la)/len(la)

   m2 = sum(lb)/len(lb)

   if m1==m2: return False

   if m1>m2:

       for i in range(len(la)):

           if la[i]<m1 and la[i]>m2 and la[i] not in lb:

                lb.append(la[i])

                la.pop(i)

                return True

       return False

   if m1<m2:

       for i in range(len(lb)):

           if lb[i]<m2 and lb[i]>m1 and lb[i] not in la:

                la.append(la[i])

                lb.pop(i)

                return True

       return False

 

count = 0

#a,b分别为两个数组

a = [1,2,5]

b = [2,3,4,5,6]

while True:

   if put(a,b):

       count += 1

    else:

       print(count)

       break

 

5.

def jump(height, ban):
   
return height+ 2 * (ban - height)
k=
3
h=2
height = 0 #此时高度
l=[1,3,6]
l=
sorted(l)
for i in range(k):
   
cha = []
    for j in l:
       
if 0<j-height<=h:
           
cha.append(j)
    ban = max(cha)
   
height = jump(height, ban)
print(height)

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值