发表更新7 分钟读完 (大约1071个字)
2019华为笔试题代码
之前因为赶火车,在KFC匆匆忙忙的做了点,只AC了第一题,第二题写了个开头就交卷了,第三题看了一眼,确认过眼神,是很难的题。所以后来回来后只抽空做了第二题,在这里贴一下第一题和第二题的代码做个记录。
之前没刷过算法题,也没看过算法方面的东西。。以为笔试会是安全方面的题,没想到竟然是统一的题。比较难受。代码比较渣,只求能用。
第一题
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
| import sys
string_list = []
line = sys.stdin.readline().strip() string_num = line.split(" ")[0]
for i in range(0, int(string_num)): split_string = (line.split(" ")[i + 1]) string_list.append(split_string)
def loop_helper(string_list): """ 这是一个循环处理函数,虽然时间复杂度有点高,但是我觉得应该可以循环到符合要求 :return: string_list """ divided_num_list = [] will_deleted_list = [] no_change_length = len(string_list) for k in range(0, no_change_length): if len(string_list[k]) == 8: pass elif len(string_list[k]) < 8: need_zero_num = 8 - len(string_list[k]) string_list[k] = string_list[k] + "0" * need_zero_num elif 8 < len(string_list[k]) <= 100: will_deleted_num = string_list[k] will_deleted_list.append(will_deleted_num) will_divided_group_num = int(len(string_list[k]) / 8) for m in range(0, will_divided_group_num): divided_num = string_list[k][m * 8:(m + 1) * 8] divided_num_list.append(divided_num) last_num = string_list[k][will_divided_group_num * 8:] divided_num_list.append(last_num) else: exit("string length > 100 !") if divided_num_list: for tmp1 in divided_num_list: string_list.append(tmp1) if will_deleted_list: for tmp2 in will_deleted_list: string_list.remove(tmp2) return string_list
result = loop_helper(string_list) result = loop_helper(result)
result = sorted(result)
for single_string in result: print(single_string,end=" ")
|
第二题
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
| import sys
def match_chars(char): if char == "(": return ")" elif char == "[": return "]" else: return "}"
def handle(strings): struct_info = [] for i in range(0,len(strings)): if strings[i].isdigit(): struct_info.append([i,int(strings[i]),strings[i+1]]) new_strings = "" for struct in struct_info[::-1]: start = struct[0] + 2 end = start + strings[start:].index(match_chars(struct[2])) new_strings = "" new_strings = new_strings + strings[:struct[0]] for k in range(0,struct[1]): new_strings = new_strings + strings[start:end]
new_strings = new_strings + strings[end+1:] strings = new_strings
return new_strings
line = sys.stdin.readline().strip() result = handle(line) print(result[::-1])
|
You need to set install_url
to use ShareThis. Please set it in _config.yml
.