Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 [ ], ( ), < >

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 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

##############################

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.

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 = [ "[]()<>", "", "ABC [( =AS D < A A> ( ) ) 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()

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 ===

----------------------------------------------------------------------------------------------------

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 <[n])

class StackAL: # 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 displayS(self): # (Bottom/Left >[0] to Top/Right <[n]) print(f">>> 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 < A A> ( ) ) 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()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Students also viewed these Databases questions