cs102 lab8
http://www.maths.nuigalway.ie/~gettrick/teach/cs102/labs/l8.html
-
For this lab you must submit all source code (python
files), and, if applicable, the result(s) of running the program on (a few)
test cases. This should be sent in via BLACKBOARD (not directly by email).
The source code must
be well presented (indenting, spaces, reasonable variable/function names,
etc.) and must include comments (as a rough guideline - aim to have nearly as
many comments as lines of code). Any questions asked should be answered by
typing into a plain text (.txt) file which should also
be uploaded via blackboard.
-
This material should be uploaded
before
the deadline of 5pm
Friday April 2nd., 2010. You will lose 20% for each day
(or part of day) the lab is late.
-
Plagiarism (the unattributed copying of work from other sources
(internet, fellow students,....)) will not be tolerated. Please see
http://www.nuigalway.ie/engineering/documents/plagiarism_guide_students
_v4.pdf. You risk getting zero for your lab if it is found to be
plagiarized.
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.
-
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).
-
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