Question: test file: 3. Part C, A2C (10 marks) Write a Python program, with the given Python files. O O o O O Program for Code

 test file: 3. Part C, A2C (10 marks) Write a Python

program, with the given Python files. O O o O O Program

test file:

for Code Strings with Simple Method for Balancing Symbols, with Stack: Define

3. Part C, A2C (10 marks) Write a Python program, with the given Python files. O O o O O Program for Code Strings with Simple Method for Balancing Symbols, with Stack: 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 [ ]. (). Name of the Class: CodeString 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 [ 1. (). 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 clement, 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: ( 0) # FINISHED by: Student NAME, Student ID, Class from Stack import Stack 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 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: Class CodeString: ######## TO BE FINISHEDY STUENT ####### 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 'c': 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 === ") symstrs = [ 00", "", "ABC ( =AS D", "ABC ( =AS D A A > ) ) AD ]") 'print(f" in removel(). Position {pos)] out of range!") ] tCodeStr = CodeString() for strElt in symStrs: tCodeStr.setCodeStr(strelt) print(" -CODE STRING: '{strelt'") print(f" SymRatio: (tCodeStr.symkatio():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: 0 () - SymRatio: 1.00, Balanced: True -CODE STRING: - SymRatio: -1.00, Balanced: True -CODE STRING: "ABC (( =AS D () AD ] - SymRatio: 0.27, Balanced: True -CODE STRING: 'O() - 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 === # 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 print(" === A2C, Balancing Symbols program, by === ") symStrs = [ "[]()", "", "ABC [( =AS D () ) AD ]", "[]()>", "ABC[ ( =AS D )) AD ]", 'print(f" in removeL(). Position [{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.is SymBalanced()}") print(" =-= Program ends === ") main()

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!