Question: # Stack.py # DO NOT modify this file class Stack: # defining a class of Stack, with Array-List def __init__(self, inSize=10): # constructor, default size

 # Stack.py # DO NOT modify this file class Stack: #defining a class of Stack, with Array-List def __init__(self, inSize=10): # constructor,default size = 10 self.pList = [None]*inSize # new a Python List

# Stack.py

# DO NOT modify this file

class Stack: # defining a class of Stack, with Array-List

def __init__(self, inSize=10): # constructor, default size = 10 self.pList = [None]*inSize # new a Python List to hold data self.top = 0 # Position of top element, starts from 1 self.capacity = inSize # The current capacity of the stack

def isEmptyS(self): return self.top==0 def displayS(self): # (Bottom/Left >[0] to Top/Right >> Stack Display, (Bottom/Left to Top/Right)") for i in range(0, self.top): print(f" > {self.pList[i]}", end='') print()

def push(self, eltIn): # method, push eltIn to the top of stack ########## section: for Enlarging the capacity if necessary # if (self.top == self.capacity): # full capacity reached? self.capacity *=2 # resize: double the original capacity newPList = [None]*(self.capacity) # create new pList for i in range(self.top): # copy original to new list newPList[i] = self.pList[i] self.pList = newPList # use the new list as updated ########## section: Insert new element, and shift the rest # self.top +=1 # update/increase position of top element self.pList[self.top-1] = eltIn # insert/add new element to top def peek(self): # method, peek/get top elt of stack without removal if self.top==0: # case of empty Stack return None else: return self.pList[self.top-1]

def pop(self): # method, Remove & return top elt of stack if self.top==0: # case of empty Stack return None else: popElt = self.pList[self.top-1] self.pList[self.top-1] = None self.top -= 1 # update/decrease position of top element return popElt

3. Part C, A2C (10 marks) Write a Python program, with the given Python files. o Program for Code Strings with Simple Method for Balancing Symbols, with Stack: o Define a Python class in a file A2C.py with the given Stack class (in file Stack.py) for the symbol balancing task, using the algorithm in our lecture notes: "Specified Balancing Symbols": 3 pairs of [ 1, (), o Name of the Class: CodeString o Operations (methods of the class) to be implemented by Students: At least one line of simple comment for each extra operation required Operation (CodeString) Description _init_(: Create and initiate a new CodeString (constructor) setCodeStr(strIn): Set a code string to this CodeString object symRatio(): float Determine and return the ratio of the number of "Specified Balancing Symbols" to that of all characters in the code string (including blank space). Return -1.0 if this cannot be determined. isSymBalanced():bool Return True if the CodeString is a code string with balanced symbols, False otherwise. An empty or None string is treated as balanced, thus True * Only consider Balancing Symbol-pairs of [ ], ( ), o Given Stack (in the file Stack.py * DO NOT modify this given file) Operation (Stack) Description _init_(): Create and initiate a new Stack (constructor) isEmptys(): bool Check if the Stack is empty. Return True if it is, otherwise False displays(): Display the Stack push(elt): Push/insert elt at the top of stack peek(): Get and return the top element, without removal pop(): Remove (& return) the top element of stack Sample codes of file A2c.py, for reference only (May contain bugs). # A2C.py, sample code, for reference only (May contain bugs). # Only consider 6 specified balanced symbols: [ 1 # FINISHED by: Student NAME, Student ID, Class from Stack import Stack class CodeString: ########### TO BE FINISHED BY STUDENT A . ### class CodeString: ############ TO BE FINISHED BY STUDENT ## def __init__(self): # constructor self.codeStr = None # the code string # simple comment HERE def setCodeStr(self, StrIn): pass # TO BE DONE # MORE TO BE DONE 3/5 One simplified algorithm, for balancing the symbol-pair '(' ')' only (pseudo-code), return TRUE if balanced, FALSE otherwise, for reference while there are still symbols to be read in the code string: if the symbol is '(': push the symbol elseif the input is ')': if stack is not empty: pop else: return FALSE if the stack is empty, return TRUE; otherwise return FALSE File A2CT.py, to be modified and completed by student. # A2CT.py, for basic running and testing. # * DO NOT modify this given test file, except the STUDENT INFO part. # Main Testing Program from A2C import CodeString def main(): # DO NOT modify this main() function, except Student NAME> print(" === A2C, Balancing Symbols program, by === ") symStrs = [ (10)", "", "ABC [( =AS D A A> ( ) ) AD ]", "00%", "ABC [( =AS D ) ) AD ]", 'print(f" in removel(). Position ({pos}} out of range!") 1 tCodeStr = CodeString() for strElt in symtrs: tCodeStr.setCodeStr(strelt) print(" -CODE STRING: '{strElt}'") print(f" - SymRatio: {tCodeStr.symRatio():0.2f}, Balanced: {tCodeStr.isSymBalanced()}") print(" =-= Program ends === ") main() Sun . nam File A2CT.py, to be modified and completed by student. # AZCT.py, for basic running and testing. # * DO NOT modify this given test file, except the STUDENT INFO part. # Main Testing Program from A2C import CodeString def main(): # DO NOT modify this main() function, except === ") symStrs = [ "010", "", "ABC [( =AS D", "ABC (( =AS D A A > ) ) AD 1", print(f" in removel(). Position f{pos}} out of range!") ] tCodeStr = CodeString() for strelt in symStrs: tCodeStr.setCodeStr(strElt) print(f" -CODE STRING: '{strelt}'") print(f" - SymRatio: (tCodeStr.symRatio():0.2f}, Balanced: {tCodeStr.isSymBalanced()}") print(" =-= Program ends === ") main() Sample console display output of executing the main testing program A2CT.py === A2C, Balancing Symbols program, by === -CODE STRING: '[()' - SymRatio: 1.00, Balanced: True -CODE STRING: - SymRatio: -1.00, Balanced: True -CODE STRING: "ABC (( =AS D A A>() AD ]' - SymRatio: 0.27, Balanced: True -CODE STRING: '[>' - SymRatio: 1.00, Balanced: False -CODE STRING: 'ABC [ ( AS D ) ) AD ] - SymRatio: 0.24, Balanced: False -CODE STRING: 'print(f" in removel(). Position [{pos}] out of range!")' - SymRatio: 0.13, Balanced: True Program ends ===

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!