题目
单项选择题
Stacks_PhPP_4 Below is function written for Stack ADT , the Stack ADT is implemented using a Python's list. Which function is this? Assume Empty is implemented. def mystery(self): if self.is_empty(): raise Empty('Stack is empty') return self._data[-1] Below is the STACK ADTs' constructor: class ArrayStack: """LIFO Stack implementation using a Python list as underlying storage.""" def __init__(self): """Create an empty stack.""" self._data = [] # nonpublic list instance
选项
A.pop
B.top
C.push
D.return self._data[-1]
查看解析
标准答案
Please login to view
思路分析
To begin, let's understand what mystery(self) is doing. It first checks if the stack is empty, and raises an Empty exception in that case. If not empty, it returns the last element of the underlying list, self._data[-1], which corresponds to the top of the stack in a LIFO structure since the end of the list is treated as the top.
Option 1: pop
- This option suggests the function would remove and ......Login to view full explanation登录即可查看完整答案
我们收录了全球超50000道考试原题与详细解析,现在登录,立即获得答案。
类似问题
Which of the following Python data structures can be used to implement a stack efficiently?
Which of the following is LIFO (Last In First Out)?
Which of the following best describes what a "stack" is?
What is the result of performing the following stack operations starting with an empty stack: push(1), push(2), pop(), push(3), peek()?
更多留学生实用工具
希望你的学习变得更简单
加入我们,立即解锁 海量真题 与 独家解析,让复习快人一步!