init
This commit is contained in:
63
Q2/code.py
Normal file
63
Q2/code.py
Normal file
@ -0,0 +1,63 @@
|
||||
# code.py
|
||||
# Please use env.yaml to build corresponding conda env if you want to run this script
|
||||
#
|
||||
# author : deng
|
||||
# date : 20230919
|
||||
# platform: MacBook Pro 14 2021
|
||||
|
||||
VALID_CHARS = ['T', 'F', 'O', 'A', '(', ')']
|
||||
CAL_DICT = {
|
||||
'T': 'T',
|
||||
'F': 'F',
|
||||
'TAT': 'T',
|
||||
'TAF': 'F',
|
||||
'FAT': 'F',
|
||||
'FAF': 'F',
|
||||
'TOT': 'T',
|
||||
'TOF': 'T',
|
||||
'FOT': 'T',
|
||||
'FOF': 'F'
|
||||
}
|
||||
|
||||
|
||||
def log_cal(exp: str) -> str:
|
||||
while True:
|
||||
if exp in CAL_DICT:
|
||||
return CAL_DICT[exp]
|
||||
else:
|
||||
try:
|
||||
exp = CAL_DICT[exp[:3]] + exp[3:]
|
||||
except KeyError:
|
||||
return 'E'
|
||||
|
||||
|
||||
def bool_exp(exp: str) -> str:
|
||||
stack = []
|
||||
|
||||
for char in exp:
|
||||
if char not in VALID_CHARS:
|
||||
return 'E'
|
||||
if char == ')':
|
||||
sub_exp = ''
|
||||
while True:
|
||||
try:
|
||||
val = stack.pop()
|
||||
if val == '(':
|
||||
break
|
||||
sub_exp = val + sub_exp
|
||||
except IndexError:
|
||||
return 'E'
|
||||
cal_exp = log_cal(sub_exp)
|
||||
if cal_exp == 'E':
|
||||
return 'E'
|
||||
stack.append(cal_exp)
|
||||
else:
|
||||
stack.append(char)
|
||||
|
||||
return log_cal(''.join(stack))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
exp = input('Please input expression:').upper()
|
||||
print(bool_exp(exp))
|
20
Q2/env.yaml
Normal file
20
Q2/env.yaml
Normal file
@ -0,0 +1,20 @@
|
||||
name: Q2
|
||||
channels:
|
||||
- conda-forge
|
||||
dependencies:
|
||||
- bzip2=1.0.8
|
||||
- ca-certificates=2023.7.22
|
||||
- libffi=3.4.2
|
||||
- libsqlite=3.43.0
|
||||
- libzlib=1.2.13
|
||||
- ncurses=6.4
|
||||
- openssl=3.1.2
|
||||
- pip=23.2.1
|
||||
- python=3.10.12
|
||||
- readline=8.2
|
||||
- setuptools=68.2.2
|
||||
- tk=8.6.12
|
||||
- tzdata=2023c
|
||||
- wheel=0.41.2
|
||||
- xz=5.2.6
|
||||
prefix: /Users/xiao_deng/miniforge3/envs/Q2
|
Reference in New Issue
Block a user