第一题:
import re
import string
inText = raw_input('Please enter a string of text (the bigger the better): ')
outText = inText.lower()
outText = re.sub(r"[^a-z]","", outText)
count, letter = [], []
for l in string.ascii_lowercase:
n = outText.count(l)
if n != 0:
count.append(n)
letter.append(l)
map = sorted(zip(count, letter), key=lambda x:(x[0]), reverse=True)
print 'The distribution of characters in "' + inText + '" is:'
for c in map:
print c[1] * c[0]
追问求教剩下两题
会追加分数
追答自己动手学习最好。
第二题
inText = raw_input('Please enter a string of text (the bigger the better): ')
print 'The distribution of characters in "' + inText + '" is:'
outText1 = "";
for l in inText[::-1]:
outText1 += l;
print outText1
outText2 = "";
for l in inText.split(" ")[::-1]:
if outText2 == "":
outText2 = l;
else:
outText2 += " " + l;
print outText2
outText3 = "";
for l in inText.split(" "):
if outText3 == "":
outText3 = l[::-1];
else:
outText3 += " " + l[::-1];
print outText3
追问改了一下
应该没问题吧