首页 文章 精选 留言 我的

精选列表

搜索[博客系统],共10000篇文章
优秀的个人博客,低调大师

[雪峰磁针石博客]Python经典面试题: 用3种方法实现堆栈和队列并示例实际应用场景

介绍 数据结构在计算机中组织存储,以便我们可以有效地访问和更改数据。 堆栈和队列是计算机科学中定义的最早的数据结构。 堆栈 遵循后进先出 (Last-in-First-Out LIFO)原则。 push - 在堆栈顶部添加元素: pop - 删除堆栈顶部的元素: 队列 遵循先入先出(FIFO:First-in-First-Out)原则。 enqueue - 在队列的开头添加元素: dequeue - 删除队列开头的元素: 使用列表实现堆栈和队列 Python的内置List数据结构k堆栈和队列操作的方法。 堆栈 letters = [] # Let's push some letters into our list letters.append('c') letters.append('a') letters.append('t') letters.append('g') # Now let's pop letters, we should get 'g' last_item = letters.pop() print(last_item) # If we pop again we'll get 't' last_item = letters.pop() print(last_item) # 'c' and 'a' remain print(letters) # ['c', 'a'] 执行结果 g t ['c', 'a'] 队列 fruits = [] # Let's enqueue some fruits into our list fruits.append('banana') fruits.append('grapes') fruits.append('mango') fruits.append('orange') # Now let's dequeue our fruits, we should get 'banana' first_item = fruits.pop(0) print(first_item) # If we dequeue again we'll get 'grapes' first_item = fruits.pop(0) print(first_item) # 'mango' and 'orange' remain print(fruits) # ['c', 'a'] 执行结果 banana grapes ['mango', 'orange'] 使用Deque库的堆栈和队列 deque是Double Ended Queue的缩写 - 可以获取存储的第一个或最后一个元素的通用队列,下面我们使用Deque库的堆栈和队列: from collections import deque # you can initialize a deque with a list numbers = deque() # Use append like before to add elements numbers.append(99) numbers.append(15) numbers.append(82) numbers.append(50) numbers.append(47) # You can pop like a stack last_item = numbers.pop() print(last_item) # 47 print(numbers) # deque([99, 15, 82, 50]) # You can dequeue like a queue first_item = numbers.popleft() print(first_item) # 99 print(numbers) # deque([15, 82, 50]) 执行结果 47 deque([99, 15, 82, 50]) 99 deque([15, 82, 50]) 参考资料 本文最新版本地址 本文涉及的python测试开发库 谢谢点赞! 本文相关海量书籍下载 python工具书籍下载-持续更新 python GUI工具书籍下载-持续更新 更严格的实现 创建撤消功能 - 允许用户回溯他们的操作,直到会话开始。堆栈是这种情况的理想选择。 我们可以通过将其推送到堆栈来记录用户所采取的每个操作。 当用户想要撤消操作时,他们将从堆栈中弹出它。 游戏中,每次按下按钮,都会触发输入事件。 测试人员注意到,如果按钮按下得太快,游戏只处理第一个按钮,特殊动作将无效!可以使用队列修复它。 我们可以将所有输入事件排入队列。 #!/usr/bin/python3 # -*- coding: utf-8 -*- # 项目实战讨论QQ群630011153 144081101 # python测试开发库汇总: https://github.com/china-testing/python-api-tesing/ # 本文最佳板式地址: https://www.jianshu.com/p/c990427ca608 # A simple class stack that only allows pop and push operations class Stack: def __init__(self): self.stack = [] def pop(self): if len(self.stack) < 1: return None return self.stack.pop() def push(self, item): self.stack.append(item) def size(self): return len(self.stack) # And a queue that only has enqueue and dequeue operations class Queue: def __init__(self): self.queue = [] def enqueue(self, item): self.queue.append(item) def dequeue(self): if len(self.queue) < 1: return None return self.queue.pop(0) def size(self): return len(self.queue) document_actions = Stack() # The first enters the title of the document document_actions.push('action: enter; text_id: 1; text: This is my favourite document') # Next they center the text document_actions.push('action: format; text_id: 1; alignment: center') # As with most writers, the user is unhappy with the first draft and undoes the center alignment document_actions.pop() # The title is better on the left with bold font document_actions.push('action: format; text_id: 1; style: bold') input_queue = Queue() # The player wants to get the upper hand so pressing the right combination of buttons quickly input_queue.enqueue('DOWN') input_queue.enqueue('RIGHT') input_queue.enqueue('B') # Now we can process each item in the queue by dequeueing them key_pressed = input_queue.dequeue() # 'DOWN' # We'll probably change our player position key_pressed = input_queue.dequeue() # 'RIGHT' # We'll change the player's position again and keep track of a potential special move to perform key_pressed = input_queue.dequeue() # 'B' # This can do the act, but the game's logic will know to do the special move

优秀的个人博客,低调大师

[雪峰磁针石博客]python人工智能作业:Windows使用SAPI和tkinter用不到40行实现文本转语音工具

直接上代码: #!/usr/bin/env python3 # -*- coding: utf-8 -*- # 技术支持:https://www.jianshu.com/u/69f40328d4f0 # 技术支持 https://china-testing.github.io/ # https://github.com/china-testing/python-api-tesing/blob/master/practices/TTS.py # 讨论钉钉免费群21745728 qq群144081101 567351477 # CreateDate: 2018-11-22 import win32com.client as wincl from tkinter import * def text2Speech(): text = e.get() speak = wincl.Dispatch("SAPI.SpVoice") speak.Speak(text) #window configs tts = Tk() tts.wm_title("Text to Speech") tts.geometry("225x105") tts.config(background="#708090") f=Frame(tts,height=280,width=500,bg="#bebebe") f.grid(row=0,column=0,padx=10,pady=5) lbl=Label(f,text="Enter your Text here : ") lbl.grid(row=1,column=0,padx=10,pady=2) e=Entry(f,width=30) e.grid(row=2,column=0,padx=10,pady=2) btn=Button(f,text="Speak",command=text2Speech) btn.grid(row=3,column=0,padx=20,pady=10) tts.mainloop() 参考资料 本文最新版本地址 讨论 钉钉群21745728 qq群144081101 567351477 本文涉及的python测试开发库 谢谢点赞! 本文相关海量书籍下载 python 3.7极速入门教程9最佳python中文工具书籍下载 最新代码地址 执行

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册