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


![default size = 10 self.pList = [None]*inSize # new a Python List](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f3123f27c8e_95066f3123e9a345.jpg)
# 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>
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
