Python代写-A2A
时间:2022-03-08
1 / 7
o
Section A, A2A (10 marks)
Multiple Choice (MC) and Matching Questions, Online (SOUL-Quiz Feature)
o Identify and select the option of choice that "best" completes the statement, matches the item, or
answers the question.
o Number of Attempts Allowed: 2
o Grading method: Highest Grade
o Make sure you have successfully completed, "Finished" and submitted before deadline.
* Attempts must be submitted before time expires, or they are NOT counted. (Zero mark given)
o Questions related to program codes are based on Python programming language, unless specified.
o 5 MC questions and 5 Matching questions. Each question carries the same mark.
Section B, A2B (20 marks): Programming Tasks
* IMPORTANT:
o Source code *.py file must start with comments, including the information of individual student
(student name, student id) as example below, unless specified. E.g. below
# A2B1.py, for IDSA A2
# FINISHED by: , <20214016>
class Rectangle: # define the class of Rectangle
o Modify the given Main testing file M*.py file (if any) to display the information of individual student
(student name, student id) as example below, unless specified. E.g. below
=== A2B1, Rectangle Program, by <20214016> ===
...
...
2 / 7
General requirements (unless further specified):
o Students should handle special cases, including empty structure, one-element structure, etc.
o Proper brief comments are required, at least at the top of each source code file.
o Proper indentations required in writing program codes.
o All related files (including *.py) should be working in the same folder.
o Python list is mainly used to hold data elements as an array in our course and this assessment.
DO NOT use methods of Python’s list (such as list.append() or list.insert() etc.),
inheritance in OOP, or other non-taught approaches in our course, unless specified.
Given Materials:
o This assignment document.
o Python file Stack.py. DO NOT modify the file.
o Python file A2B1.py, A2B2.py to be modified and completed by student.
 Also modify top comments for your STUDENT INFO.
 DO NOT modify the given portions, including given methods if any.
o Python files MA2B1.py and MA2B2.py, the main file(s) for basic running and testing.
 DO NOT modify this given main test file, except the STUDENT INFO part.
A2B1 (10 marks)
Develop a Stack-based Queue having an internal Stack field, with the given Python file A2B1.py.
In our lecture notes, the sample Queue is implemented with Linked-List approach (with internal fields
head and tail nodes).
In this task, we need to re-implement the Queue ADT with another approach, a queue data type having an
internal field inStack of Stack type, only accessing the interface (operations) of the Stack.
o Treating data at TOP of Stack as FRONT of Queue; Bottom of Stack as REAR of Queue
* Useful hint: student may try to use two stacks to simulate a queue behaviour.
External: A Queue (behaviour) Internal: Use Stack to hold and access data
3 / 7
o Given an implemented Stack (below) in file Stack.py and an uncompleted Queue in A2B1.py
Operations (Stack) GIVEN Description
__init__(): Initiate/create a new Stack (constructor / initializer)
* This code sample is in Python-style
push(elt): Insert an element elt at the top of stack
pop():elt Remove and return the top element from the stack
- If failed, return null/None; e.g. empty stack
peek():elt Get and return the top element of the stack, without removal
- If failed, return null/None; e.g. empty stack
displayS(): Display all elements of the stack in order
Write a Python program, with the given Python files.
o Complete the Stack-based Queue based on Queue ADT, by modifying our given file A2B1.py
o Extra Operations (methods of the class) to be implemented by Students:
* ONLY access the interface (methods) of the given Stack data type.
* DO NOT access the internal fields of the given stack, such as pList, top and capacity.
 At least one line of simple comment for each extra operation required
Operation (QueueS) Description
__init__(): Initiate/create a new Queue (constructor / initializer) ; GIVEN
displayQ(): Display all elements of the Queue in order ; GIVEN
peek():elt Get and return the front element of queue, without removal (EASY)
- If failed, return None; e.g. empty queue
dequeue():elt Remove and return the front element from the queue (EASY)
- If failed, return None; e.g. empty queue
reverseQ(): Reverse the queue, by reversing elements in the internal inStack
enqueue(elt): Insert an element elt at the rear of queue (More Challenging)
* Useful hint: may use method reverseQ() and create an extra
temporary stack to complete this.
File A2B1.py, to be modified and completed by student.
# A2B1.py, for IDSA A2
# ...
# ONLY access the interface of the internal Stack
# displayS(), push(), peek(), pop()
from Stack import Stack
class QueueS: # defining a class of Queue, based on Stack
# Treating TOP of Stack as FRONT of Queue; Bottom of Stack as REAR of Queue
def __init__(self): # constructor
self.inStack = Stack() # an initial empty Stack, for internal Stack
def displayQ(self): # (REAR/Left to FRONT/Right)
print(f">>> Queue Display, (REAR/Left to FRONT/Right)")
if self.inStack.peek()==None:
print(" ... AN EMPTY QUEUE"); return
else:
self.inStack.displayS()
print()
###################### STUDNET's WORK ######################
# simple comment HERE
def peek(self):
pass # TO BE DONE
# MORE TO BE DONE
#################### END of STUDNET's WORK ######################
4 / 7
File MA2B1.py for basic running and testing
# MA2B1.py, for basic running and testing.
# * DO NOT modify this given test file, except the STUDENT INFO part.
# Main Testing Program
from A2B1 import QueueS
def main():
print("=== A2B1, Stack-based Queue, by ===\n")
myQ = QueueS()
print("\n--- 1. New QueueS created ---")
myQ.displayQ()
print(f" peek(), front elt is {myQ.peek()}")
myQ.enqueue('A'); myQ.enqueue('B'); myQ.enqueue('C')
print("\n--- 2. ENQueued: A,B,C ---")
myQ.displayQ()
myQ.reverseQ()
print("--------- 2.1 reverseQ once ---")
myQ.displayQ()
myQ.reverseQ()
print("--------- 2.2 reverseQ twice BACK ---")
myQ.displayQ()
deqElt = myQ.dequeue()
print("\n--- 3. DEQueued: elt is:", deqElt)
myQ.displayQ()
print(f" peek(), front elt is {myQ.peek()}")
myQ.enqueue('D')
deqElt = myQ.dequeue()
print("\n--- 4. ENQueued: D, then DEQueued: elt is:", deqElt)
myQ.displayQ()
# below, avoid execution if import only
if __name__ == "__main__": main()
Sample console display output of executing the main testing program MA2B1.py
=== A2B1, Stack-based Queue, by ===
--- 1. New QueueS created ---
>>> Queue Display, (REAR/Left to FRONT/Right)
... AN EMPTY QUEUE
peek(), front elt is None
--- 2. ENQueued: A,B,C ---
>>> Queue Display, (REAR/Left to FRONT/Right)
> C > B > A
--------- 2.1 reverseQ once ---
>>> Queue Display, (REAR/Left to FRONT/Right)
> A > B > C
--------- 2.2 reverseQ twice BACK ---
>>> Queue Display, (REAR/Left to FRONT/Right)
> C > B > A
--- 3. DEQueued: elt is: A
>>> Queue Display, (REAR/Left to FRONT/Right)
> C > B
peek(), front elt is B
--- 4. ENQueued: D, then DEQueued: elt is: B
>>> Queue Display, (REAR/Left to FRONT/Right)
> D > C
5 / 7
A2B2 (10 marks)
Develop a Stack-Based Application of evaluating Postfix Arithmetic Expressions (PAE), with the
given Python file A2B2.py.
o Suppose we are defining our OWN binary arithmetic operators with string symbols below:
Operator symbols (Strings) Description
'ad' Addition, as Python +
'su' Subtraction, as Python -
'mu' Multiply, as Python *
o Write a Python program to evaluate postfix Arithmetic Expressions, with the given Stack:
o Define a Python PostFixAE class in file A2B2.py, with given Stack class (in file Stack.py), to
evaluate Postfix Arithmetic Expression (PAE) using the algorithm in our lecture note (slide 21):
o Examples of valid PAE (Postfix Arithmetic Expression) stored in a Python list as input PAR
o Input Python List [9,9,7,'su','mu'] as input PAE 997-*
o Input Python List [9,7,'su',2,3,'mu','ad'] as input PAE 97-23*+
o Operations (methods of the class) to be implemented by Students:
* ONLY access the interface (methods) of the given Stack data type.
* DO NOT access the internal fields of the given stack, such as pList, top and capacity.
 At least one line of simple comment for each extra operation required
Operation
(PostFixAE)
Description
__init__(inList): Create and initiate a new object (constructor) GIVEN
- Internal Python List self.inPAEList holds the valid PAE data as a List
* Assume elements in the input list inList are either numbers or our binary
operators (thus, they are of valid element types), and valid PAE
binaryOpn(num1,
oprSym, num2):
int
Evaluate and return result of binary operation: num1 oprSym num2
- E.g. evaluate 9-7 if num1=9 ; oprSym='su' ; num2=7
- Also display result of binary operation before return, as sample format
string f"{num1}.{oprSym}.{num2} = {result}"
* Assume parameters are of the right types: valid numbers and operator symbols
(thus, they are of valid element types).
evaluatePAE():
int
Evaluate and return the result of the input postfix expression.
- Should make use of the above method binaryOpn()
* Assume elements in the input list inList form a valid PAE
* Hints:
- Create a local new Stack in this method for PAE evaluation.
- Use method type() to check type, e.g. type(123)==int gives True
Sample codes of file A2B2.py, for reference only (May contain bugs).
# A2B2.py, for IDSA A2
# ..
from Stack import Stack
class PostFixAE:
# * Assume valid PAE in inList
def __init__(self, inList): # constructor
self.inPAEList = inList # the input List as PAE
6 / 7
########### TO BE FINISHED BY STUDENT ###################
# simple comment HERE
# * Assume valid input parameters
def binaryOpn(self, num1, oprSym, num2): # return result of binary operation
pass # TO BE DONE
# simple comment HERE
# * Assume valid PAE in self.inPAEList
def evaluatePAE(self): # return result of PAE
pass # TO BE DONE, based on the algorithm in lecture note
File MA2B2.py for basic running and testing
# MA2B2.py, for basic running and testing.
# * DO NOT modify this given test file, except the STUDENT INFO part.
# Main Testing Program
from A2B2 import PostFixAE
def main():
print("=== A2B2, PAE using Stack, by ===\n")
myPAE = PostFixAE([9,9,7,'su','mu'])
print(f" DIRECT TEST 1: myPAE.binaryOpn(9,'su',7): {myPAE.binaryOpn(9,'su',7)}")
print(f" DIRECT TEST 2: myPAE.binaryOpn(9,'ad',7): {myPAE.binaryOpn(9,'ad',7)}")
print(f" DIRECT TEST 3: myPAE.binaryOpn(9,'mu',7): {myPAE.binaryOpn(9,'mu',7)}")
print("\n- 1. ---")
print(f" RESULT of {myPAE.inPAEList}: {myPAE.evaluatePAE()}")
myPAE = PostFixAE([19,7,'su',32,3,'mu','ad'])
print("\n- 2. ---")
print(f" RESULT of {myPAE.inPAEList}: {myPAE.evaluatePAE()}")
# below, avoid execution if import only
if __name__ == "__main__": main()
Sample console display output of executing the main testing program MA2B2.py
=== A2B2, PAE using Stack, by ===
9.su.7 = 2
DIRECT TEST 1: myPAE.binaryOpn(9,'su',7): 2
9.ad.7 = 16
DIRECT TEST 2: myPAE.binaryOpn(9,'ad',7): 16
9.mu.7 = 63
DIRECT TEST 3: myPAE.binaryOpn(9,'mu',7): 63
- 1. ---
9.su.7 = 2
9.mu.2 = 18
RESULT of [9, 9, 7, 'su', 'mu']: 18
- 2. ---
19.su.7 = 12
32.mu.3 = 96
12.ad.96 = 108
RESULT of [19, 7, 'su', 32, 3, 'mu', 'ad']: 108
7 / 7
o Submit ALL related files to SOUL:
o Stack.py (Given, DO NOT modify this file)
o A2B1.py, MA2B1.py
o A2B2.py, MA2B2.py
o Do NOT compress/zip or rename the files. Submission work not following requirements may be
penalized or not be assessed.
o Students are responsible for ensuring that their answer files are submitted successfully and properly
(e.g. submit the right file to the right submission item on SOUL, etc.).
o Lecturers will not handle or inform students by any means if improper file was submitted, such
as submitted file was corrupted or sent wrongly with an old version.
o It is highly recommended that students should download the uploaded files after submission, to
double-check their submitted work before deadline.
o Students should be able to finish this assignment within 1~3 hours.
o Students should well-plan their time and schedule to complete the assignment, review the related
course materials, finish and submit the assignment within the first 3 days after the assignment has been
released.
o Students who fail to follow take their own risk of improper submission or even missing the
submission deadline, even though the given submission period and set deadline may be much
longer than what is required to finish the assignment.
* Remarks:
o More testing may be performed during assessment based on the requirements, apart from the given
testing files.
~ END ~

essay、essay代写