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

字符串合并处理

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())

全部评论

相关推荐

05-14 20:34
门头沟学院 Java
窝补药贝八股:管他们,乱说,反正又不去,直接说680
点赞 评论 收藏
分享
程序员饺子:正常 我沟通了200多个 15个要简历 面试2个 全投的成都的小厂。很多看我是27直接不会了😅
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务