0022-编程打卡:找出4位纯数字列表可组成最大有效时间
本帖最后由 不二如是 于 2022-9-19 19:05 编辑一星答案:
def late_clock(lst):
lst.sort(reverse = True)
list2 = lst[:]
if 2 in list2:
list2.remove(2)
list1 = []
list1.append(2)
for i in range(3, -1, -1):
if i in list2:
list2.remove(i)
list1.append(i)
if list2 < 6:
list1.extend(list2)
return '%d%d:%d%d'%tuple(list1)
if list2[-1] < 6:
list1.extend(reversed(list2))
return '%d%d:%d%d'%tuple(list1)
list2 = lst[:]
if 1 in list2:
list2.remove(1)
list1 = []
list1.append(1)
list1.append(list2)
list2.pop(0)
elif 0 in list2:
list2.remove(0)
list1 = []
list1.append(0)
list1.append(list2)
list2.pop(0)
else:
return ''
if list2 < 6:
list1.extend(list2)
return '%d%d:%d%d'%tuple(list1)
if list2[-1] < 6:
list1.extend(reversed(list2))
return '%d%d:%d%d'%tuple(list1)
return ''
print(late_clock())
二星答案:
#递归
def late_clock(digits):
def loc_num(arr, n):
req_num = req_idx = -1
for k in range(len(arr)):
if arr <= n:
if arr >= req_num:
req_num = arr
req_idx = k
arr = -1
return req_num
from copy import deepcopy
digits_copy = deepcopy(digits)
t = * 4
nl =
for i in range(len(nl)):
if i == 1:
t = loc_num(digits, nl) if t == nl else loc_num(digits, nl ** 2)
else:
t = loc_num(digits, nl)
if min(t) < 0:
nl =
t = ) for i in range(len(nl))]
return '%s%s:%s%s' % (t, t, t, t)
三星答案:
from datetime import time
from itertools import permutations
def late_clock(digits):
ret = []
for ll in permutations(digits):
try:
hr = ll * 10 + ll
mm = ll * 10 + ll
ret.append(time(hr, mm))
except ValueError: pass
return max(ret).strftime('%H:%M')
print(late_clock())
基础语法:
https://www.bilibili.com/video/BV1c4411e77t
算法讲解:
https://www.bilibili.com/video/BV1HT4y1K7DY
您发那么多主题干嘛啊,还不如多花时间写一些有意思的文章
页:
[1]