Python 堆叠数据结构以制作web浏览器
这里有一个解决方案和一个家庭作业 家庭作业:Python 堆叠数据结构以制作web浏览器,python,stack,Python,Stack,这里有一个解决方案和一个家庭作业 家庭作业: 你能自己完成前进法吗 注意: 看看我做了什么,自己完成前进法 类堆栈: 定义初始化(自): self.items=[] def推送(自身,项目): self.items.append(项目) #修改:如果在空堆栈上调用此方法,则引发异常 def pop(自我): 如果self.isEmpty()==True: 引发异常('堆栈为空,无法弹出') 如果self.isEmpty()==False: 返回self.items.pop() #修改:如果在
- 你能自己完成前进法吗
类堆栈:
定义初始化(自):
self.items=[]
def推送(自身,项目):
self.items.append(项目)
#修改:如果在空堆栈上调用此方法,则引发异常
def pop(自我):
如果self.isEmpty()==True:
引发异常('堆栈为空,无法弹出')
如果self.isEmpty()==False:
返回self.items.pop()
#修改:如果在空堆栈上调用此方法,则引发异常
def peek(自):
如果self.isEmpty()==True:
引发异常('堆栈为空,无法查看')
如果self.isEmpty()==False:
返回自项目[len(self.items)-1]
定义为空(self):
返回self.items==[]
def大小(自身):
返回len(自我项目)
def显示(自我):
打印(自选项目)
定义(自我):
stackAsString=''
对于self.items中的项目:
stackAsString+=项目+“”
返回堆栈字符串
#从堆栈导入堆栈
def getAction():
'''
编写docstring来描述函数
输入:此函数不接受任何参数。
返回:如果用户选择了正确的输入选项之一,则包含用户输入的字符串
'''
正确输入=错误
当正确输入=错误时:
用户输入=输入(“输入=输入URL,<返回,>前进,q退出:”)
如果用户输入=='q'或用户输入=='>'或用户输入=='Varaible,则函数名应在带有下划线的小写字母后面。使用if…==正确:
不太好,出于两个不同的原因,如果……,您可以简单地坚持使用:代码>取而代之。
from stack import stack
def getAction():
'''
Write docstring to describe function
Inputs: no arguments taken by this function.
Returns: string containing users input if he has chosen one of the correct input options
'''
correct_input=False
while correct_input==False:
user_input=input("Enter = to enter a URL, < to go back, > to go forward, q to quit: ")
if user_input =='q' or user_input =='>' or user_input =='<' or user_input =='=':
correct_input=True
if correct_input==False:
print('Invalid entry.')
return user_input
def goToNewSite(current, pages):
'''
Write docstring to describe function
Inputs: index of the current website (int), reference to list containing the webpage addresses to go back and forth between
Returns: address inputted by user as a string
'''
new_web_address=input('Enter a new website address ')
for i in range(current+1,len(pages)):
pages.pop()
pages.append(new_web_address)
#pages[current+1]=new_web_address
#pages=pages[:current+1]
return current+1
def goBack(current, pages):
'''
Write docstring to describe function
Inputs: index of the current website (int),reference to list containing the webpage addresses to go back and forth between
Returns: index of the previous webpage (int)
'''
# alternatively this could be done by checking if current-1>=0
if current-1>=0:
return current-1
else:
print('Cannot go backward')
return current
def goForward(current, pages):
'''
Write docstring to describe function
Inputs: index of the current website (int),reference to list containing the webpage addresses to go back and forth between
Returns: index of the previous webpage (int)
'''
# alternatively this could be done by checking if current +1 in range(len(pages))
if current+1<len(pages):
return current+1
else:
print('Cannot go forward')
return current
def main():
'''
Controls main flow of web browser simulator
Inputs: N/A
Returns: None
'''
HOME = 'www.google.ca'
websites = [HOME]
currentIndex = 0
quit = False
while not quit:
print('\nCurrently viewing', websites[currentIndex])
print(websites)
action = getAction()
if action == '=':
currentIndex = goToNewSite(currentIndex, websites)
elif action == '<':
currentIndex = goBack(currentIndex, websites)
elif action == '>':
currentIndex = goForward(currentIndex, websites)
elif action == 'q':
quit = True
print('Browser closing...goodbye.')
if __name__ == "__main__":
main()
class Stack:
def __init__(self):
self.items = []
def push(self, item):
self.items.append(item)
# MODIFY: RAISE AN EXCEPTION IF THIS METHOD IS INVOKED ON AN EMPTY STACK
def pop(self):
if self.isEmpty()==True:
raise Exception('Stack is empty cannot pop')
if self.isEmpty()==False:
return self.items.pop()
# MODIFY: RAISE AN EXCEPTION IF THIS METHOD IS INVOKED ON AN EMPTY STACK
def peek(self):
if self.isEmpty()==True:
raise Exception('Stack is empty cannot peek')
if self.isEmpty()==False:
return self.items[len(self.items)-1]
def isEmpty(self):
return self.items == []
def size(self):
return len(self.items)
def show(self):
print(self.items)
def __str__(self):
stackAsString = ''
for item in self.items:
stackAsString += item + ' '
return stackAsString