定义一个Python栈:stack.py
class Stack:
def __init__(self):
self.items = []
def is_Empty(self):
return self.items == []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
def peek(self):
return self.items[len(items)-1]
def size(self):
return len(self.items)
进制转换:
from stack import Stack
def divideBy2(decNumber, base):
remstack = Stack()
digits = "0123456789ABCDEF"
while decNumber > 0:
rem = decNumber % base
remstack.push(rem)
decNumber //= base
binString = ""
while not remstack.is_Empty():
binString = binString + digits[remstack.pop()]
return binString
print(divideBy2(15,16))
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。