题解 | #字符串合并处理#
字符串合并处理
https://www.nowcoder.com/practice/d3d8e23870584782b3dd48f26cb39c8f
import string def concate(lst): con = "" for x in lst: con += x return con def my_sort(strs): lst_strs = list(strs) even = lst_strs[::2] odd = lst_strs[1::2] lst_strs[::2] = sorted(even) lst_strs[1::2] = sorted(odd) return "".join(lst_strs) def reverse(strs2): """ 先将一个该十六进制字符转换为十进制,再转换为二进制,进行反转, 将反转后的二进制字符转换为十进制,再转换为十六进制 """ res = "" hex_strs = string.hexdigits for y in strs2: if y in hex_strs: decimal_value = int(y, 16) bin_value = bin(decimal_value)[2:].rjust( 4, "0" ) # 涉及到后面的顺序反转,所以一定要补齐四位,因为16进制数对应的二进制都是四位,2的4次方=16 reversed_bin_value = bin_value[::-1] reversed_decimal_value = int(reversed_bin_value, 2) reversed_hex_value = hex(reversed_decimal_value)[2:].upper() res += reversed_hex_value else: res += y return res if __name__ == "__main__": inp = input().split() con_val = concate(inp) sorted_val = my_sort(con_val) result = reverse(sorted_val) print(result)