Introduction to Computer Science II
Final Practice Exam
Solve the below problems by
implementing the appropriate classes and methods in file FinalPractice.py.
Problems
1. Implement recursive function prod()
that takes a list of numbers and returns their product. If
the input list is empty, the function should return 1. Your
implementation must be recursive.
Usage:
>>> prod([])
1
>>> prod([4])
4
>>> prod([4, 2, 6])
48
2. Implement recursive function fileNum()
that takes a pathname (as a string) as input and that returns
the total number of regular files contained in the directory
associate with the pathname, whether directly in the folder or
indirectly through subfolders. If the pathname refers to a regular
file, the function should just return 1. Test your function
on your file FinalPractice.py that you are working on and 2) the
folder test obtained by downloading the file test.zip
and unziping it in the same folder as your file FinalPractice.py.
Usage:
>>> fileNum('FinalPractice.py')
1
>>> fileNum('test')
6
3. As we saw in homework assignment 8, HTML
supports ordered and unordered lists. An ordered list is defined
using element ol and an unordered list is defined using
element ul. Develop class ListCounter as a
subclass of HTMLParser that, when fed an HTML file, counts
the number of top-level lists (ordered and unordered) in the HTML
document; A list is top-level if it is not inside any other list.
For example, in lists.html, there are 5
lists but only 2 are top-level.
The class ListCounter should support method getCount()
that takes no input arguments but returns the number of lists. Test
your solution on lists.html.
Usage:
>>> infile = open('lists.html')
>>> content = infile.read()
>>> infile.close()
>>> parser = ListCounter()
>>> parser.feed(content)
>>> parser.getCount()
2
4. In each of the four exercises in
function test(),
construct the regular expression that will match the described list
of words in file frankenstein.txt:
Usage:
>>>
test('frankenstein.txt')
Exercise i: the number of
times the string Frankenstein appears in the text:
28
Exercise ii: the list of all
different numbers appearing in the text
['24', '26', '27', '20',
'21', '22', '23', '28', '1', '3', '2', '5', '4', '7', '6', '9',
'8', '11', '10', '13', '12', '15', '14', '17', '16', '19', '18']
Exercise iii: the list of
strings of the form 'horror of <lowercase string>
<lowercase string>'
['horror of that
countenance', 'horror of my situation', 'horror of the recent',
'horror of his children', 'horror of my feelings', 'horror of my
employment', 'horror of\nmy proceedings', 'horror\nof others
appeared']
Exercise iv: the sentences
containing the word 'laboratory'
I sat one evening in my
laboratory; the sun had set, and the moon was
just rising from the sea; I
had not sufficient light for my employment,
and I remained idle, in a
pause of consideration of whether I should
leave my labour for the
night or hasten its conclusion by an unremitting
attention to it.
He then took me into
his laboratory and
explained to me the uses of
his various machines, instructing me as
to what I ought to procure
and promising me the use of his own when
I should have advanced far
enough in the science not to derange
their mechanism.
Sometimes I could not
prevail on
myself to enter my
laboratory for several days, and at other times
I toiled day and night in
order to complete my work.
My application was at
first
fluctuating and uncertain;
it gained strength as I proceeded and
soon became so ardent and
eager that the stars often disappeared
in the light of morning
whilst I was yet engaged in my laboratory.
The next morning, at
daybreak, I summoned
sufficient courage and
unlocked the door of my laboratory.
Soon after my arrival in the
hovel I discovered some papers in the
pocket of the dress which I
had taken from your laboratory.
He had also changed
my apartment; for he perceived that I
had acquired a dislike for
the room which had previously been my laboratory.
5. Implement function webdir() that takes as
input: a url (as a string) and non-negative integers depth and indent. Your function
should visit every web page reachable from the starting url web page
in depth clicks, and
print each web page's url. As shown below, indentation should be
used to indicate the depth of a url.
Usage:
>>>
webdir('http://reed.cs.depaul.edu/lperkovic/test1.html', 2, 0)
http://reed.cs.depaul.edu/lperkovic/csc242/test1.html
http://reed.cs.depaul.edu/lperkovic/csc242/test2.html
http://reed.cs.depaul.edu/lperkovic/csc242/test4.html
http://reed.cs.depaul.edu/lperkovic/csc242/test3.html
http://reed.cs.depaul.edu/lperkovic/csc242/test4.html