2018校招算法工程师

抛砖引玉

2018阿里校招笔试

Question 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import numpy as np
N = 9
intersections = np.array([[0,3],[1,5],[2,7],[3,3],[4,5],[5,7],[6,9],[7,3],[8,5]])
M = 14
roads = np.array([[0,1,4],[0,7,8],[1,2,8],[1,7,11],[2,3,7],[2,5,4],[2,8,2],[3,4,9],[3,5,14],[4,5,10],[5,6,2],[6,8,6],[6,7,1],[7,8,7]])
s = 0
t = 4
def WaitTime(CurrentTime,permision):
if CurrentTime % (2*permision) in [x for x in range(permision)]: # Green light
return 0
else:
return (2*permision) - CurrentTime % (2*permision)
def CanGo(source):
NodeWithTime = {}
for i in range(len(roads[roads[:,0] == source])):
NodeWithTime[roads[roads[:,0] == source][i,1]] = roads[roads[:,0] == source][i,2]
for i in range(len(roads[roads[:,1] == source])):
NodeWithTime[roads[roads[:,1] == source][i,0]] = roads[roads[:,1] == source][i,2]
return NodeWithTime
def minTravelTime(N,intersections,M,roads,s,t):
Nodes = [s]
A = {}
A[s] = 0
for source in Nodes:
permision = intersections[source][1]
NodeWithTime = CanGo(source)
for determination in NodeWithTime.keys():
value = A[source] + WaitTime(A[source],permision) + NodeWithTime[determination]
if determination in A.keys():
A[determination] = min(value, A[determination])
else:
A[determination] = value
if determination not in Nodes:
Nodes.extend([determination])
#print Nodes
#print A
return A[t]
print minTravelTime(N,intersections,M,roads,s,t)
# 28

Question 2

1
2
3
4
5
6
7
8
9
10
def number(k):
count = 1
iteration = 1
while count < k:
count += iteration +1
iteration += 1
startposition = count - iteration
return k - startposition

2018 京东笔试

Question 1

1
2
3
4
5
6
def calc(n):
count = 1
while count < k:
count += iteration +1
iteration += 1
return iteration

80%

Question 2

brute force
20%
try

1
2
3
4
5
6
7
8
9
10
11
12
13
def calc(n):
if n == 1:
return 1
else:
sum = n*n
for a in range(2,n+1):
for b in range(1,n+1):
if (a**b < n) and (a**(b+1) > n):
for i in range(1, b+1):
sum += i*n/(b)
elif a**b == n:
sum += n
return sum

2018 didi

1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#coding = utf-8
import sys
if __name__ == "__main__":
# 读取第一行的n
n = int(sys.stdin.readline().strip())
line = sys.stdin.readline().strip()
# 把每一行的数字分隔后转化成int列表
values = map(int, line.split())
#values.sort()
value_set = set(values)
count = 0
for i in value_set:
values.remove(i)
if i in values:
count += 1
elif i == 0:
count += 1
print count

2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#coding = utf-8
import sys
if __name__ == "__main__":
# 读取第一行的n
n = int(sys.stdin.readline().strip())
if n == 1:
print 1
a = [2,3,5]
while True:
for i in range(len(a)):
for j in range(len(a)):
result = a[i] * a[j]
if result < a[-1]:
a.append(result)
a.sort()

头条

1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#coding = utf-8
import sys
if __name__ == "__main__":
# 读取第一行的n
first_line = map(int,sys.stdin.readline().strip().split())
n = first_line[0]
m = first_line[1]
c = first_line[2]
dicti = {}
for i in range(n):
# 读取每一行
line = sys.stdin.readline().strip()
# 把每一行的数字分隔后转化成int列表
values = map(int, line.split())
if len(values) > 1:
dicti[tuple(values[1:])] = i+1
else:
dicti[tuple()] = i+1
sth = {}
for i in range(1,c+1):
a = []
for keys in dicti.keys():
if i in keys:
a += dicti[keys]
sth[i] = a+a
count = 0
for key, value in sth.iteritems():
for i in range(len(value)-1):
if ((value[i]+m) % n) > value[i+1]:
count += 1
print count

2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#coding = utf-8
import sys
if __name__ == "__main__":
# 读取第一行的n
n = int(sys.stdin.readline().strip())
second_line = map(int,sys.stdin.readline().strip().split())
check_num = int(sys.stdin.readline().strip())
for i in range(check_num):
# 读取每一行
line = sys.stdin.readline().strip()
# 把每一行的数字分隔后转化成int列表
values = map(int, line.split())
count = 0
sth = second_line[values[0]-1:values[1]]
while values[2] in sth:
count += 1
sth.remove(values[2])
print count

科大讯飞

1

0.8

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
people = raw_input()
count = 0
while len(people) != 0:
if people[0] == 'L':
count += 1
people = people[1:]
print people
else:
if 'L' in people:
L_index = people.index('L')
if (L_index + 1) < (len(people) - 1):
people = 'R'+people[L_index+1:]
print people
else:
people = 'R'
print people
else:
print people
count += len(people)
break
print people
print count

2

0.4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
n = raw_input()
course = []
dicti = {}
repeat_key = []
for i in range(int(n)):
line = raw_input()
course += [line.split()]
#print 'course'+str(course)
if course[i][0] in dicti.keys():
repeat_key += [course[i][0]]
#print 'dicti[course[i][0]]'+str(dicti[course[i][0]])
#print '[course[i][1]]'+str([course[i][1]])
dicti[course[i][0]] += [course[i][1]]
else:
dicti[course[i][0]] = [course[i][1]]
#print course
#print dicti
#print repeat_key
if len(repeat_key) == 0:
print 'YES'
else:
repeat_key.sort()
for i in repeat_key:
print i +' '+ ' '.join(dicti[i])

1
2
3
4
5
6
5
01 204521
23 204523
22 204526
01 204528
22 204527
1
2
3
4
3
11 204521
23 204522
43 204531

3

1
2
3
4
5
n = raw_input()
name = []
for i in range(int(n)):
group = raw_input()
name += [group]

souhu

1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
f_l = raw_input()
arr = f_l.split('/')
path = []
for i in range(len(arr)):
if arr[i] == '.':
pass
elif arr[i] == '..':
path.pop()
else:
path += [arr[i]]
sth = '/'.join(path)
sth = sth[:-1]
print sth

2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
f_l = raw_input()
n = int(f_l)
s_l = raw_input()
arr = map(int, s_l.split(' '))
# print s_l
#print arr
count = 0
while len(arr) > 1:
if arr[0] < arr[-1]:
count += arr[0]*2
#print count
arr.pop(0)
elif arr[0] > arr[-1]:
count += arr[-1]*2
#print count
arr.pop(-1)
else:
count += arr[0]*2
#print count
arr.pop(0)
arr.pop(-1)
if len(arr) == 0:
print count
else:
print count+arr[0]

美的

1 文件中不重复的url

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def get_next_target(page):
start_link = page.find('http')
if start_link == -1:
return None, 0
start_quote = page.find('http', start_link)
end_quote = page.find('http', start_quote + 1)
url = page[start_quote + 1:end_quote]
return url, end_quote
def union(a, b):
if b not in a:
a.append(b)
def get_all_links(page):
links = []
while True:
url, endpos = get_next_target(page)
if url:
union(links,url)
page = page[endpos:]
else:
break
return links
file_path = "G:\intro to DA\Data Wrangling with MongoDB - Udacity.html"
HtmlFile = open(file_path, 'r')
context = HtmlFile.read() # type: string
links = get_all_links(context)
HtmlFile.close()
urlfile = open('URL.txt', 'w')
for url in links:
url.write("%s\n" % item)
urlfile.close()

2 实现前向传播算法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import numpy as np
def layer_sizes(X, Y):
"""
Arguments:
X -- input dataset of shape (input size, number of examples)
Y -- labels of shape (output size, number of examples)
Returns:
n_x -- the size of the input layer
n_h -- the size of the hidden layer
n_y -- the size of the output layer
"""
n_x = X.shape[0] # size of input layer
n_h = 4
n_y = Y.shape[0] # size of output layer
return (n_x, n_h, n_y)
def initialize_parameters(n_x, n_h, n_y):
"""
Argument:
n_x -- size of the input layer
n_h -- size of the hidden layer
n_y -- size of the output layer
Returns:
params -- python dictionary containing your parameters:
W1 -- weight matrix of shape (n_h, n_x)
b1 -- bias vector of shape (n_h, 1)
W2 -- weight matrix of shape (n_y, n_h)
b2 -- bias vector of shape (n_y, 1)
"""
np.random.seed(2) # we set up a seed so that your output matches ours although the initialization is random.
W1 = np.random.randn(n_h,n_x) * 0.01
b1 = np.zeros((n_h,1))
W2 = np.random.randn(n_y,n_h) * 0.01
b2 = np.zeros((n_y,1))
assert (W1.shape == (n_h, n_x))
assert (b1.shape == (n_h, 1))
assert (W2.shape == (n_y, n_h))
assert (b2.shape == (n_y, 1))
parameters = {"W1": W1,
"b1": b1,
"W2": W2,
"b2": b2}
return parameters
def forward_propagation(X, parameters):
"""
Argument:
X -- input data of size (n_x, m)
parameters -- python dictionary containing your parameters (output of initialization function)
Returns:
A2 -- The sigmoid output of the second activation
cache -- a dictionary containing "Z1", "A1", "Z2" and "A2"
"""
# Retrieve each parameter from the dictionary "parameters"
W1 = parameters["W1"]
b1 = parameters["b1"]
W2 = parameters["W2"]
b2 = parameters["b2"]
# Implement Forward Propagation to calculate A2 (probabilities)
Z1 = np.dot(W1, X) + b1
A1 = np.tanh(Z1)
Z2 = np.dot(W2, A1) + b2
A2 = sigmoid(Z2)
assert(A2.shape == (1, X.shape[1]))
cache = {"Z1": Z1,
"A1": A1,
"Z2": Z2,
"A2": A2}
return A2, cache
n_x = layer_sizes(X, Y)[0]
n_y = layer_sizes(X, Y)[2]
# Initialize parameters, then retrieve W1, b1, W2, b2. Inputs: "n_x, n_h, n_y". Outputs = "W1, b1, W2, b2, parameters".
parameters = initialize_parameters(n_x, n_h, n_y)
W1 = parameters["W1"]
b1 = parameters["b1"]
W2 = parameters["W2"]
b2 = parameters["b2"]
A2, cache = forward_propagation(X, parameters)

3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
```
# Face ++
25%
改进未完成
```python
f = True
while f:
s_n = raw_input()
n = int(s_n)
if n == -1:
break
else:
s = 0
s_f = 0.0
for i in range(n):
s1 = raw_input()
if '.' in s1:
s_li = s_f.split('.')
s_l, s_r = long(s_li[0]), long(s_li[1])
li = s1.split('.')
left, right = long(li[0]), long(li[1])
lll = s_l + left
rrr = s_r + right
n1 = float(s1)
else:
n1 = int(s1)
s += n1
print s

baidu

1

当时未完成

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
s_n = raw_input()
n = int(s_n)
def f(n):
lis = [2,3,5,7]
lis_d = []
for i in range(1,6):
lis_i = []
for j in range(10**i,10**(i+1)):
lis_ = lis[:]
fl = []
for k in lis_:
if j % k == 0:
fl.append(True)
break
if True not in fl:
lis_i.append(j)
lis.append(j)
#print 'lis_i: ' + str(lis_i)
lis__ = lis_i[:]
if len(lis__) > 0:
for ii in lis__:
s_ii = str(ii)
r_s_ii = s_ii[::-1]
r_ii = int(r_s_ii)
if r_ii != ii and r_ii in lis__:
lis_d.append(ii)
if len(lis_d) == n:
return lis_d
print f(n)

小红书

1

90%

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
s = raw_input()
f = True
num = len(s) / 3
ss = s.replace("RED", "", num)
li = '1234567890'
max_n = -1
s_n = ''
for i in range(len(ss)):
si = ss[i]
if si in li:
f = False
s_n += si
n = int(s_n)
if n > max_n:
max_n = n
else:
s_n = ''
if max_n > 0:
print max_n
if f:
print -1

2

stay stuned.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
s = raw_input()
mo = s_mo.split(' ')
n_mo = map(int, mo)
a,b,n = n_mo[0], n_mo[1], n_mo[2]
ss = ['r','e','d']
ite = ss[:]
for i in range(n):
l_re = []
for j in ss:
for k in ite:
re = j + k
l_re.append(re)
ss = l_re[:]
#judge
for i in range(len(ss)):
ch = ss[i]
ch.replace('r','1',len(ss))
ch.replace('e','0',len(ss))
ch.replace('d','0',len(ss)

3

80%

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
s_n = raw_input()
n = int(s_n)
s_mo = raw_input()
mo = s_mo.split(' ')
n_mo = map(int, mo)
# print n_mo
s_sum = raw_input()
sum_ = int(s_sum)
li = n_mo[:]
count = 1
if sum_ in li:
print 1
else:
while min(li) < sum_:
l_re = []
for i in li:
for j in n_mo:
re = i + j
#if re not in li:
l_re.append(re)
li = l_re[:]
count += 1
if sum_ in li:
print count
break
if min(li) > sum_:
print -1

宜信

1

少了半小时答题,差一点就搞定了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
s = raw_input()
mo = s.split(' ')
n_mo = map(int, mo)
N, W = n_mo[0], n_mo[1]
if W >= 1:
li = []
for i in range(N):
s_i = str(i)
li.append(s_i)
itr = li[:]
for j in range(W-1):
l_re = []
for k1 in li:
for k2 in itr:
if k1 != k2:
re = k1 + k2
l_re.append(re)
li = l_re[:]
print (W**N - len(li))

新浪面试

一面:

主要招的是视频和图像方向的

  1. 自我介绍
  2. 做的项目
  3. CNN权值
  4. CNN介绍
  5. kmeans GMM 区别
  6. 物体运动快慢
  7. 视频编解码

美团

1

2

10%

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
s = raw_input()
s = s.split(' ')
s_n, s_m = s[0], s[1]
c = len(s_n) / 10
c_m = len(s_n) % 10
if c == 0:
n = int(s_n)
if c > 0:
su = 0
for k in range(c):
for kk in range(-1, -11, -1):
tem = int(s_n[kk+10*k]) * 10**(-(kk+10*k)-1)
#print tem
su = su + tem
for kk_ in range(-1, -c_m, -1):
tem = int(s_n[kk_+10*c]) * 10**(-(kk_+10*c)-1)
su = su + tem
n = su
m = int(s_m)
#print n, m
l = [str(x) for x in range(1,n+1)]
ll = ''.join(l)
#print ll
for i in range(m):
num = raw_input()
p = ll.find(num)
ll = num + ll[:p] + ll[p+1:]
#print ll
for j in ll:
print j

深信服科技校园招聘研发试题–卷B

1

50%

1
2
3
4
5
s = raw_input()
a = s.decode('unicode_escape')
a = a[1:-1]
print a

2

25%

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
s = raw_input()
def f(s, set_i_o):
set_00 = s + set_i_o[0]
set_01 = set_i_o[1]
set_10 = set_i_o[0]
set_11 = set_i_o[1] + s
return (set_00, set_01), (set_10, set_11)
c = 0
arr = [('','')]# * 2**(c)
while c < len(s):
l = len(arr)
for i in range(l):
a, b = f(s[c], arr[i])
arr += [a]
#print arr
arr += [b]
arr = arr[2**c:]
#print arr
c += 1
o = [None] * len(arr)
for i in range(len(arr)):
o[i] = arr[i][1]+arr[i][0]
s_o = set(o)
for j in s_o:
print j
```
# 人人
## 1
AC
```python
n = int(raw_input())
print (n-1)*n*2 - (n-1)*n + 1

2

50%

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
s = raw_input()
arr = map(int, s.split(' '))
n,m,k = arr[0], arr[1], arr[2]
#print n,m,k
l = []
for i in range(k):
tem = raw_input()
arr = map(int, tem.split(' '))
#print arr
arr = tuple(arr)
l.append(arr)
#print arr,l
a = sorted(l, key=lambda t: t[0])
b = sorted(l, key=lambda t: t[1])
#print a, b
if a == b:
p = ''
if (1,1) not in a:
a = [(1,1)] + a
if (n,m) not in a:
a = a + [(n,m)]
for i in range(len(a)-1):
nd = a[i+1][0] - a[i][0]
nr = a[i+1][1] - a[i][1]
p += 'D'*nd
p += 'R'*nr
print p
else:
print "Impossible"

3

浪潮

一面

  1. 自我介绍
  2. 突出点
  3. 性格
    明天之前给二面消息,三天内给offer