| CPSC 115L: Introduction to Computing | Spring 2010 |
In the official list of 113,809 crosswords, 4502 words begin with 're'. The shortest such word is 're', and the longest such word is 'representativenesses'.
For this, it will be helpful to define a function named isaPrefix(word, pre) that returns True if pre is a prefix of word and False otherwise. For example, isaPrefix('record', 're') would return True but isaPrefix('record', 'ref') would return False. Make sure you test that your function is correct.
Run your script and save the snapshot of your test run in a text
file named re.out.
1.2. In English, many nouns end with the suffix
‘tion’; for example, action, computation, section, to list
a few. As in the previous problem, write a function named
isaSufffix(word, suff) that returns True if
suff is a suffix of word and False otherwise.
For example, isaSuffix('caption', 'tion') would return
True but isaSuffix('captions', 'tion') would return
False. Make sure you test that your function is correct.
What words in crosswords.txt have the suffix tion? Write a Python script named tion.py that opens the file crosswords.txt and reads all of its words into a list. (See last week's lab for a script to do this part). Then write a function named getSuffixes(list, suff) that takes the list of words, list, and returns a new list containing those words that have the suffix suff. For example, getSuffixes(['rover', 'router', 'caption', 'mister'], 'er') would return the list: ['rover', 'router', 'mister']. Your script should display a list containing all the crosswords with the 'tion' suffix.
1.3 Modify your script from the previous exercise to produce a sublist of crosswords that have a certain prefix and a certian suffix. For this problem write a function getPrefSuffs(list, pre, suff) that takes a list of words, list, and a certain prefix, pre, and suffix, suff, and returns the sublist that contains words with both that prefix and suffix.
Copy and paste your previous solution as the starting point for this problem, naming your script presuff.py. Run your script and save a snapshot of your test in a textfile named presuff.out.
1.4 Modify your script from the previous exercise (copy and paste) so that it will convert each word in a list to its Pig Latin translation. For this problem write a function named mapToPigLatin(list) that will take a list of words, list, and return a list with each of the words translated into Pig Latin. For example, maptoPigLatin(['reaction', 'redemption']) would return the list ['eactionray', 'edemptionray'].
Use this function, together with your other functions, to write a script that lets the user enter a prefix and a suffix, and then displays the list of the Pig Latin translation of all the words in the crosswords list that have that prefix and suffix. Your script should function as follows:
Type in a prefix > corr Type in a suffix > tion Here's your list: ['orrectioncay', 'orrelationcay', 'orroborationcay', 'orrugationcay', 'orruptioncay'] Type in a prefix > corr ...
|
|
CPSC 115L home page |
|
|