题解 | #字符串排序#

字符串排序

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

Note new functions:

  • isalpha(): determine whether all English characters
  • sorted(xxx, key = str.upper): sort by uppercase
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.upper)
        # 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
全部评论

相关推荐

不愿透露姓名的神秘牛友
11-26 16:06
已编辑
快手电商 后端 23k-35k
点赞 评论 收藏
分享
我已成为0offer的糕手:别惯着,胆子都是练出来的,这里认怂了,那以后被裁应届被拖工资还敢抗争?
点赞 评论 收藏
分享
评论
2
收藏
分享
牛客网
牛客企业服务