题解 | #字符串合并处理#
字符串合并处理
https://www.nowcoder.com/practice/d3d8e23870584782b3dd48f26cb39c8f
input = ''.join(input().split(' ')) ji = [] ou = [] for index in range(len(input)): if index%2==0: ou.append(ord(input[index])) else: ji.append(ord(input[index])) ji_sorted = sorted(ji) ou_sorted = sorted(ou) new_str = [] for index in range(len(input)): if index%2==0: new_str.append(chr(ou_sorted.pop(0))) else: new_str.append(chr(ji_sorted.pop(0))) res = [] for item in new_str: if item.isdigit(): # 这里的重点16进制用二进制表示最大要用四位,转换二进制时,不足四位的要补足四位 res.append(hex(int(bin(int(item))[2:].rjust(4,'0')[::-1],base=2))[2:].upper()) elif item.isalpha() and item.lower() in ['a','b','c','d','e','f']: res.append(hex(int(bin(int(item,base=16))[2:].rjust(4,'0')[::-1],base=2))[2:].upper()) else: res.append(item) print(''.join(res))#字符串合并处理#