马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 不二如是 于 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[0] < 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[0])
list2.pop(0)
elif 0 in list2:
list2.remove(0)
list1 = []
list1.append(0)
list1.append(list2[0])
list2.pop(0)
else:
return ''
if list2[0] < 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([2,3,1,6]))
二星答案:
#递归
def late_clock(digits):
def loc_num(arr, n):
req_num = req_idx = -1
for k in range(len(arr)):
if arr[k] <= n:
if arr[k] >= req_num:
req_num = arr[k]
req_idx = k
arr[req_idx] = -1
return req_num
from copy import deepcopy
digits_copy = deepcopy(digits)
t = [0] * 4
nl = [2, 3, 5, 9]
for i in range(len(nl)):
if i == 1:
t[i] = loc_num(digits, nl[i]) if t[i - 1] == nl[i - 1] else loc_num(digits, nl[i] ** 2)
else:
t[i] = loc_num(digits, nl[i])
if min(t) < 0:
nl = [1, 9, 5, 9]
t = [loc_num(digits_copy, nl[i]) for i in range(len(nl))]
return '%s%s:%s%s' % (t[0], t[1], t[2], t[3])
三星答案:
from datetime import time
from itertools import permutations
def late_clock(digits):
ret = []
for ll in permutations(digits):
try:
hr = ll[0] * 10 + ll[1]
mm = ll[2] * 10 + ll[3]
ret.append(time(hr, mm))
except ValueError: pass
return max(ret).strftime('%H:%M')
print(late_clock([1,2,4,5]))
基础语法:
算法讲解:
|