题解 | #字符串合并处理#
字符串合并处理
http://www.nowcoder.com/practice/d3d8e23870584782b3dd48f26cb39c8f
大坑是存在左侧数据会比右侧多一个的情况,故不能用zip函数打包两个排序序列遍历,在这里磨蹭了好久
def contact_str(str1,str2):
return str1 + str2
def sort_str(strings):
result = []
arr1 = [] #奇数索引序列
arr2 = [] #偶数索引序列
for index in range(len(strings)):
if index % 2 == 0:
arr1.append(strings[index])
else:
arr2.append(strings[index])
arr1, arr2 = sorted(arr1), sorted(arr2)
p,q = 0,0
for i in range(len(strings)):
if i % 2 == 0:
result.append(arr1[p])
p += 1
else:
result.append(arr2[q])
q += 1
return result
def transfer_string(strings):
result = ''
for case in strings:
if case in '0123456789abcdefABCDEF':
result += transfer(case).upper()
else:
result += case
return result
def transfer(hex_num):
hex_num = int('0x' + hex_num, 16) # 16转10
hex_num = bin(hex_num) # 10转2
hex_num = hex_num[:2] + hex_num[2:].rjust(4,'0') #补0
hex_num = hex_num[:2] + ''.join([i for i in reversed(hex_num[2:])]) #逆序
hex_num = int(hex_num, 2)
return hex(hex_num)[2:]
while True:
try:
str1, str2 = input().split()
strings = contact_str(str1, str2)
result = sort_str(strings)
print(transfer_string(result))
except EOFError: break