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

字符串合并处理

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

# 处理字符串排序问题
def stsort(strings:str):
    oddlst, evenlst = [], []
    result = ''
    for i, string in enumerate(strings):
        if (i+1) % 2 == 1:
            oddlst.append(string)
        else:
            evenlst.append(string)
    oddlst.sort(key=lambda x:ord(x))
    evenlst.sort(key=lambda x:ord(x))
    if (len(oddlst) == len(evenlst)):
        for i in range(len(evenlst)):
            result += oddlst[i] + evenlst[i]
    else:
        for i in range(len(evenlst)):
            result += oddlst[i] + evenlst[i]
        result += oddlst[-1]
    return result

# 处理字符串的转换
def change(strings):
    result = ''
    for string in strings:
        new = string
        if string.isdigit() or 65 <= ord(string.upper()) <=70:
            if string.isdigit():
                new = str(bin(int(string))).replace('0b', '').rjust(4, '0')[::-1]
                new = '0b' + new
                new = int(new, 2)
            else:
                new = str(bin(ord(string.upper())-55)).replace('0b', '').rjust(4, '0')[::-1]
                new = '0b' + new
                new = int(new, 2)
            if new < 10:
                new = str(new)
            else:
                new = str(chr(new + 55))
        result += new
    return result
        

# 接收 输入的参数
while True:
    try:
        strings = input().strip().replace(' ', '')
        strings = stsort(strings)
        result = change(strings)
        print(result)
    except:
        break

全部评论

相关推荐

jack_miller:杜:你不用我那你就用我的美赞臣
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务