当前位置:网站首页>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 .
边栏推荐
- Three traversal methods of binary tree
- [high frequency interview questions] difficulty 1.5/5, LCS template questions
- 微服务之配置管理中心
- Convn-n dimensional convolution
- How to close windows defender Security Center
- Secyun won the "2022 AI analysis · it operation and maintenance vendor panorama report" as the representative vendor of intelligent operation and maintenance aiops Market
- 关于枚举类的两种用法
- Steps for win10 to completely and permanently turn off automatic updates
- 号称史上最难618,淘宝数据盘点你做对了吗?
- MySQL learning 1: installing MySQL
猜你喜欢

Uniapp drop-down layer selection box effect demo (sorting)

面试突击60:什么情况会导致 MySQL 索引失效?

Industry insight - how should brand e-commerce reshape growth under the new retail format?

ACL 2022 | 中科院提出TAMT:TAMT:通过下游任务无关掩码训练搜索可迁移的BERT子网络

How histrix works

AI for Science:科研范式、开源平台和产业形态

picocli-入门

MySQL high level statements (I)

Interview shock 60: what will cause MySQL index invalidation?

想学好C语言,操作符也很重要
随机推荐
Script defer async mode
【粉丝福利】今天给大家介绍一个白捡钱的方法-可转债,本人亲自验证,每年每人能获利1500元
Hibernate operation Oracle database primary key auto increment
uni-app开发微信小程序动态渲染页面,动态改变页面组件模块顺序
alibaba jarslink
Private dry goods sharing: how to implement platform in Enterprise Architecture
word文本框换页
栈的计算(入栈出栈顺序是否合法)-代码
浏览器输入url地址,到页面渲染发生了什么
关于枚举类的两种用法
Mit6.031 software construction7 reading notesdesigning specifications
Interview shock 60: what will cause MySQL index invalidation?
Interview shock 60: what will cause MySQL index invalidation?
threejs的环境光+点光源+平行光源+球面光 以及hepler理解+阴影()
application.properties 的配置信息
和动态规划的第一次相遇
消息队列的使用
picocli-入门
How to find the movie and TV clips with the same lines? These 8 movies search for artifact, and find the corresponding segment in one line
如何下载带有超链接的图片