Cron表达式 python
·
这段代码实现了一个时间组合计算器,主要功能是解析输入的时间格式并计算可能的组合数量。程序首先处理输入字符串,将其分割为列表。然后通过三种方式解析时间元素:通配符(*)返回预设范围,连字符(-)解析为连续数字区间,逗号(,)分隔为多个数值。最后计算前三项的组合数,并结合月份天数处理得出总组合数1095。代码展示了灵活的时间格式解析和组合计算能力,适用于需要处理多种时间表达式的场景。


def dataType(pos):
# 位置的不同 *返回的值也不同
if pos == 1 or pos == 2 :
return list(range(0,60))
elif pos == 3:
return list(range(0,24))
elif pos == 4:
return list(range(1,32))
elif pos == 5:
return list(range(1,13))
return None
def num_num(string):
if '-' in string:
strs = string.split('-')
return list(range(int(strs[0]),int(strs[1]) + 1))
return None
def num_d(string):
if ',' in string:
strs = string.split(',')
return list(map(int,strs))
return None
# 4 2 1,3,15 1-31 *
lst = list(input().split())
print(lst)
result = []
for index,string in enumerate(lst):
if string=='*':
tmp =dataType(index+1)
if tmp:
result.append(tmp)
continue
tmp = num_num(string)
if tmp:
result.append(tmp)
continue
tmp = num_d(string)
if tmp:
result.append(tmp)
continue
result.append([int(string)])
print(result)
res = 1
for index,lst in enumerate(result):
if index == 3:
break
res *= len(lst)
print(res)
days = 0
for month in result[4]:
day = len(result[3])
if month not in [1,3,5,7,8,10,12]:
if 31 in result[3]:
day -= 1
days += day
# 处理2月
if 2 in result[4]:
if 29 in result[3]:
days -= 1
if 2 in result[4]:
if 30 in result[3]:
days -= 1
res *= days
print(res)
4 2 1,3,15 1-31 *
[‘4’, ‘2’, ‘1,3,15’, ‘1-31’, ‘*’]
[[4], [2], [1, 3, 15], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]]
3
1095
更多推荐


所有评论(0)