Last active
November 24, 2020 06:00
-
-
Save zhongql/863097e57a813db2dd368c4b1c5deaba to your computer and use it in GitHub Desktop.
时间计算
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
def calculate(params, tm): | |
ex_stack = [] | |
tmp = [] | |
mutil = [] | |
for p in params: | |
if (not p): | |
continue | |
if (p is '('): | |
ex_stack.append(p) | |
continue | |
if (p is '='): | |
# 不符合规则 | |
if (not ex_stack): | |
return '' | |
num_tmp = [] | |
for i in range(len(ex_stack)): | |
last = ex_stack[-1] | |
if (last.isdigit()): | |
num_tmp.insert(0, ex_stack.pop()) | |
str = ''.join(num_tmp) | |
tmp.append(str) | |
continue | |
if (p is '*' or p is 'x'): | |
# 不符合规则 | |
if (not ex_stack): | |
return '' | |
mutil.append(ex_stack.pop()) | |
continue | |
if (p.isdigit() or p is '-'): | |
if (tmp): | |
tmp.append(p) | |
elif (mutil): | |
mutil.append(p) | |
else: | |
ex_stack.append(p) | |
continue | |
if (p is '.'): | |
# 不符合规则 | |
if not (mutil and len(mutil) > 1): | |
return '' | |
mutil.append(p) | |
continue | |
if (p is ','): | |
# 不符合规则 | |
if not (tmp and len(tmp) > 1): | |
return '' | |
num = tmp[0] | |
# 不符合规则 | |
if (not num.isdigit()): | |
return '' | |
ex = ''.join(tmp[1:]) | |
rs = multi_num(ex, num) | |
if not rs: | |
return '' | |
tmp = [] | |
ex_stack.append(rs) | |
continue | |
if (p is ')'): | |
# 不符合规则 | |
if not (tmp and len(tmp) > 1): | |
return '' | |
num = tmp[0] | |
# 不符合规则 | |
if (not num.isdigit()): | |
return '' | |
ex = ''.join(tmp[1:]) | |
rs = multi_num(ex, num) | |
if not rs: | |
return '' | |
tmp = [] | |
ex_stack.append(rs) | |
if (ex_stack): | |
ex_tmp = [] | |
for i in range(len(ex_stack)): | |
last = ex_stack.pop() | |
if last is '(': | |
break | |
else: | |
ex_tmp.insert(0, last) | |
rs = add_num(ex_tmp) | |
ex_stack.append(rs) | |
# 不符合规则 | |
if not (mutil and len(mutil) > 1): | |
return '' | |
ex = mutil[0] | |
# 必须是60秒 | |
if (len(ex.replace(',', '').replace('-', '')) != 60): | |
return '' | |
num = ''.join(mutil[1:]) | |
rs = multi_num(ex, num) | |
if not rs: | |
return '' | |
ex_stack.append(rs) | |
# 表达式的全部的元素 | |
seri = add_num(ex_stack) | |
seri = seri.split(',') | |
# 时间换算 | |
tl = tm.split(':') | |
min = tl[0] | |
# 不符合规则 | |
if (not min.isdigit()): | |
return '' | |
total = int(min) * 60 | |
if(len(tl) == 2): | |
sec = tl[1] | |
# 不符合规则 | |
if (not sec.isdigit()): | |
return '' | |
total += int(sec) | |
return ','.join(seri[0:total]) | |
def multi_num(ex, num_str): | |
ex = ex.split(',') | |
nums = num_str.split('.') | |
min = 0 | |
sec = 0 | |
# 不符合规则 | |
if (len(nums) > 2): | |
return '' | |
if (len(nums) == 1): | |
min = nums[0] | |
if (not min.isdigit()): | |
return '' | |
min = int(min) | |
if (len(nums) == 2): | |
min = nums[0] | |
sec = nums[1] | |
if (not (sec.isdigit() and min.isdigit())): | |
return '' | |
min = int(min) | |
sec = int(sec[0]) | |
rs = [] | |
if (min): | |
for i in range(min): | |
rs.append(','.join(ex)) | |
if (sec): | |
num_len = sec * 6 | |
if (len(ex) < num_len): | |
return '' | |
rs.append(','.join(ex[:num_len])) | |
return ','.join(rs) | |
def add_num(ex_stack): | |
return ','.join(ex_stack) | |
if __name__ == "__main__": | |
params = sys.argv[1] | |
tm = sys.argv[2] | |
params = params.replace(' ', '').replace('(', '(').replace(',', ',').replace(')', ')') | |
tm = tm.replace(' ', '').replace(':', ':') | |
rs = calculate(params, tm) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
时间的表达式:
params = '(3=0,4=-1,3=1)+(10=1,47=0,3=-1)*10'
时间表达式:
tm = '2:10'
计算需要的参数:
calculate(params, tm)
得到的结果是以时间为准:
'000-1-1-1-1111111111111100000000000000000000000000000000000000000000000-1-1-111111111110000000000000000000000000000000000000000000'