cs102 lab8


http://www.maths.nuigalway.ie/~gettrick/teach/cs102/labs/l8.html



In this lab you will use dictionaries/lists to manipulate text from a file. You may use as a starting point the Python lab handed out in lectures (the one which counts the occurrences of words in a file).
You should write two (2) separate Python programs. Each one should ask the user which (text) file they want to process/read. It should then open the file.

  1. For your first program, you should produce a dictionary where each key in the dictionary is the length of some word in the text file. Each value corresponding to a key should also be a dictionary, whose
    • keys are the actual words of that length
    • values are the number of occurrences of that specific word
    So, for example, if you run your Python program, and give as input the file http://www.maths.nuigalway.ie/~gettrick/teach/cs102/labs/toBE.txt the output should be
    {9: {'question!': 1}, 2: {'to': 2, 'is': 1, 'or': 1, 'be': 1}, 3: {'not': 1, 'the': 1, 'be,': 1}, 4: {'that': 1}}
    (do not worry about capitalization or punctuation marks). (Note that in this particular file, there is no word of length 1).
  2. For your second program, you should also produce a dictionary where each key in the dictionary is the length of some word in the text file. But this time, each value should be a list of all the words in the file that have that particular length (and if the same word occurs a few times, it will still only occur once in this list).
    So, for example, if you run your Python program, and give as input the file http://www.maths.nuigalway.ie/~gettrick/teach/cs102/labs/toBE.txt the output should be
    {9: ['question!'], 2: ['to', 'be', 'or', 'is'], 3: ['not', 'be,', 'the'], 4: ['that']}
Run both of your programs on the text file http://www.maths.nuigalway.ie/~gettrick/teach/cs102/labs/raglan.txt (a Patrick Kavanagh poem), and submit the output of you program as a text file (uploaded as an attachment in blackboard along with your 2 programs).
© NUI, Galway