题解 | #字符串合并处理#
字符串合并处理
https://www.nowcoder.com/practice/d3d8e23870584782b3dd48f26cb39c8f
import sys
inputS = sys.stdin.readline().strip()
s = inputS.split(' ')
join = s[0] + s[1]
even = sorted(join[::2])
odd = sorted(join[1:len(join):2])
sortedjoin = ''
for i in range(len(odd)+len(even)):
if i%2 == 0:
sortedjoin += even[0]
if len(even) >1:
even = even[1:]
else:
sortedjoin += odd[0]
if len(odd) > 1:
odd = odd[1:]
ans = ''
for s in sortedjoin:
if ord('0') <= ord(s) <= ord('9') or ord('a')<=ord(s)<=ord('f') or ord('A') <= ord(s) <= ord('F'):
s = bin(int(s, 16))[2:]
if len(s) < 4:
s = '0'*(4-len(s)) + s
s = s[::-1]
s = hex(int(s, 2))[2:]
if ord('a') <= ord(s) <= ord('f'):
s = chr(ord(s)+(ord('A') - ord('a')))
ans += s
print(ans)
这次直接用python内置函数bin()和hex()进行进制转化。注意合并奇偶位时不要数组越界。另外,在对特定字符串做进制转换时,需注意和不做转换的字符串保持相同命名,否则后者就会找不到加入ans中的未做转换的字符串。
