【题目详见:https://www.cnblogs.com/uncleyong/p/12026042.html】
对字符串“2kd4-1124*2|^2sdAmZ%fkMcv”排序,并返回符合要求格式的元组数据。
排序规则:按照ASCII码由大到小排序;
返回数据格式:((最大字符,最小字符),(次大字符,次小字符),,,)
s = '2kd4-1124*2|^2sdAmZ%fkMcv' def fun(s): li = [] li2 = [] tu = () for i in s: li.append(i) print('排序前:',end='') print(li) n = len(li) for i in range(n-1): for j in range(n-1-i): if li[j]<li[j+1]: li[j],li[j+1] = li[j+1],li[j] if n%2==0: for i in range(n//2): li2.append('({},{})'.format(li[i],li[n-1-i])) else: for i in range(n//2): li2.append('({},{})'.format(li[i], li[n-1-i])) li2.append('({})'.format(li[n//2])) print('排序后:',end='') print(li) return tuple(li2) tu = fun(s) print('新格式:',end='') print(tu)