优化程序结构 分类: divide into python python 小练习 2013-12-30 20:53 229人阅读 评论(0) 收藏
优化程序结构,增加程序的可读性
#coding:utf-8
import re
def match_sxz(noun):
return re.search('[sxz]$',noun)
def apply_sxz(noun):
return re.sub('$','es',noun)
def match_h(noun):
return re.search('[^adioudgkprt]h$',noun)
def apply_h(noun):
return re.sub('$','es',noun)
def match_y(noun):
return re.search('[^aeiou]y$',noun)
def apply_y(noun):
return re.sub('y$','ies',noun)
def match_default(noun):
return 1
def apply_default(noun):
return noun +'s'
rules = [(match_sxz,apply_sxz),
(match_h,apply_h),
(match_y,apply_y),
(match_default,apply_default)
]
def plural(noun):
for matchRule,applyRule in rules:
if matchRule(noun):
return applyRule(n)
if __name__ == '__main__':
plural(noun)
版权声明:本文为博主原创文章,未经博主允许不得转载。