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

字符串合并处理

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

def reserve(str: str):
    return str[-1::-1]

def str2Hex2Bin2SortedHexStr(s: str):
    decimalism = int(s, 16)
    binary = bin(decimalism)[2:]
    if len(binary) < 4:
        binary = (4 - len(binary)) * "0" + binary
    binary = reserve(binary)
    hexStr = hex(int(binary, 2))[2:]
    if hexStr.isalpha():
        return hexStr.upper()
    else:
        return hexStr

def step1(str1: str, str2: str):
    return str1 + str2

def step2(mergeStr: str):
    strLength = len(mergeStr)
    evenIndexStr = []
    oddIndexStr = []
    for i in range(strLength):
        if i%2 != 0:    # odd digit index
            oddIndexStr.append(mergeStr[i])
        else:   # even digit index
            evenIndexStr.append(mergeStr[i])
    evenIndexStr = sorted(evenIndexStr)
    oddIndexStr = sorted(oddIndexStr)
    res = ""
    for i in range(len(evenIndexStr)):
        res += evenIndexStr[i]
        try:
            res += oddIndexStr[i]
        except Exception as e:
            # print(e)
            pass
    return res

def step3(sortedStr: str):
    str1 = "ABCDEF" 
    str2 = "abcdef"
    res = ""
    strList = list(sortedStr)
    for i in range(len(strList)):
        if (strList[i] in str1) or (strList[i] in str2) or (strList[i].isdigit()):
            if strList[i].isalpha():
                strList[i] = strList[i].lower()
            res += str2Hex2Bin2SortedHexStr(strList[i])
        else:
            res += strList[i]
    return res
str1, str2 = map(str, input().strip().split(" "))
print(step3(step2(step1(str1, str2))))

步骤相对清晰,题目分散来做,实际不难。

全部评论

相关推荐

拉丁是我干掉的:把上海理工大学改成北京理工大学。成功率增加200%
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务