46 Simple Python Exercises 23-25题

会贴出原题和答案,答案不是最优的,也反映了我的学习过程,如果有时间会更新优化的代码。

  1. Define a simple "spelling correction" function correct() that takes a string and sees to it that 1) two or more occurrences of the space character is compressed into one, and 2) inserts an extra space after a period if the period is directly followed by a letter. E.g. correct("This   is  very funny  and    cool.Indeed!") should return "This is very funny and cool. Indeed!" Tip: Use regular expressions!
#Define a simple "spelling correction" function correct()
# that takes a string and sees to it that
# 1) two or more occurrences of the space character is compressed into one,
# and 2) inserts an extra space after a period if the period is directly followed by a letter.
# E.g. correct("This   is  very funny  and    cool.Indeed!")
# should return "This is very funny and cool. Indeed!" Tip: Use regular expressions!
import re
def correct(string):
    strinfo_a=re.compile(r'[\s\,]+')
    space=strinfo_a.sub(" ",string)
    #print(space)
    period='. '.join(space.split('.'))
    return period
a="This   is  very funny  and    cool.Indeed!"
print(correct(a))

 

  1. The third person singular verb form in English is distinguished by the suffix -s, which is added to the stem of the infinitive form: run -> runs. A simple set of rules can be given as follows:
  1. If the verb ends in y, remove it and add ies
  1. If the verb ends in o, ch, s, sh, x or z, add es
  1. By default just add s
Your task in this exercise is to define a function make_3sg_form() which given a verb in infinitive form returns its third person singular form. Test your function with words like try, brush, run and fix. Note however that the rules must be regarded as heuristic, in the sense that you must not expect them to work for all cases. Tip: Check out the string method endswith().
'''
The third person singular verb form in English is distinguished by the suffix -s,
which is added to the stem of the infinitive form: run -> runs.
A simple set of rules can be given as follows:
1.If the verb ends in y, remove it and add ies
2.If the verb ends in o, ch, s, sh, x or z, add es
3.By default just add s
Your task in this exercise is to define a function make_3sg_form()
which given a verb in infinitive form returns its third person singular form.
Test your function with words like try, brush, run and fix.
Note however that the rules must be regarded as heuristic,
in the sense that you must not expect them to work for all cases.
 Tip: Check out the string method endswith().
'''
import re
def make_3sg_form(verb):
    strinfo_a = re.compile(r'y$')
    if verb.endswith('y'):
        space = strinfo_a.sub('ies', verb)
    elif verb.endswith('sh')or verb.endswith('o')or verb.endswith('ch')or verb.endswith('s')or verb.endswith('x') or  verb.endswith('z'):
        space = verb+'es'
    else:
        space=verb+'s'
    return(space)
a='try'
print(make_3sg_form(a))
b='brush'
print(make_3sg_form(b))
c='run'
print(make_3sg_form(c))
d='fix'
print(make_3sg_form(d))

 

  1. In English, the present participle is formed by adding the suffix -ing to the infinite form: go -> going. A simple set of heuristic rules can be given as follows:
  1. If the verb ends in e, drop the e and add ing (if not exception: be, see, flee, knee, etc.)
  1. If the verb ends in ie, change ie to y and add ing
  1. For words consisting of consonant-vowel-consonant, double the final letter before adding ing
  1. By default just add ing
Your task in this exercise is to define a function make_ing_form() which given a verb in infinitive form returns its present participle form. Test your function with words such as lie, see, move and hug. However, you must not expect such simple rules to work for all cases.
'''
In English, the present participle is formed by adding the suffix -ing to the infinite
form: go -> going. A simple set of heuristic rules can be given as follows:
a.If the verb ends in e, drop the e and add ing (if not exception: be, see, flee, knee, etc.)
b.If the verb ends in ie, change ie to y and add ing
c.For words consisting of consonant-vowel-consonant, double the final letter before adding ing
d.By default just add ing
Your task in this exercise is to define a function make_ing_form()
which given a verb in infinitive form returns its present participle form.
 Test your function with words such as lie, see, move and hug.
 However, you must not expect such simple rules to work for all cases.'''
import re
def make_ing_form(verb):
    strinfo_a = re.compile(r'e$')
    strinfo_b = re.compile(r'ie$')
    strinfo_c = re.compile(r'[bcdfghjklmnpqrstvwxyz][aeiou][bcdfghjklmnpqrstvwxyz]$')
    if verb in ('be','see','flee','knee'):
        participle = verb + 'ing'
    elif verb.endswith('ie'):
        participle = strinfo_b.sub('ying', verb)
    elif verb.endswith('e') :
        participle = strinfo_a.sub('ing', verb)
    elif re.match(strinfo_c,verb):
        participle= verb+verb[-1]+'ing'
    else:
        participle=verb+'ing'
    return participle

a='lie'
print(make_ing_form(a))
b='see'
print(make_ing_form(b))
c='move'
print(make_ing_form(c))
d='hug'
print(make_ing_form(d))

 

转载于:https://www.cnblogs.com/wangchao0203/p/6561972.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值