练习9-1
fin = open('C://words.txt')
for line in fin:
word = line.strip()
if len(word) >20:
print word
练习9-2
统计字母表中含有e的单词占有的百分比
fin = open('C://words.txt')
counter1 = 0
counter2 = 0
def has_no_e(wo):
for i in range(len(wo)):
if wo[i-1]=='e':
return True
for line in fin:
word = line.strip()
flag = has_no_e(word)
counter1=counter1+1
if flag == True:
counter2=counter2+1
counter1=float(counter1)
counter2=float(counter2)
print 100-counter2/counter1*100
练习9-3
利用下标循环实现输入文字中是否存在禁用字母的鉴别
def avoids(word,forbiden):
for i in range(len(forbiden)):
for j in range(len(word)):
if word[j-1]==forbiden[i-1]:
return False
return True
print 'Plesase input forbiden alphabet:'
ban=raw_input('>')
print 'Please input desired word:'
test=raw_input('>>')
print avoids(test,ban)
练习9-4
检测是否是由可用字符组成的
def use_only(word,avaliable):
for i in range(len(word)):
flag=0
for j in range(len(avaliable)):
if word[i-1]==avaliable[j-1]:
flag =1
if flag==0:
return False
return True
print 'Plesase input avaliable alphabet:'
ban=raw_input('>')
print 'Please input desired word:'
test=raw_input('>>')
print use_only(test,ban)
练习9-5
检测alphebat是否被全部使用
def uses_all(word,avaliable):
counter=0
for i in range(len(avaliable)):
flag=0
for j in range(len(word)):
if word[j-1]==avaliable[i-1]:
flag =1
if flag==0:
return False
else:
counter=counter+1
if counter == len(avaliable):
return True
else:
return False
print 'Plesase input avaliable alphabet:'
ban=raw_input('>')
print 'Please input desired word:'
test=raw_input('>>')
print uses_all(test,ban)
练习9-6
def is_abecedarian():
fin = open ('C://words.txt')
counter=0
for line in fin:
flag = 0
for i in range(len(line)-1):
if line[i]<=line[i+1]:
flag=flag+1
if flag==len(line)-2:
counter=counter+1
return counter
print 'The number of abecedarian words in directionary is'
print is_abecedarian()
练习9-7
QNMD车迷天下。。。。不做了