Bruce Lee random walk

Test

1 Baidu

选择题:单选、多选

选择题考查的范围非常广泛,包括C++、Java语法、算法、数据结构、网络知识、数据库、MapReduce、机器学习、深度学习等。但是都不是太深。

简答题

采样算法在机器学习的重要性

设计题

深度学习在NLP的应用

编程题

下面的程序在Python3.5环境下测试通过(百度笔试采用的赛码平台依然采用Python2.7)

素数相关的问题

# 素数检验
def is_prime(n):
    if n < 2:
        return False
    for i in range(2, int(n**0.5 + 1)):
        if n % i == 0:
            return False
    return True

# 双素数检验
def is_doubleprime(n):
    m = int(str(n)[::-1])                             # 数字反转
    return n != m and is_prime(n) and is_prime(m)

# 第K个素数
def prime_by_num(k):
    assert(1<=k<=10000)
    
    num = 1
    count = 0
    while 1:
        num += 1
        if is_prime(num):
            count +=1
            
        if count == k:
            return num
        
# 第K个双素数
def doubleprime_by_num(k):
    assert(1<=k<=10000)
    
    num = 1
    count = 0
    while 1:
        num += 1
        if is_doubleprime(num):
            count +=1
            
        if count == k:
            return num

2 Didi

智商题

包含一些传统的智力测试,如寻找给定数列的规律,排列组合,还有25匹马5个赛道选出前三名或前五名最少需要几场比赛等。

技术题

考察了机器学习的一些基础知识,当然在深度学习如火如荼的当下,也考察了一些相关知识,如卷积核的维数,RNN的序列建模,激活函数及其导数,优化,正则化等

编程题

下面的程序在Python3.5终端环境下测试通过(滴滴笔试采用的牛客网当然也可以采用Python2.7)

字符串压缩的问题

从示例来看,下面的encode函数存在一些问题,测试覆盖率40%

import sys

#'aaa' => 3[a] => 3
#'aaaaa' => 5[a] => 4
#'aabcaabcd' => 2[aabc]d => 8
#‘ababeababe’ => 2[ababe] => 2[2[ab]e] => 9

def encode(string):
    l = len(string)
    first = string[0]
    idx = string.find(first, 1)
    if idx == -1:
        return string
    else:
        num = string.count(string[:idx])
        encoded_str = str(num) + '[' + string[:idx] + ']' + string[idx * num:]
        encode(string[:idx])
            
    return encoded_str

if __name__ == "__main__":
    string = sys.stdin.readline().strip()
    string = ''.join(string.split())
    string = string.lower()
    assert(string != '' and len(string) <= 160)
    
    encoded_string = encode(string)
    length_encoded = len(encoded_string)
    length_original = len(string)
    if length_encoded >= length_original:
        length = length_original
    else:
        length = length_encoded    
    
    print(length)

3 Toutiao

最大子矩阵和

#include <stdio.h>  
#include <string.h>  
  
#define N 102  
  
int main(){  
    int a[N][N];  
    int k,i,j,n,sum,maxx,i2,i1;  
  
    while(scanf("%d",&n)!=EOF){  
  
        for(i=0;i<=n;++i)a[0][i]=a[i][0]=0;  
        for(i=1;i<=n;++i){  
            for(j=1;j<=n;++j){  
                scanf("%d",&a[i][j]);  
                a[i][j]+=a[i-1][j];//这里就是矩阵预处理即列进行累加和  
            }  
        }  
        maxx = -129;  
        for(i1=0;i1<n;++i1){//子矩阵的行的上边界  
            for(i2=i1+1;i2<=n;++i2){//子矩阵的行的下边界  
                sum = 0;  
                for(k=1;k<=n;++k){//转化为n个元素中找最大连续子序列的问题每个元素即为a[i2][k]-a[i1][k]  
                    if(sum<=0)sum = (a[i2][k]-a[i1][k]);  
                    else sum += (a[i2][k]-a[i1][k]);  
  
                    if(sum>maxx)maxx = sum;  
                }  
            }  
        }  
        printf("%d\n",maxx);  
    }  
    return 0;  
}      

推盒子最短步数

问题:人的初始位置S,盒子初始位置0,盒子期望位置E

示例:

..S#..
..#.E.
...0..

提示:类似贪吃蛇游戏

旅馆问题

有n个房间,假设将第i个房间人员全部清出,然后从i+1,i+2,…,开始依次每间房安排一个人进去,最后一个房间后面为第一个房间,即取模循环,直到将最后一个人安排到房间中去。

# input(房间数,最后一个人入住房号;调整后的人数): 3 1
			6 5 1
# output(调整前的人数): 4 4 4

import sys

line = list(map(int, sys.stdin.readline().strip().split()))
n, x = line[0], line[1]
# assert of n, 1<=x<=n
a = list(map(int, sys.stdin.readline().strip().split()))
# assert of a_i

pre_a = list([0 for _ in range(len(a))])
x = x - 1
i = 0
while a[(x -i) % n] != pre_a[(x - i) % n]:
    a[(x -i) % n] -= 1
    pre_a[(x -i) % n] += 1
    i += 1	
print(pre_a)

魔方最大完美度

问题:完美度为魔方中一个面的四个数相加的和

思路:将魔方按一定方式(上-左-中-右-下-下)展开(对应数字是按行排列的,输入6个面总共24个数字),然后按此顺序提取每个面的四个数字,求和组成一个序列,然后问题就成为经典的最大子序列和问题了。