Homework Assignment Number 8
- Write a function that add er
to the end of a word as follows:
- If the last letter of the word is y, change the last letter to an i before adding er
- Double the last letter before adding er if:
- the last letter is b,d,g,l,m,n,p,r,s
or t
- and the second to last letter is a vowel
- and the word is 2 letters long or the third to last letter is
not a vowel
- If the last letter of the word is e, just add an r instead of er
- Othewise just add er without changing the word
- Assume that there is a single parameter for your function, the
word that you want to add er
to.
- Use regular expressions to identify the different cases.
- Examples: bid → bidder, eat →
eater, sin → sinner, silly → sillier, sprint → sprinter, bike → biker
- Note: The above rules may not correctly handle all of these
examples. See grading criteria below regarding exceptions
Grading Criteria
- Does your programs work?
- Does it solve the problem?
- Do you use regular expressions?
- Is your code clearly written?
- Is it elegant or clever?
- Optionally, change rules to handle exceptions: coy → coyer, dicey → dicier, dopey → dopier
- Please make a note using a comment (to make it clear that you
are intentionally varying from the instructions)
Note: You can ask any questions by email or in person and I will
give you hints if you are having trouble.
What you Should remember:
- re.search(pattern,string)
- Will return nothing if no pattern is found
- This is treated like False, if used as a boolean expression
- If a pattern is found, a match object will be returned.
- There are 3 slots of that object:
- pattern.group(0) is the matched substring
- pattern.start() is the starting position of that substring
- pattern.end() is the ending position
- You need to use the square brackets to indicate a list of
possible letters.
- [abcd] means a or b or c or d
- You need to use $ to represent the end of the word.
- You need to create new a new string by slicing and appending.