华为 笔试
前两题比较简单,就不说了。
第三题是求所有依赖循环的,一开始是想做DFS找有向图中的所有环的,但是浪费了很长时间没写出来,后来就打算用生成式做一个递归。
只写了个思路,后面没debug,所以得了0分。
然后结束后自己debug了一下,也不知道能过多少测试样例了,就提供下自己的思路吧
#华为笔试##笔试题目##华为#class Solution: def findpath(self, relys_dict, start_file, stop_file): if start_file in relys_dict: for file in relys_dict[start_file]: if file != stop_file: for f in self.findpath(relys_dict, file, stop_file): res = [start_file] + f yield res else: yield [start_file, stop_file] if __name__ == '__main__': res = ['a.h:b.h c.h d.h','b.h:c.h d.h','c.h:d.h','d.h:a.h', 'headfile is :a.h'] headfile = res[-1].split(':')[-1] relyfile = res[:-1] relyfile = [x.split(':') for x in relyfile] relydict = dict(relyfile) for k in relydict: relydict[k] = relydict[k].split(' ') a = Solution() flag = False for x in a.findpath(relydict, headfile, headfile): if x[-1] == headfile: flag = True break if not flag: print('none loop include', headfile) else: print('Bad coding -- loop include as bellow:') for x in a.findpath(relydict, headfile, headfile): if x[-1]==headfile: for i in range(len(x)-1): if i != len(x)-2: print(x[i], end = ' ') else: print(x[i])