当前位置:网站首页>Stack calculation (whether the order of entering and leaving the stack is legal) - Code
Stack calculation (whether the order of entering and leaving the stack is legal) - Code
2022-06-27 12:40:00 【Unconquerable&Llxy】
1) Learn about stacks
Stack can be understood as :
A layered box , Only the top has an entrance . The value entered first must go down .
And when it comes to taking values outside , above , That is, those who went in later were taken out first , Those who enter first can only leave later . It's called :“ First in, then out ”.
2) Example .
for example , The stack order is a,b,c,d,e, Find the illegal stack order :
A. a,b,c,d,e
B. e,d,c,b,a
C. a,b,c,e,d
D. e,c,d,b,a
situation 1) All in , Then the stack order is :e,d,c,b,a(B Options ), Nothing to say .
situation 2) Single entry . One by one , Take... One by one . such as , Enter a value first a, And then not in the input value , But take it out first , Enter again b, take b, Enter into c, take c, wait . that , This stack order becomes a,b,c,d,e.(A Options )
situation 3) Enter a part first , take , Enter again , Retake , wait . There are too many cases in this part , Think about it with your own understanding .
3) Program realization .
It may be difficult to teach a program how to judge such a complex situation , But let it “ Exhausting ” Can reflect its strengths .
#coding=utf-8
class ZHAN(object):
def __init__(self):
self.real=[]
def add(self,other):
self.real.append(other)
def remove(self):
return self.real.pop()
def size(self):
return len(self.real)
def clear(self):
self.real=[]
def everything_out(self):
a=self.real[::-1]
self.clear()
return a
def canout(self):
try:
return self.real[-1]
except IndexError:
return None
def islegal(lt,choose_list):
init=ZHAN()
aa=0
legal=[]
illegal=[]
for i in choose_list:
aa+=1
out=[]#8 25 14 87 51 90 6 19 20
init.clear()
for j in lt:
init.add(j)
try:
while (i[len(out)]==init.canout()):
out.append(init.remove())
except:
pass
print(out)
if len(out)==len(i):
print(f"{aa} legal .")
legal.append(aa)
else:
print(f"{aa} illegal .")
illegal.append(aa)
a=input(" Entry order :")
sp=input(" Separator ")
if sp=="":
a=list(a)
else:
a=a.split(sp)
print(" Options :( This line does not enter content + Line feed stop input )")
b=[]
aa=0
while True:
aa+=1
z=input(f"{aa} term :")
if sp!="":
m=z.split(sp)
else:
m=list(z)
if z!="":
b.append(m)
else:
break
islegal(a,b)
First create a stack of entity data types , Then make an exhaustive analysis .
Let's take a look at the results :
Entry order :abcde
Separator
Options :( This line does not enter content + Line feed stop input )
1 term :abcde
2 term :edcba
3 term :abced
4 term :ecdba
5 term :
['a', 'b', 'c', 'd', 'e']
1 legal .
['e', 'd', 'c', 'b', 'a']
2 legal .
['a', 'b', 'c', 'e', 'd']
3 legal .
['e']
4 illegal .
Be careful , You can enter nothing at the separator , Line break directly , Indicates that each character is a separate value .
边栏推荐
- 树莓派 3b+ 学习
- log4j. Detailed configuration of properties
- nifi从入门到实战(保姆级教程)——身份认证
- 自学ADT和OOP
- Mathematical knowledge -- ideas and examples of game theory (bash game, Nim game, wizov game)
- Industry insight - how should brand e-commerce reshape growth under the new retail format?
- 带你认识图数据库性能和场景测试利器LDBC SNB
- .NET6接入Skywalking链路追踪完整流程
- Configuration of YML
- Mybaitis generator details
猜你喜欢
推荐系统的下一步?阿里时空聚合GNN,效果吊打LightGCN!
让学指针变得更简单(一)
Quanzhi A13 tossing memo
解除百度文库VIP、语雀、知乎付费限制,原来这么简单
解开C语言的秘密《关键字》(第六期)
convn-N 维卷积
Mathematical knowledge -- ideas and examples of game theory (bash game, Nim game, wizov game)
带你认识图数据库性能和场景测试利器LDBC SNB
Dm8: Dameng database - lock timeout
AI for Science:科研范式、开源平台和产业形态
随机推荐
浏览器输入url地址,到页面渲染发生了什么
如何修改 node_modules 里的文件
Interview shock 60: what will cause MySQL index invalidation?
Steps for win10 to completely and permanently turn off automatic updates
Topic38——56. 合并区间
Self taught ADT and OOP
秒云荣获《2022爱分析 · IT运维厂商全景报告》智能运维AIOps市场代表厂商
yml的配置
Word text box page feed
convn-N 维卷积
log4j的详情配置
Microservice splitting
Unzip log. GZ file
hibernate操作oracle数据库 主键自增
推荐系统的下一步?阿里时空聚合GNN,效果吊打LightGCN!
如何下载带有超链接的图片
Threejs' ambient light + point light + parallel light + spherical light and Hepler understanding + shadow ()
Hibernate operation Oracle database primary key auto increment
Win10彻底永久关闭自动更新的步骤
First encounter with dynamic programming