题解 | #字符串合并处理#

字符串合并处理

https://www.nowcoder.com/practice/d3d8e23870584782b3dd48f26cb39c8f

import sys


def change_chr(chr):
    chr = '0x'+chr
    bnum = bin(int(chr,16))
    bnum = bnum[2:].zfill(4)
    bnum = bnum[::-1]
    xnum = hex(int('0b'+bnum,2))[2:].upper()
    return xnum

for line in sys.stdin:
    a = line.split()
    strthis = ''.join(a)

    odd = []
    even = []
    for i in range(len(strthis)):
        if i%2==1:
            odd.append(strthis[i])
        else:
            even.append(strthis[i])
    odd = sorted(odd)
    even = sorted(even)

    newstr = [odd.pop(0) if i%2==1 else even.pop(0) for i in range(len(strthis))]
    newstr = ''.join(newstr)

    outstr = []
    for i in newstr:
        if ord('0')<=ord(i)<=ord('9') or  ord('a')<=ord(i.lower())<=ord('f'):
            out = change_chr(i)
        else:
            out = i
        outstr.append(out)
    
    print(''.join(outstr).strip())

全部评论

相关推荐

牛客618272644号:佬携程工作怎么样,强度大吗
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务