question archive Question 1: Emulate the stack behaviour using the list data structure
Subject:Computer SciencePrice:3.86 Bought7
Question 1:
Emulate the stack behaviour using the list data structure.
a) Complete the methods of the following Stack class according to their description
class Stack: def __init__(self): """ Initialize a new stack """ self.elements = [] def push(self, new_item): """ Append the new item to the stack """ ## CODE HERE ### def pop(self): """ Remove and return the last item from the stack """ ## CODE HERE ### def size(self): """ Return the total number of elements in the stack """ ## CODE HERE ### def is_empty(self): """ Return True if the stack is empty and False if it is not empty """ ## CODE HERE ### def peek(self): """ Return the element at the top of the stack or return None if the stack is empty """ ## CODE HERE ###
b) Use the Stack class that you defined in Q3a to complete the code of the is_valid() function, which checks whether the order of the brackets of an arithmetic expression is correct. Some examples are given below:
exp1 = "(2+3)+(1-5)" # True exp2 = "((3*2))*(7/3))" # False exp3 = "(3*5))]" # False
def is_valid(exp): """ Check the order of the brackets Returns True or False """ opening = ['(', '[', '{'] closing = [')', ']', '}'] ## CODE HERE ## return ## CODE HERE ## is_valid(exp1) is_valid(exp2) is_valid(exp3)
c) Use the Stack class that you defined in Q3a to complete the code of the count_pairs() function, which returns the number of the valid bracket pairs of an arithmetic expression. Some examples are given below:
exp1 = "(2+3)+(1-5)" # 2 pairs exp2 = "((([()])))" # 5 pairs exp3 = "[([])" # 2 pairs
def count_pairs(exp): """ Count the valid number of brackets Returns the total number of valid brackets in the string """ opening = ['(', '[', '{'] closing = [')', ']', '}'] ## CODE HERE ## return ## CODE HERE ## count_pairs(exp1) count_pairs(exp2) count_pairs(exp3)
Question 2:
According to each method's documentation, complete the code of the TextProcessor class and its subclass TextAnalyzer.
class TextProcessor: def __init__(self, text): self.text = text def setStopWords(): ''' set stop words as recieved in the parameters ''' ## CODE HERE ## def getStopWords(): ''' return stop words ''' ## CODE HERE ## def getUniqWords(self): return set(self.__text.split()) def getFilteredText(self): ''' remove filter words from the text return filtered text ''' ## CODE HERE ## class TextAnalyzer: def __init__(): ''' Construct the class ''' ## CODE HERE ## def getWordFrequency(): ''' Call the getFilteredText() method Create a dictionary of words key = word and value= frequency return the dictionary ''' ## CODE HERE ##
Verify the correctness of your code using the following steps:
ta = TextAnalyzer("a quick brown fox " +
"a quick brown fox jumps " +
"a quick brown fox jumps over " +
"a quick brown fox jumps over the " +
"a quick brown fox jumps over the lazy " +
"a quick brown fox jumps over the lazy dog")
2.Assign a list of stop words using the setStopWords() method: ta.setStopWords(['a', 'the'])
3.Count the occurrences of each word using the getWordFrequency() method: ta.getWordFrequency()
4.The output should be as follows {'quick': 6, 'brown': 6, 'fox': 6, 'jumps': 5, 'over': 4, 'lazy': 2, 'dog': 1}}
(ONLY QUESTION 2 is required to answer)
Purchased 7 times