题解 | #字符串排序#

字符串排序

http://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584

Using key = str.lower is faster in speed...

while True:
    try:
        ipt = input()
        # 1. sort out all characters
        a = ''
        for i in ipt:
            # add to new string only if it is an English character
            if i.isalpha():
                a += i
        b = sorted(a, key = str.lower)
        # 2. add other non-characters
        index = 0
        c = ''
        for i in range(0, len(ipt)):
            if ipt[i].isalpha():
                c += b[index]
                index += 1
            else:
                c += ipt[i]
        print(c)
    except:
        break
全部评论

相关推荐

听说改名字就能收到offer哈:Radis写错了兄弟
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务