6–3. 排序
(a) 输入一串数字,从大到小排列之.
(b) 跟a 一样,不过要用字典序从大到小排列之.
解答:
# -*- coding:utf-8 -*- ''' Created on 2012-7-24 @author: wanglei ''' #/usr/bin/python #Filename:6-3.py astr=raw_input("请输入一行数字,以空格分隔: ") astr=astr.split(" ") intlist=[] alist=list(astr) for i in alist: intlist.append(int(i)) intlist.sort(reverse=True) print intlist alist.sort(reverse=True) print alist
测试数据:
请输入一行数字,以空格分隔: 12 54 786 3 4
[786, 54, 12, 4, 3]
['786', '54', '4', '3', '12']
6–5. 字符串
(d)接受一个字符,在其后面加一个反向的拷贝,构成一个回文字符串.
''' Created on 2012-7-24 @author: wanglei ''' astr=raw_input("input a string: ") anotherstr=astr+astr[::-1] print anotherstr
测试数据:
input a string: china
chinaanihc