Skip to content

Instantly share code, notes, and snippets.

@GuoJing
Forked from zhongql/film_clean.py
Created November 16, 2020 02:27
Show Gist options
  • Save GuoJing/d36a71dc967adddbfd4d5ddfb9d82eb4 to your computer and use it in GitHub Desktop.
Save GuoJing/d36a71dc967adddbfd4d5ddfb9d82eb4 to your computer and use it in GitHub Desktop.
时间计算
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment