Last active
November 24, 2020 06:00
-
-
Save zhongql/863097e57a813db2dd368c4b1c5deaba to your computer and use it in GitHub Desktop.
时间计算
This file contains hidden or 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 (tmp and len(tmp) > 1): | |
| return '' | |
| num = tmp[0] | |
| # 不符合规则 | |
| if (not num.isdigit()): | |
| return '' | |
| ex = ''.join(tmp[1:]) | |
| rs = multi_num(ex, int(num)) | |
| 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, int(num)) | |
| 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 | |
| elif last is not '(': | |
| 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] | |
| num = ''.join(mutil[1:]) | |
| # 不符合规则 | |
| if (not num.isdigit()): | |
| return '' | |
| rs = multi_num(ex, int(num)) | |
| ex_stack.append(rs) | |
| # 表达式的全部的元素 | |
| seri = add_num(ex_stack) | |
| seri = seri.replace(',', '') | |
| # 时间换算 | |
| 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 seri[0:total] | |
| def multi_num(ex, num): | |
| rs = [] | |
| for i in range(num): | |
| rs.append(str(ex)) | |
| return ','.join(rs) | |
| def add_num(ex_stack): | |
| return ','.join(ex_stack) | |
| if __name__ == "__main__": | |
| params = sys.argv[1] | |
| tm = sys.argv[2] | |
| print('start !!!') | |
| params = params.replace(' ', '').replace('(', '(').replace(',', ',').replace(')', ')') | |
| tm = tm.replace(' ', '').replace(':', ':') | |
| rs = calculate(params, tm) | |
| print(rs) |
Author
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'