华为海思实习生4.15机试

前言

华为今天实习开始加了笔试,原本4.1接到笔试,因为没有机器就没接,现在改为4.15参加笔试,笔试分为三道题,分值分别是:100分, 200分, 300分。时间为2个小时,每周三的19:00 —— 21:00。两个小时做完两道题的也是大佬。渣渣我一般都是一道题,但是今年华为的这题和牛客上的完全不一样,oc好艰难,后两题没有写出来,贴上大佬的代码。

第一题

做一个投票机制,评选规则:
(1)票数最多者当选
(2)票数相同时,按照员工姓名排序,首字母越小越排前,a>b>c; A>B>C 如果姓名之间有包含关系,短名字排在长名字的前面:Tom>Tomy

输入描述:

输入为投票清单,用字符串表示,投票姓名之间用英文逗号隔开,中间不含有空格,例如:Tom,Lily,Jack,Lucy

输入要求:

(1) 员工名字只包含大小写字母,首字母大写,其他字母小些。
(2)总票数n:1

示例1
输入:

Tom,Lily,Tom,Lucy,Lucy,Jack

输出:

Lucy

说明:

Tom和Lucy投票相同,Lucy排序优于Tom

示例2
输入:

Tom,Lily,Tom,Lucy,Lucy,Tom,Jack

输出:

Tom

说明:

Tom得票数最多

code:
import sys
 
def fun():
    names = sys.stdin.readline().strip().split(",")
    m = {}
 
    def checkName(name):
        first = name[0]
        last = name[1:]
        if ord('A') > ord(first) or ord('Z') < ord(first):
            return False
        for each in last:
            if ord('a') > ord(each) or ord('z') < ord(each):
                return False
        return True
 
    for each in names:
        if not checkName(each):
            print("error.0001")
            return
        if each not in m.keys():
            m[each] = 1
        else:
            m[each] += 1
 
    star = []
    max_ticket = 0
    for k in m.keys():
        if m[k] > max_ticket:
            max_ticket = m[k]
 
    for k in m.keys():
        if m[k] == max_ticket: star.append(k)
 
    star.sort()
    print(star[0])
 
if __name__ == '__main__':
    fun()


第二题

字符串处理,当时看了只有思路,看了大佬的代码,明白了。。。

输入描述:

匹配关键字,带匹配的字符串
其中两个字符串之间通过一个空格区分

输出描述:

将每一个匹配到的条目寄存器值,按照addr mask val的值先后顺序,通过空格区分输出,要包含0x或者0X且与输入保持一致。
每一个匹配的项目单独一行,换行为\r\n,最后一项也增加换行。如果匹配失败,输出fail

示例1
输入

read read[ addr=0x17,mask=0xff,val=ox7], read_his[addr=0xff,mask=0xff,val=0x1],read[addr=0xf0,mask=0xff,val=0x80]

输出:

0x17 0xff 0x7
0xf0 0xff 0x80

code:


import sys
 
def fun():
    key_word, words = sys.stdin.readline().strip().split(" ")
 
    res = []
    words = words.split("],")
    words[-1] = words[-1][:-1]
    for each in words:
        index = each.find('[')
        key = each[:index]
        each = each[index + 1:]
        if key == key_word:
            addr, mask, val = each.split(",")
            addr_info = addr[5:]
            mask_info = mask[5:]
            val_info = val[4:]
            try:
                _ = int(addr_info, base=16)
                _ = int(mask_info, base=16)
                _ = int(val_info, base=16)
                res.append([addr_info, mask_info, val_info])
            except ValueError:
                continue
    if len(res) == 0:
        print("FAIL")
    for each in res:
        print(" ".join(each))
 
 
if __name__ == '__main__':
    fun()

第三题

第三题是多叉树的最大路径和,用的回溯法,赶时间,就懒得优化了。最重要的一个测试用例是在没有入口函数的时候输出R,否则只能过80%
(这个题我都没时间做)

code:

import sys
 
 
def fun():
    def getPathSum(path):
        size = 0
        for each in path:
            size += func_info[each][0]
        return size
 
    def backtrace(path, choice):
        if len(choice) == 0:
            return getPathSum(path)
        size = 0
        for each in choice:
            if each in path:
                print('R')
                sys.exit()
            if each not in func_info.keys():
                print('NA')
                sys.exit()
            path.append(each)
            res = backtrace(path, func_info[each][1])
            size = max(size, res)
            path.pop(-1)
        return size
 
    first = sys.stdin.readline().strip().split(" ")
    first = [int(each) for each in first]
    n = first[0]
    func_info = {}
    for i in range(n):
        # build func info
        temp_func = sys.stdin.readline().strip().split(" ")
        func_num = int(temp_func[0])
        func_stack = int(temp_func[1])
        func_insert = [int(each) for each in temp_func[2:]]
        func_info[func_num] = [func_stack, func_insert]
    for each in func_info.keys():
        if each in func_info[each][1]:
            print('R')
            sys.exit()
    # 得到所有的入口函数
    entrys = []
    for each1 in func_info.keys():
        flag = True
        for each2 in func_info.keys():
            if each1 in func_info[each2][1]:
                flag = False
                break
        if flag:
            entrys.append(each1)
    if len(entrys) == 0:
        print("R")
        sys.exit()
    maxSize = 0
    for each in entrys:
        path = [each]
        stackSize = backtrace(path, func_info[each][1])
        maxSize = max(stackSize, maxSize)
    print(maxSize)
 
 
if __name__ == '__main__':
    fun()

还是要好好刷leecode,牛客上的华为108题太没有代表性了。。
参考:
【1】https://www.nowcoder.com/discuss/409163?type=2

你可能感兴趣的:(实习)