当前位置:网站首页>leetcode:736. LISP syntax parsing [flowery + stack + status enumaotu + slots]
leetcode:736. LISP syntax parsing [flowery + stack + status enumaotu + slots]
2022-07-07 02:24:00 【White speed Dragon King's review】

analysis
Look at knowledge without logic
Enum + auto
Setting up id The state of , It can be understood as “ Static state variables of class ”
Used to judge and identify unique States , You can use is Judge 
solts
About solts Usage of , It can also be understood as “ Static variable of class ”

Once a class is modified , The value of all instances will change
ac code
from enum import Enum, auto
class ExprStatus(Enum):
VALUE = auto() # The initial state
NONE = auto() # Unknown expression type
LET = auto() # let expression
LET1 = auto() # let The expression has been parsed vi Variable
LET2 = auto() # let The expression has parsed the last expression expr
ADD = auto() # add expression
ADD1 = auto() # add The expression has been parsed e1 expression
ADD2 = auto() # add The expression has been parsed e2 expression
MULT = auto() # mult expression
MULT1 = auto() # mult The expression has been parsed e1 expression
MULT2 = auto() # mult The expression has been parsed e2 expression
DONE = auto() # Parsing complete
class Expr:
#__slots__ = 'status', 'var', 'value', 'e1', 'e2'
def __init__(self, status):
self.status = status
self.var = '' # let The variable of vi
self.value = 0 # VALUE The number of States , perhaps LET2 The value of the last expression of the state
self.e1 = self.e2 = 0 # add or mult Two expressions of expression e1 and e2 The numerical
class Solution:
def evaluate(self, expression: str) -> int:
scope = defaultdict(list)
def calculateToken(token: str) -> int:
return scope[token][-1] if token[0].islower() else int(token)
vars = []
s = []
cur = Expr(ExprStatus.VALUE)
i, n = 0, len(expression)
while i < n:
if expression[i] == ' ':
i += 1 # Remove space
continue
if expression[i] == '(':
i += 1 # Remove the left parenthesis
s.append(cur)
cur = Expr(ExprStatus.NONE)
continue
if expression[i] == ')': # In essence, it turns the expression into a token
i += 1 # Remove the closing bracket
if cur.status is ExprStatus.LET2:
token = str(cur.value)
for var in vars[-1]:
scope[var].pop() # Clear scope
vars.pop()
elif cur.status is ExprStatus.ADD2:
token = str(cur.e1 + cur.e2)
else:
token = str(cur.e1 * cur.e2)
cur = s.pop() # Get the upper level status
else:
i0 = i
while i < n and expression[i] != ' ' and expression[i] != ')':
i += 1
token = expression[i0:i]
if cur.status is ExprStatus.VALUE:
cur.value = int(token)
cur.status = ExprStatus.DONE
elif cur.status is ExprStatus.NONE:
if token == "let":
cur.status = ExprStatus.LET
vars.append([]) # Record all variables in the scope of this layer , Convenient for subsequent removal
elif token == "add":
cur.status = ExprStatus.ADD
elif token == "mult":
cur.status = ExprStatus.MULT
elif cur.status is ExprStatus.LET:
if expression[i] == ')': # let The last of the expression expr expression
cur.value = calculateToken(token)
cur.status = ExprStatus.LET2
else:
cur.var = token
vars[-1].append(token) # Record all variables in the scope of this layer , Convenient for subsequent removal
cur.status = ExprStatus.LET1
elif cur.status is ExprStatus.LET1:
scope[cur.var].append(calculateToken(token))
cur.status = ExprStatus.LET
elif cur.status is ExprStatus.ADD:
cur.e1 = calculateToken(token)
cur.status = ExprStatus.ADD1
elif cur.status is ExprStatus.ADD1:
cur.e2 = calculateToken(token)
cur.status = ExprStatus.ADD2
elif cur.status is ExprStatus.MULT:
cur.e1 = calculateToken(token)
cur.status = ExprStatus.MULT1
elif cur.status is ExprStatus.MULT1:
cur.e2 = calculateToken(token)
cur.status = ExprStatus.MULT2
return cur.value
summary
After a few months
I also found a daily problem that I don't even want to see the solution
nice
边栏推荐
- 本周 火火火火 的开源项目!
- Freeswitch dials extension number source code tracking
- The boss is quarantined
- 企业中台建设新路径——低代码平台
- 【论文阅读|深读】 GraphSAGE:Inductive Representation Learning on Large Graphs
- 云原生混部最后一道防线:节点水位线设计
- Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
- 3--新唐nuc980 kernel支持jffs2, Jffs2文件系统制作, 内核挂载jffs2, uboot网口设置,uboot支持tftp
- 遇到慢SQL该怎么办?(下)
- [C # notes] use file stream to copy files
猜你喜欢

张平安:加快云上数字创新,共建产业智慧生态
![[paper reading | deep reading] anrl: attributed network representation learning via deep neural networks](/img/06/17acf9958228cce5d80ada3275ad24.png)
[paper reading | deep reading] anrl: attributed network representation learning via deep neural networks

云原生混部最后一道防线:节点水位线设计

Livox激光雷达硬件时间同步---PPS方法

SchedulX V1.4.0及SaaS版发布,免费体验降本增效高级功能!

一片葉子兩三萬?植物消費爆火背後的“陽謀”

解密函数计算异步任务能力之「任务的状态及生命周期管理」

Lidar: introduction and usage of ouster OS

Flir Blackfly S 工业相机 介绍

强化学习如何用于医学影像?埃默里大学最新《强化学习医学影像分析》综述,阐述最新RL医学影像分析概念、应用、挑战与未来方向
随机推荐
【论文阅读|深读】 GraphSAGE:Inductive Representation Learning on Large Graphs
[paper reading | deep reading] graphsage:inductive representation learning on large graphs
Web3的先锋兵:虚拟人
【Unity】升级版·Excel数据解析,自动创建对应C#类,自动创建ScriptableObject生成类,自动序列化Asset文件
【LeetCode】Day97-移除链表元素
Recommended collection!! Which is the best flutter status management plug-in? Please look at the ranking list of yard farmers on the island!
#夏日挑战赛#数据库学霸笔记(下)~
A new path for enterprise mid Platform Construction -- low code platform
猿桌派第三季开播在即,打开出海浪潮下的开发者新视野
The mega version model of dall-e MINI has been released and is open for download
Chang'an chain learning notes - certificate model of certificate research
Flir Blackfly S 工业相机:通过外部触发实现多摄像头同步拍摄
组合导航:中海达iNAV2产品描述及接口描述
postgresql之整體查詢大致過程
The foreground downloads network pictures without background processing
[xlua notes] array of lua to array of C #
最近小程序开发记录
Redis tool class redisutil (tool class III)
Jacob Steinhardt, assistant professor of UC Berkeley, predicts AI benchmark performance: AI has made faster progress in fields such as mathematics than expected, but the progress of robustness benchma
STM32F4---通用定时器更新中断