HW10 - UCSB Computer Science

Name: __________________________________________
Umail address:[email protected]
Computer Science 8
Instructor: Buoni
HW10
Lab Time (circle one): W8 W9 W10 W11 F11 F12 F1 F2
Collaborator Name:_______________________________
Reading required: pp.
177 - 181 (section 6.2)
For this HW assignment, you will need to download wordlist.txt
1. Write a function getListOfWordsBeginningWithChar(ch) that returns a list of all the words
contained in file wordlist.txt that begin with string ch.
For example, if ch = ‘hippo’, then the following list is returned: ['hippo', 'hippoboscid',
'hippocampus', 'hippocentaur', 'hippocras', 'hippodrome', 'hippogriff', 'hippogryph',
'hippologist', 'hippophagist', 'hippophile', 'hippopotamus']
2. Write a function writeWordsBeginningWithCharToFile(ch) that creates a new file named
‘ch-words.txt’ (e.g. ‘hippo-words.txt’ if ch = ‘hippo’) containing all the words of wordlist.txt that
begin with string ch and one word per line of file.
3. Did you ever wonder what are the longest words that when spelled backwards form other
valid words? Well, now you get to answer that question (even if you never asked it ;-)). Write a
function writeLongestBackwardsWordsToFile(M) that writes a file ‘topMBackwardsWords.txt’
(e.g. ‘top3BackwardsWords.txt’ if M = 3), containing the M longest words and their
corresponding backwards word together on one line, and different word pairs on different
lines. Try not to duplicate the same word pair (with order reversed) on a different line, but if
you cannot solve the problem without doing this no points will be deducted.
See the following expected output for ‘top3BackwardsWords.txt’:
Hint: Break the function into 3 sections: 1) read in wordlist.txt and convert to a set, 2) for-loop
through the set of words and check if the reverse word is also in the set (sets are very fast at
checking membership, lists are slowww…), forming a list of reversible words. 3) Sort the list of
words by length (make a list of tuples) and write first M to file.