xiezilong1
xiezilong1
2020-09-17 09:18
采纳率: 100%
浏览 43

用python实现一个栈,输出结果为什么是[...],该怎么解决呢?谢谢

class Stack:
def init(self):
self.items = []

def isEmpty(self):
    return self.items == []

def push(self, items):
    self.items.append(self.items)

def pop(self):
    return self.items.pop()

def peek(self):
    return self.items[len(self.items) - 1]

def size(self):
    return len(self.items)
from DataStruct.Stack import Stack


def divideBy2(decNumber):
    remstack = Stack()

    while decNumber > 0:
        rem = decNumber % 2
        remstack.push(rem)
        decNumber = decNumber // 2

    binString = " "
    while not remstack.isEmpty():
        binString = binString + str(remstack.pop())

    return binString


print(divideBy2(123))

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • caozhy
    已采纳
    # encoding: utf-8
    
    
    class Stack:
        def __init__(self):
            self.items = []
    
        def isEmpty(self):
            return self.items == []
    
        def push(self, item): //这里错了,应该append作为参数的item,不是self.items
            self.items.append(item)
    
        def pop(self):
            return self.items.pop()
    
        def peek(self):
            return self.items[len(self.items) - 1]
    
        def size(self):
            return len(self.items)
    def divideBy2(decNumber):
        remstack = Stack()
    
        while decNumber > 0:
            rem = decNumber % 2
            remstack.push(str(rem))
            decNumber = decNumber // 2
    
        binString = " "
        while not remstack.isEmpty():
            binString = binString + remstack.pop()
    
        return binString
    
    
    print(divideBy2(123))
    
    点赞 评论

相关推荐