当前位置:网站首页>leetcode:736. Lisp 语法解析【花里胡哨 + 栈 + 状态enumaotu + slots】
leetcode:736. Lisp 语法解析【花里胡哨 + 栈 + 状态enumaotu + slots】
2022-07-06 18:37:00 【白速龙王的回眸】
分析
不看逻辑看知识
Enum + auto
设置固定id的状态,可以理解为“类的静态状态变量”
用于判断和标识独有的状态,可用is判断
solts
关于solts的用法,也是可以理解为“类的静态变量”
类一旦做一个修改,所有的实例的值都会变
ac code
from enum import Enum, auto
class ExprStatus(Enum):
VALUE = auto() # 初始状态
NONE = auto() # 表达式类型未知
LET = auto() # let 表达式
LET1 = auto() # let 表达式已经解析了 vi 变量
LET2 = auto() # let 表达式已经解析了最后一个表达式 expr
ADD = auto() # add 表达式
ADD1 = auto() # add 表达式已经解析了 e1 表达式
ADD2 = auto() # add 表达式已经解析了 e2 表达式
MULT = auto() # mult 表达式
MULT1 = auto() # mult 表达式已经解析了 e1 表达式
MULT2 = auto() # mult 表达式已经解析了 e2 表达式
DONE = auto() # 解析完成
class Expr:
#__slots__ = 'status', 'var', 'value', 'e1', 'e2'
def __init__(self, status):
self.status = status
self.var = '' # let 的变量 vi
self.value = 0 # VALUE 状态的数值,或者 LET2 状态最后一个表达式的数值
self.e1 = self.e2 = 0 # add 或 mult 表达式的两个表达式 e1 和 e2 的数值
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 # 去掉空格
continue
if expression[i] == '(':
i += 1 # 去掉左括号
s.append(cur)
cur = Expr(ExprStatus.NONE)
continue
if expression[i] == ')': # 本质上是把表达式转成一个 token
i += 1 # 去掉右括号
if cur.status is ExprStatus.LET2:
token = str(cur.value)
for var in vars[-1]:
scope[var].pop() # 清除作用域
vars.pop()
elif cur.status is ExprStatus.ADD2:
token = str(cur.e1 + cur.e2)
else:
token = str(cur.e1 * cur.e2)
cur = s.pop() # 获取上层状态
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([]) # 记录该层作用域的所有变量, 方便后续的清除
elif token == "add":
cur.status = ExprStatus.ADD
elif token == "mult":
cur.status = ExprStatus.MULT
elif cur.status is ExprStatus.LET:
if expression[i] == ')': # let 表达式的最后一个 expr 表达式
cur.value = calculateToken(token)
cur.status = ExprStatus.LET2
else:
cur.var = token
vars[-1].append(token) # 记录该层作用域的所有变量, 方便后续的清除
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
总结
时隔几个月
又找到一道我连题解都不想看的每日一题
nice
边栏推荐
- Command injection of cisp-pte
- How can I code for 8 hours without getting tired.
- Analyze "C language" [advanced] paid knowledge [End]
- 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
- Vingt - trois mille feuilles? "Yang mou" derrière l'explosion de la consommation végétale
- ROS learning (XIX) robot slam function package cartographer
- 6 seconds to understand the book to the Kindle
- Infrared camera: juge infrared mag32 product introduction
- JVM memory model
- Word wrap when flex exceeds width
猜你喜欢
Decryption function calculates "task state and lifecycle management" of asynchronous task capability
ROS學習(23)action通信機制
Command injection of cisp-pte
JVM memory model
Integrated navigation: product description and interface description of zhonghaida inav2
15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient
Alibaba cloud middleware open source past
FLIR blackfly s usb3 industrial camera: white balance setting method
ROS learning (21) robot slam function package -- installation and testing of orbslam
TiFlash 源码阅读(四)TiFlash DDL 模块设计及实现分析
随机推荐
ROS学习(二十)机器人SLAM功能包——rgbdslam的安装与测试
Flir Blackfly S USB3 工业相机:白平衡设置方法
企业中台建设新路径——低代码平台
强化学习如何用于医学影像?埃默里大学最新《强化学习医学影像分析》综述,阐述最新RL医学影像分析概念、应用、挑战与未来方向
FLIR blackfly s industrial camera: synchronous shooting of multiple cameras through external trigger
CISP-PTE实操练习讲解(二)
sql中批量删除数据---实体中的集合
ROS学习(24)plugin插件
组合导航:中海达iNAV2产品描述及接口描述
ROS学习(26)动态参数配置
FLIR blackfly s industrial camera: explanation and configuration of color correction and code setting method
A new path for enterprise mid Platform Construction -- low code platform
将截断字符串或二进制数据
@Before, @after, @around, @afterreturning execution sequence
Blackfly s usb3 industrial camera: buffer processing
FLIR blackfly s usb3 industrial camera: white balance setting method
Seconds understand the delay and timing function of wechat applet
【论文阅读|深读】ANRL: Attributed Network Representation Learning via Deep Neural Networks
Domestic images of various languages, software and systems. It is enough to collect this warehouse: Thanks mirror
Flir Blackfly S 工业相机:配置多个摄像头进行同步拍摄