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

字符串合并处理

https://www.nowcoder.com/practice/d3d8e23870584782b3dd48f26cb39c8f?tpId=37&tqId=21253&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3Fpage%3D1%26tpId%3D37%26type%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=

import sys

"""
将A,B接收
"""
s = input().split()
A, B = s[0], s[1]

# 生成新的字符串
s = A + B

# 定义2个列表分别存放奇数好偶数

oddlist = []
evenlist = []

for i in range(len(s)):
    if i % 2 == 0:
        oddlist.append(s[i])
    if i % 2 != 0:
        evenlist.append(s[i])

oddlist.sort()
evenlist.sort()


"""
将重新排序后字符串按照奇数和偶数交替的方法放入res中拼接起来
"""
s2 = ""
j = 0
for x in oddlist:
    s2 += x
    if j < len(evenlist):
        s2 += evenlist[j]
    j += 1

dict2 = {}

for i in range(10, 17):
    if i == 10:
        dict2[str(i)] = "A"
    elif i == 11:
        dict2[str(i)] = "B"

    elif i == 12:
        dict2[str(i)] = "C"

    elif i == 13:
        dict2[str(i)] = "D"

    elif i == 14:
        dict2[str(i)] = "E"

    elif i == 15:
        dict2[str(i)] = "F"


L3 = list(s2)

for i in range(len(L3)):
#替换特殊字符
    if L3[i] in "0123456789ABCDEFabcdef":
        x =int(L3[i],16)
        x =bin(x)[2:].rjust(4,"0")
        x=x[::-1]
        x=int(x,2)
        L3[i]=str(x)

#将大于10的数替换回来
for i in range(len(L3)):
    if L3[i] in dict2.keys():
        L3[i] = dict2[L3[i]]
#全部转化为字符串输出
L3 = list(map(lambda x: str(x), L3))
print("".join(L3))

全部评论

相关推荐

点赞 评论 收藏
分享
无敌虾孝子:喜欢爸爸还是喜欢妈妈
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务