Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Find the time and space complexity of the following program: Program specifications: Develop class ListCollector as a subclass of HTMLParser that, when fed an HTML

Find the time and space complexity of the following program:

Program specifications:

Develop class ListCollector as a subclass of HTMLParser that, when fed an HTML file, creates a Python list for every ordered or unordered list in the HTML document. Each item of a Python list should be the test data that appears in one item of the corresponding HTML list. You may assume that every item of every list in the HTML document contains only text data (i.e. no other HTML element). method getLists() The class ListCollector should support method getLists() that takes no input arguments but returns a list containing all the created Python lists.

Program Code:

#Define the ListCollector class ListCollector(HTMLParser):

#constructor def __init__(self): #call the supper class constructor HTMLParser.__init__(self) #set the myFlag to 0 self.myFlag = 0 #set the olCounter to 0 self.olCounter = 0 #define the ulList to store the unordered-list elements self.ulList = [] #define the olList to store the ordered-list elements self.olList = []

#method to handle the start tag def handle_starttag(self, tag, attributes): #check if tag is ol if tag == 'ol': #set the olCounter to 1 self.olCounter = 1 #check if tag is ul if tag == 'ul': #set the olCounter to 2 self.olCounter = 2 #check the tag is not li if tag != 'li': #return return #check if myFlag is 1 if self.myFlag: #increment the myFlag by 1 self.myFlag += 1 #return return #set the myFlag to 1 self.myFlag = 1

#method to handle the close tag def handle_endtag(self, tag): #check if tag is ol or ul if tag == 'ol' or tag == 'ul': #set the olCounter to 0 self.olCounter = 0

#check tag is li and myFlag is 1 if tag == 'li' and self.myFlag:

#Decrement the myFlag by 1 self.myFlag -= 1

#method to handle the data inside the list def handle_data(self, data):

#if myFlag is set if self.myFlag:

#check olCOunter is 2 if self.olCounter == 2:

#append the data to ulList self.ulList.append(data)

#check olCounter is 1 elif self.olCounter == 1:

#append the data to the olList self.olList.append(data)

#method to print the list items def getLists(self):

#print the ordered and unordered list items print([self.ulList,self.olList])

Program Output:

image text in transcribed

I need the time and space complexity only, please explain the answer as well!

Sample Output

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

deadlock

Answered: 1 week ago