题解 | #字符串合并处理#
字符串合并处理
http://www.nowcoder.com/practice/d3d8e23870584782b3dd48f26cb39c8f
本题有点繁琐,主要在于对数字和字符的转换,除了a~f之外的字符不用处理:代码如下:
def num(i):
if i>='0' and i<='9':
n=int(i)
# print(n)
elif i >= 'A' and i <= 'F':
n = int(ord(i) - 55)
elif i >= 'a' and i <= 'f':
n=int(ord(i) - 87)
else:
return i
m=list(bin(n)[2:])
m.reverse()
if len(m) < 4:
m.extend(['0']*(4-len(m)))
# print(m)
k=int(''.join(m),2)
if k<=9:
return str(k)
if k>=10:
return chr(ord(hex(k)[2])-32)
return k
while True:
try:
list0 = list(input())
# print(list0)
for i in range(len(list0)):
if list0[i] ==' ':
index=i
list0.pop(index)
# print(list0)
list1 = []
list2 = []
# print(list1)
# print(list2)
for i in range(len(list0)):
if i % 2 == 0:
list1.append(list0[i])
else:
list2.append(list0[i])
list1.sort()
list2.sort()
i=0
j=0
list3=[]
while i<len(list1) and j<len(list2):
list3.append(list1[i])
list3.append(list2[j])
i+=1
j+=1
if i <len(list1):
list3.append(list1[i])
# print(list3)
for i in range(len(list3)):
list3[i]=num(list3[i])
print(''.join(list3))
except:
break
if i>='0' and i<='9':
n=int(i)
# print(n)
elif i >= 'A' and i <= 'F':
n = int(ord(i) - 55)
elif i >= 'a' and i <= 'f':
n=int(ord(i) - 87)
else:
return i
m=list(bin(n)[2:])
m.reverse()
if len(m) < 4:
m.extend(['0']*(4-len(m)))
# print(m)
k=int(''.join(m),2)
if k<=9:
return str(k)
if k>=10:
return chr(ord(hex(k)[2])-32)
return k
while True:
try:
list0 = list(input())
# print(list0)
for i in range(len(list0)):
if list0[i] ==' ':
index=i
list0.pop(index)
# print(list0)
list1 = []
list2 = []
# print(list1)
# print(list2)
for i in range(len(list0)):
if i % 2 == 0:
list1.append(list0[i])
else:
list2.append(list0[i])
list1.sort()
list2.sort()
i=0
j=0
list3=[]
while i<len(list1) and j<len(list2):
list3.append(list1[i])
list3.append(list2[j])
i+=1
j+=1
if i <len(list1):
list3.append(list1[i])
# print(list3)
for i in range(len(list3)):
list3[i]=num(list3[i])
print(''.join(list3))
except:
break