题解 | #字符串合并处理#
字符串合并处理
https://www.nowcoder.com/practice/d3d8e23870584782b3dd48f26cb39c8f
import sys
# 第一步:将输入的两个字符串str1和str2进行前后合并
string1 = input().replace(' ','')
# 第二步:对合并后的字符串进行排序 下标为奇数的字符和下标为偶数的字符分别从小到大排序
odd_str = sorted(string1[1::2])
even_str = sorted(string1[::2])
combine_str = ''
for x in range(len(string1)):
if x%2 == 1:
combine_str+=odd_str[x//2]
else:
combine_str+=even_str[x//2]
# 第三步:对排序后的字符串中的'0'~'9'、'A'~'F'和'a'~'f'字符,进行转换操作。
# 如果是字母需要按照16进制转换然后转换成2进制
new_str = ''
for x in combine_str:
try:
int(x, 16)
except:
new_str+=x
continue
new_str+= str(hex(int(format(int(x, 16),'04b')[::-1],2)))[2:].capitalize()
print(new_str)