描述: 编写一个程序,将输入字符串中的字符按如下规则排序。
规则 1 :英文字母从 A 到 Z 排列,不区分大小写。
如,输入: Type 输出: epTy
规则 2 :同一个英文字母的大小写同时存在时,按照输入顺序排列。
如,输入: BabA 输出: aABb
规则 3 :非英文字母的其它字符保持原来的位置。
如,输入: By?e 输出: Be?y
数据范围:输入的字符串长度满足 1 \le n \le 1000 \1≤n≤1000
输入:
A Famous Saying: Much Ado About Nothing (2012/8).
复制
输出:
A aaAAbc dFgghh: iimM nNn oooos Sttuuuy (2012/8).
# 1/提取所有英文大小写字符写入字母list,非大小写写入字符list;
# 2/字母记录index原始索引,转list后排序,保留key顺序为index;
# 3/遍历输入instr,遇到大小写字母则写入字母,遇到非大小写则写入字符list;
def exam(instr):
ins = instr
low = [chr(i) for i in range(97,123)]
up = [i.upper() for i in low]
low_up = low + up
# print(low_up)
# ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
# ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
sub_lu = []
sub_others = []
for i in ins:
if i in low_up:
sub_lu.append(i)
if i not in low_up:
sub_others.append(i)
# print(sub_lu)
# ['A', 'F', 'a', 'm', 'o', 'u', 's', 'S', 'a', 'y', 'i', 'n', 'g', 'M', 'u', 'c', 'h', 'A', 'd', 'o', 'A', 'b', 'o', 'u', 't', 'N', 'o', 't', 'h', 'i', 'n', 'g']
# print(sub_others)
# [' ', ' ', ':', ' ', ' ', ' ', ' ', ' ', '(', '2', '0', '1', '2', '/', '8', ')', '.']
sub_lu_index = sub_lu.index
# 没使用到这个index,一般面向于set列表去重复后再保持原始排序使用,set没有排序问题
# sub_lu_sort = sorted(sub_lu,key=sub_lu_index)
# ['A', 'A', 'A', 'F', 'a', 'a', 'm', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 's', 'S', 'y', 'i', 'i', 'n', 'n', 'g', 'g', 'M', 'c', 'h', 'h', 'd', 'b', 't', 't', 'N']
# sub_lu_sort = sorted(sub_lu,reverse=False)
# ['A', 'A', 'A', 'F', 'M', 'N', 'S', 'a', 'a', 'b', 'c', 'd', 'g', 'g', 'h', 'h', 'i', 'i', 'm', 'n', 'n', 'o', 'o', 'o', 'o', 's', 't', 't', 'u', 'u', 'u', 'y']
sub_lu_sort = sorted(sub_lu,key=str.lower)
#
# ['A', 'a', 'a', 'A', 'A', 'b', 'c', 'd', 'F', 'g', 'g', 'h', 'h', 'i', 'i', 'm', 'M', 'n', 'N', 'n', 'o', 'o', 'o', 'o', 's', 'S', 't', 't', 'u', 'u', 'u', 'y']
sub_lu_sort = sorted(sub_lu,key=str.upper)
# ['A', 'a', 'a', 'A', 'A', 'b', 'c', 'd', 'F', 'g', 'g', 'h', 'h', 'i', 'i', 'm', 'M', 'n', 'N', 'n', 'o', 'o', 'o', 'o', 's', 'S', 't', 't', 'u', 'u', 'u', 'y']
# print(sub_lu_sort)
res = ''
# print(sorted(['A','a','a','B'],key=lambda x:x[-1]))
# ['A', 'B', 'a', 'a']
# print(['A','a','a','AA'][::-1].pop())
# A
# 字符串倒序,;;-1即可,从开始到结束,循环规则为后到前-1,无需sort排序
# 此处倒序处理,主要是在遍历输入初始字符串时候,方便pop出站,出站每次出最后一个list
new_lu = sub_lu_sort[::-1]
# ['y', 'u', 'u', 'u', 't', 't', 'S', 's', 'o', 'o', 'o', 'o', 'n', 'N', 'n', 'M', 'm', 'i', 'i', 'h', 'h', 'g', 'g', 'F', 'd', 'c', 'b', 'A', 'A', 'a', 'a', 'A']
new_others = sub_others[::-1]
# ['.', ')', '8', '/', '2', '1', '0', '2', '(', ' ', ' ', ' ', ' ', ' ', ':', ' ', ' ']
# print(new_others)
for i in instr:
# 遍历初始输入instr,判断属于大小写字母,则pop存储到res
# 如不是大小写,则popothers字符,others字符也需要出站
if i in low_up:
res += new_lu.pop()
if i not in low_up:
res += new_others.pop()
print(res)
instr = input().strip()
exam(instr=instr)