Data Structures and Algorithms(python)
Also, show me the images of code and run module in Pycharm
Given Materials: o Python file StackAL.py, for reference. * DO NOT modify this given file. o Python file A2CT.py, to be modified and completed by student. Also modify top comments for your STUDENT INFO, and the STUDENT INFO part.
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 Array-based List (in file StackAL.py), for the following symbol balancing: o "Specified Balancing Symbols": 3 pairs of [ ], ( ), 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. o Return -1.0 if this cannot be determined. isSymBalanced():bool Return True if the CodeString is a code string with balanced symbols, False otherwise. o An empty or None string is treated as balanced, thus True * Only consider Balancing Symbol-pairs of [ ], ( ),
StackAL.py # StackAL.py, Python module for Stack ADT, with Array-List # Remark: Internal Position (top) index of elements starts from 1 # (Bottom/Left >[0] to Top/Right [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 print(" The peek() is unsuccessful!") print(" ---The Stack is empty!") 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 print(" The pop() is unsuccessful!") print(" ---The Stack is empty!") 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
------------------------------------------------------------------------------------------------------------------------------------------------------------
A2CT.py # 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.isSymBalanced()}") print(" === Program ends === ") main()
3. Part C, A2C (10 marks) Write a Python program, with the given Python files. & Program for Code Strings with Simple Method for Balancing Symbols, with Stack: Define a Python class in a file A2C.py with the given Array-based List (in file StackAL.py), for the following symbol balancing "Specified Balancing Symbols": 3 pairs of 1,0. 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_0: Create and initiate a new CodeString (constructor) setCodestr(strin): Set a code string to this CodeString object symRatio): float Determine and retun the ratio of the number of "Specified Balancing Symbols" to that of all characters in the code string. 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 [ ], ( ), Sample 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 []( # FINISHED by: Student NAME, Student ID, Class from StackAL import StackAL ###### class CodeStringi ########## 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 ##***#***##***********#***#### Given Materials: Python file StackAL.py, for reference. * DO NOT modify this given file. Python file A2CT.py, to be modified and completed by student. Also modify top comments for your STUDENT INFO, and the STUDENT INFO part. 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
print(" === A2C, Balancing Symbols program, by === ") symStrs = [ (10)", "", "ABC [( =AS D", "ABC | ( =AS D ) ) AD 1", print(f" in removeL(). Position [{pos}} out of range!")' 1 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: '110' 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 === # StackAL.py, Python module for Stack ADT, with Array-List # Remark: Internal Position (top) index of elements starts from 1 # (Bottom/Left >[0] to Top/Right [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 = newList # 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 print(" The peek() is unsuccessful!") print(" ---The Stack is empty!") 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 print(" The pop() is unsuccessful!") print(" ---The Stack is empty!") 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. & Program for Code Strings with Simple Method for Balancing Symbols, with Stack: Define a Python class in a file A2C.py with the given Array-based List (in file StackAL.py), for the following symbol balancing "Specified Balancing Symbols": 3 pairs of 1,0. 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_0: Create and initiate a new CodeString (constructor) setCodestr(strin): Set a code string to this CodeString object symRatio): float Determine and retun the ratio of the number of "Specified Balancing Symbols" to that of all characters in the code string. 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 [ ], ( ), Sample 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 []( # FINISHED by: Student NAME, Student ID, Class from StackAL import StackAL ###### class CodeStringi ########## 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 ##***#***##***********#***#### Given Materials: Python file StackAL.py, for reference. * DO NOT modify this given file. Python file A2CT.py, to be modified and completed by student. Also modify top comments for your STUDENT INFO, and the STUDENT INFO part. 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 print(" === A2C, Balancing Symbols program, by === ") symStrs = [ (10)", "", "ABC [( =AS D", "ABC | ( =AS D ) ) AD 1", print(f" in removeL(). Position [{pos}} out of range!")' 1 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: '110' 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 === # StackAL.py, Python module for Stack ADT, with Array-List # Remark: Internal Position (top) index of elements starts from 1 # (Bottom/Left >[0] to Top/Right [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 = newList # 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 print(" The peek() is unsuccessful!") print(" ---The Stack is empty!") 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 print(" The pop() is unsuccessful!") print(" ---The Stack is empty!") 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