本文为原创文章,转载注明出处,欢迎关注网站https://hkvision.cn
缘起
做了个华为实习生的笔试,3道题,2小时,比字节和阿里要好点
题目
明日之星
题目名字叫明日之星,就是一个投票,输入一行人名(投给的人),然后统计谁的票数最高,输出票数最高的人的名字,然后如果有重票的,就按字母表来
由于忘了python多关键的排序咋弄的了,就写了个有点问题的
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
| import sys
if __name__ == "__main__":
line = sys.stdin.readline()
data = line.strip().split(",")
data_struct = dict()
failed = False
for vote in data:
if not vote.isalpha():
failed = True
break
if vote[0] not in "QWERTYUIOPASDFGHJKLZXCVBNM":
failed = True
break
try:
data_struct[vote] += 1
except:
data_struct[vote] = 1
if failed:
print("error.0001")
else:
res = sorted(data_struct.items(), key=lambda x: x[1], reverse=True)
if len(res) > 1 and res[0][1] == res[1][1]:
print(res[0][0] if res[0][0] < res[1][0] else res[1][0])
else:
print(res[0][0])
|
应该排序那里直接多关键字排序的,就是像这样
1
| res = sorted(data_struct.items(), key=lambda x: (x[1], -x[0]), reverse=True)
|
寄存器匹配
输入一个待匹配的寄存器,和一串寄存器的字符串,输出匹配到的寄存器的addr,mask,val
输入大概长这样
read read[addr=0x17,mask=0xff,val=0x7],read_his[addr=0xff,mask=0xff,val=0x1],read[addr=0xf0,mask=0xff,val=0x80]
输出
0x17 0xff 0x7
0xf0 0xff 0x80
还有一些其他的不重要的输出约束
我写的代码长这样
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
| """
read read[addr=0x17,mask=0xff,val=0x7],read_his[addr=0xff,mask=0xff,val=0x1],read[addr=0xf0,mask=0xff,val=0x80]
"""
import sys
import re
if __name__ == "__main__":
line = sys.stdin.readline().strip()
need_match, data = line.split(" ")
# data = data.split(",")
data = re.findall(r"\w*?\[.*?\]", data)
data_struct = dict()
for item in data:
parse = re.findall(r"(\w*?)\[addr=(\w*?),mask=(\w*?),val=(\w*?)\]", item)[0]
try:
data_struct[parse[0]].append(parse[1:])
except:
data_struct[parse[0]] = [parse[1:]]
keyword = ""
addr = ""
mask = ""
val = ""
if len(need_match.split("[")) == 1:
keyword = need_match.split("[")[0]
else:
keyword, right = need_match.split("[")
right = right[:-1]
right_struct = right.split(",")
for item in right_struct:
key, value = item.split("=")
if key == "addr":
addr = value
if key == "mask":
mask = value
if key == "val":
val = value
failed = False
try:
keyword_match = data_struct[keyword]
except:
failed = True
if not failed:
failed = True
for item in data_struct[keyword]:
match = True
if addr:
if addr.upper() != item[0].upper():
match = False
if mask:
if mask.upper() != item[1].upper():
match = False
if val:
if val.upper() != item[2].upper():
match = False
if match:
print(" ".join(item))
failed = False
if failed:
print("FAIL")
|
写的挺长的,其实后来我发现我搞复杂了,好像题目的输入只会是关键字,待匹配的寄存器不会长这样read[addr=0x77]
,但是我考虑了这些。
计算调用栈的最大大小
哇,题目描述又很复杂,大概的意思就是给你一些函数之间调用关系的数据,然后让你统计一些哪个函数调用栈的内存调用最大
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
| """
5 2 3 1 0 0
1 20 2 3
2 30 3 4 5
3 50 4
4 60
5 80
"""
import sys
res = []
if __name__ == "__main__":
line = sys.stdin.readline().strip()
funcs_num = int(line.split(" ")[0])
funcs_callnum = list(map(int, line.split(" ")[1:]))
funcs_info = dict()
# 生成一个调用矩阵
call_matrix = [[0]*funcs_num]*funcs_num
for i in range(funcs_num):
line = sys.stdin.readline().strip()
data = list(map(int, line.split(" ")))
func_name = data[0]
func_cost = data[1]
call_link = data[2:]
funcs_info[func_name] = [func_cost, call_link]
for called in call_link:
call_matrix[called-1][func_name-1] = 1
# 然后分析调用链
# 首先找入口
infuncs = []
for i in range(funcs_num):
if 1 not in call_matrix[i]:
infuncs.append(i+1)
# 分析递归
r = False
for i in range(funcs_num):
if call_matrix[i][i] == 1:
r = True
if r:
print("R")
else:
for infunc in infuncs:
find_(funcs_info, infunc)
print(max(res))
def find_(call_info, call, cost=0):
global res
call_funcs = call_info[call][1]
cost += call_info[call][0]
if not call_funcs:
res.append(cost)
return
for func in call_funcs:
find_(call_info, func, cost)
|
我这个代码有问题,我也不知道哪错了,然后时间有点不够了就稀里糊涂交卷了,20%case.
回过头来看了下,主要的问题是调用矩阵那里,不知道怎么描述,大家自行debug一下就明白了,下面是我修改后的,也有点问题,是那个递归调用的判断那里太简单,但是我不想再去想了,回过头来想想其实不要那个什么狗屁调用矩阵,直接上递归完事,每个函数假设是入口,反正不是真入口的调用链肯定比真入口的短,答案还是不会错的。
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
| """
5 2 3 1 0 0
1 20 2 3
2 30 3 4 5
3 50 4
4 60
5 80
"""
import sys
def find_(call_info, call, cost=0):
global res
call_funcs = call_info[call][1]
cost += call_info[call][0]
if not call_funcs:
res.append(cost)
return
for func in call_funcs:
find_(call_info, func, cost)
res = []
if __name__ == "__main__":
line = sys.stdin.readline().strip()
funcs_num = int(line.split(" ")[0])
funcs_callnum = list(map(int, line.split(" ")[1:]))
funcs_info = dict()
# 生成一个调用矩阵
call_matrix = [[0 for i in range(funcs_num)] for j in range(funcs_num)]
for i in range(funcs_num):
line = sys.stdin.readline().strip()
data = list(map(int, line.split(" ")))
func_name = data[0]
func_cost = data[1]
call_link = data[2:]
funcs_info[func_name] = [func_cost, call_link]
for called in call_link:
call_matrix[called-1][func_name-1] = 1
# 然后分析调用链
# 首先找入口
infuncs = []
for i in range(funcs_num):
if 1 not in call_matrix[i]:
infuncs.append(i+1)
# 分析递归
r = False
for i in range(funcs_num):
if call_matrix[i][i] == 1:
r = True
if r:
print("R")
else:
for infunc in infuncs:
find_(funcs_info, infunc)
print(max(res))
|
总结
还是不要想太多,用自己最有把握的来搞,另外,基础的一些内容还是要记住,不要总是去面向搜索引擎的编程。