concordance.py: 34 points
The design of your program is up to you, but you should certainly divide the work to be done among several functions. From this point in the semester onwards, a
main() function is always a requirement for a complete correct submission. Make sure to include one. For the other functionality, you may want to use the following functions:
This function takes a string
word containing a single word, and returns a new string that has the letters of
word converted to lowercase with punctuation removed from the beginning and end of the word.
Python provides a variable that contains common punctuation characters called
string.punctuation, which you can access by importing the
string.punctuation variable can then be passed into the
strip() function to remove the leading and trailing punctuation from a given string:
word = word.strip(string.punctuation)
add_word(word, line_number, concordance):
This handles the work of recording that a given
word was found on the given
line_number in the dictionary
This handles printing one
word of the output and its line numbers.
concordance is the dictionary storing the concordance, so
concordance[word] should be the list of line numbers on which
main() organizes the high-level functionality of the file. We might suggest including the following computations in
main() for this lab:
standardize_word()to prepare each
wordfor adding to the concordance
add_word()to add the
wordand its current
wordto handle the output. The loop should print the words from the text in alphabetical order.
Make sure that before you submit your lab, you try out some filenames that are not those we provide as test files in repl.it or the Testing section. This will help you try out your
You need to print all of the words in your concordance in alphabetical order, followed by their line numbers. For this, we can use Python’s build-in
sorted() takes any iterable (i.e., anything that can be iterated over) and converts it into a sorted list. So, if we want to get a sorted version of our concordance keys, we can write
which will conveniently return a list of our keys in alphabetical order.