【发布时间】:2018-07-05 09:38:41
【问题描述】:
我不明白他们在问什么。有人可以澄清这个问题吗?
问题是:(他们给了我们文档字符串)
在 stack_array 模块中编写并测试方法 combine。通过名为 q3.py 的模块对其进行测试。
这是对 Stack 类的扩展。它应该产生与问题 1 相同的结果,但它必须在堆栈代码定义的最低级别工作。也就是说,这个方法不能调用Stack的push和pop,它必须直接使用_values。
def combine(self, s2):
"""
-------------------------------------------------------
Combines a second stack with the current stack.
(iterative algorithm)
Use: s3 = s1.combine(s2)
-------------------------------------------------------
Preconditions:
s2 - an array-based stack (Stack)
Postconditions:
Returns:
s3 - the contents of the current stack and s2
are interlaced into s3 - current stack and s2
are empty (Stack)
-------------------------------------------------------
"""
#Write your code here
我已经使用 push、pop 和 peek 解决了这个问题。
s3 = Stack()
if len(s1._values) >= len(s2._values):
for things in s1:
if s2.is_empty() != True:
s3.push(things)
s1.pop()
s3.push(s2.peek())
s2.pop()
else:
s3.push(things)
s1.pop()
else:
for things in s2:
if s1.is_empty() != True:
s3.push(things)
s2.pop()
s3.push(s1.peek())
s1.pop()
else:
s3.push(things)
s2.pop()
return s3
【问题讨论】:
-
我们不会做你的功课或老师的工作来给出正确的指示。
-
我不是要求人们做我的功课,我只是想澄清一下问题是什么,或者人们将如何尝试。
-
如果你不理解你的作业,请让你的老师澄清一下。他们最清楚被问到的是什么,而且他们会得到报酬来教你。
-
它如何创建新堆栈?所以如果你有
s1作为[1,2,3]和s2作为[3,4,5],s3必须是什么样子?会是[1, 3, 2, 4, 3, 5]吗? -
@RoadRunner :它使用它创建一个新堆栈,是的,它应该看起来像那样。 'def __init__(self): """ ---------------------------------------- --------------- 初始化一个空栈。数据存储在一个列表中。使用:s = Stack() ---------------- --------------------------------------- 后置条件:初始化一个空栈。---- -------------------------------------------------- - """ self._values = [] return'
标签: python python-3.x stack adt