class AList: # defining a class of Dynamic-Array-List
def __init__(self, inSize=10): # constructor, default size = 10 self.pList = [None]*inSize # new a Python List to hold data self.last = 0 # The position of the last element self.capacity = inSize # The current capacity of the list
###################### STUDNET's WORK ###################### # simple comment HERE def isEmptyL(self): pass # TO BE DONE # simple comment HERE def appendL(self, elt): pass # TO BE DONE
# simple comment HERE def removeLastL(self): pass # TO BE DONE # simple comment HERE def searchL(self, elt): pass # TO BE DONE #######################################
def sizeL(self): return self.last def displayL(self): print(f">>> AList Display, with size/last,", f"capacity:") for i in range(self.sizeL()): # show data values in the list print(f" > {self.pList[i]}", end='') print()
def getL(self, pos): # pos start from 1, # O(1) if self.sizeL()==0: # case of empty list print(" in getL(). The List is empty!") return None elif (posself.last): # case of out of range print(f" in getL(). Position [{pos}] out of range!") return None else: return(self.pList[pos-1]) # O(1) pos-1, map to list index
def removeL(self, pos): # pos start from 1, # O(n) if self.sizeL()==0: # case of empty list print(" in removeL(). The List is empty!") return None elif (posself.last): # case of out of range print(f" in removeL(). Position [{pos}] out of range!") return None else: i = pos-1 removeElt = self.pList[i] while i self.pList[i] = self.pList[i+1] i+=1 self.pList[i] = None self.last -=1 # update position of last element return removeElt
def insertL(self, eltIn, pos): # pos start from 1, # O(n) if (posself.last+1): # case of out of range print(f" in insertL(). Position [{pos}] out of range!") else: ########## section: Enlarge the capacity if necessary # if (self.last == self.capacity): # check full capacity? self.capacity *=2 # double original capacity newPList = [None]*(self.capacity) # create new pList for i in range(self.last): # copy data to new list newPList[i] = self.pList[i] self.pList = newPList # use new list as updated one ########## section: Insert new element, and shift the rest # i = self.last while i>=pos: # O(n): shift/pack elements to right self.pList[i] = self.pList[i-1] i-=1 self.pList[pos-1] = eltIn # insert/add new element self.last +=1 # update position of last element
2. Part B, A1B (10 marks) Write a Python program, with the given Python files. compuy Sue # Aones.py, for IDSA Assign 1 class Alist: # defining a class of Dynamic-Array-List # ... #**##****########*****# STUDNET'S WORK ##****##*####********* # simple comment HERE def isEmptyl (self): pass # TO BE DONE # AND MORE #*************** *************** 1 o Enhancing Array-List: Enhance and implement a List ADT (with Array-based approach), by modifying our given Python file AoneB.py (based on the one in our lecture notes, Alist.py) Extra Operations (methods of the class) to be implemented by Students: At least one line of simple comment for each extra operation required Operation (DLL) Description isEmptyL(): bool Check if the list is empty or not append(elt): Insert a new element elt into the end of the current list. removeLastL():elt Remove & return the last element elt of the list o Return None in case of empty list searchL(elt):int Search & return the position of the first occurrence of an input searching element elt. * Position starts from 1. Return - 1 if not exist. File AoneBT.py for basic running and testing # Aonect.py, for basic running and testing. # * DO NOT modify this given test file, except the STUDENT INFO part. # Main Testing Program from Aone import AList O def main(): print("=== A1B, Enhanced ArrayList, by
=== ") myL - AList() print(f"--- 1. New AL created, isEmptyL() result: {myl.isEmptyL()}") myL.insertL('A', 1); myL.insertL('C', 2); myL.appendL('ABC'); myL.append('D'); myL.append('B'); print(" --- 2. Finished AppendL(): A,C > ABC,D,B ---") myL.displayL() print(f" removeLastL(), elt is {myL.removeLastL()}") print(" --- 3. Finished removeLastL ---") myl.displayL() print(" --- 4. isEmptyL(), value: {myl.isEmptyL()}") print(f" --- 5. searchL('ABC'), pos value: {myl.searchL('ABC')}") print(f" --- 6. searchL('XYZ'), pos value: {myl.searchL('XYZ')}") print(" === Program ends === ") 14:4 main() O Given Materials: Python file AoneB.py, to be modified and completed by student. Also modify top comments for your STUDENT INFO. (DO NOT modify this given portions, including given methods if any) Python file AoneBT.py for basic running and testing. DO NOT modify this given test file, except the STUDENT INFO part. Sample console display output of executing the main testing program Aonelt.py === A1B, Enhanced ArrayList, by === File Aonel.py, to be modified and completed by student. # Aones.py, for IDSA Assign 1 class Alist: # defining a class of Dynamic-Array-List --- 1. New AL created, isEmptyL() result: True --- 2. Finished append(): A,C > ABC,D,B --- >>> AList Display, with size/last: > A > C > ABC > D > B removeLastL(), elt is B ####################### STUDNET'S WORK #********************* # simple comment HERE def isEmptyL(self): pass # TO BE DONE # AND MORE --- 3. Finished removeLastL() --- >>> AList Display, with size/last, capacity: > A > C > ABC > D --- 4 isEmptyL(), value: False --- 5. searchl('ABC'), pos value: 3 File AoneBr.py for basic running and testing # Aonect.py, for basic running and testing. # * DO NOT modify this given test file, except the STUDENT INFO part. # Main Testing Program -- 6. searchL("XYZ'), pos value: -1 === Program ends --- from Aone import Alist 2. Part B, A1B (10 marks) Write a Python program, with the given Python files. compuy Sue # Aones.py, for IDSA Assign 1 class Alist: # defining a class of Dynamic-Array-List # ... #**##****########*****# STUDNET'S WORK ##****##*####********* # simple comment HERE def isEmptyl (self): pass # TO BE DONE # AND MORE #*************** *************** 1 o Enhancing Array-List: Enhance and implement a List ADT (with Array-based approach), by modifying our given Python file AoneB.py (based on the one in our lecture notes, Alist.py) Extra Operations (methods of the class) to be implemented by Students: At least one line of simple comment for each extra operation required Operation (DLL) Description isEmptyL(): bool Check if the list is empty or not append(elt): Insert a new element elt into the end of the current list. removeLastL():elt Remove & return the last element elt of the list o Return None in case of empty list searchL(elt):int Search & return the position of the first occurrence of an input searching element elt. * Position starts from 1. Return - 1 if not exist. File AoneBT.py for basic running and testing # Aonect.py, for basic running and testing. # * DO NOT modify this given test file, except the STUDENT INFO part. # Main Testing Program from Aone import AList O def main(): print("=== A1B, Enhanced ArrayList, by === ") myL - AList() print(f"--- 1. New AL created, isEmptyL() result: {myl.isEmptyL()}") myL.insertL('A', 1); myL.insertL('C', 2); myL.appendL('ABC'); myL.append('D'); myL.append('B'); print(" --- 2. Finished AppendL(): A,C > ABC,D,B ---") myL.displayL() print(f" removeLastL(), elt is {myL.removeLastL()}") print(" --- 3. Finished removeLastL ---") myl.displayL() print(" --- 4. isEmptyL(), value: {myl.isEmptyL()}") print(f" --- 5. searchL('ABC'), pos value: {myl.searchL('ABC')}") print(f" --- 6. searchL('XYZ'), pos value: {myl.searchL('XYZ')}") print(" === Program ends === ") 14:4 main() O Given Materials: Python file AoneB.py, to be modified and completed by student. Also modify top comments for your STUDENT INFO. (DO NOT modify this given portions, including given methods if any) Python file AoneBT.py for basic running and testing. DO NOT modify this given test file, except the STUDENT INFO part. Sample console display output of executing the main testing program Aonelt.py === A1B, Enhanced ArrayList, by === File Aonel.py, to be modified and completed by student. # Aones.py, for IDSA Assign 1 class Alist: # defining a class of Dynamic-Array-List --- 1. New AL created, isEmptyL() result: True --- 2. Finished append(): A,C > ABC,D,B --- >>> AList Display, with size/last: > A > C > ABC > D > B removeLastL(), elt is B ####################### STUDNET'S WORK #********************* # simple comment HERE def isEmptyL(self): pass # TO BE DONE # AND MORE --- 3. Finished removeLastL() --- >>> AList Display, with size/last, capacity: > A > C > ABC > D --- 4 isEmptyL(), value: False --- 5. searchl('ABC'), pos value: 3 File AoneBr.py for basic running and testing # Aonect.py, for basic running and testing. # * DO NOT modify this given test file, except the STUDENT INFO part. # Main Testing Program -- 6. searchL("XYZ'), pos value: -1 === Program ends --- from Aone import Alist