Python:每日一题 39

2017-11-27 13:25:00
六月
来源:
http://bbs.fishc.com/thread-86832-1-1.html
转贴 586
今天的题目比较有意思。
会给你一个列表,有四个数,[2,3,1,6]
你需要做的是,把这四个数字组成一个时间格式,并使得这个时间是所有组合中最晚的组合。
例如[2,3,1,6]可以组成,13:26,16:23等等,但是最晚的时间是23:16。所以请返回23:16这个结果。
如果四个数无法组成时间格式,返回""。
答案一样会延迟放出。你猜我到底是会还是不会呢?

测试代码如下,test.py还是请去33,34中寻找。

  1. test.assert_equals(late_clock([9, 1, 2, 5]), '21:59')
  2. test.assert_equals(late_clock([0, 2, 2, 2]), '22:20')
  3. test.assert_equals(late_clock([9, 0, 1, 1]), '19:10')
  4. test.assert_equals(late_clock([2, 3, 2, 4]), '23:42')
  5. test.assert_equals(late_clock([1, 2, 8, 9]), '19:28')
  6. test.assert_equals(late_clock([4, 7, 8, 9]), '')
复制代码


这么多大佬发过了,我也发个我的吧。就不设置隐藏了。
  1. def late_clock(digits):
  2.     """
  3.     有四个数,[2,3,1,6],把这四个数字组成一个时间格式,并使得这个时间是所有组合中最晚的组合。
  4.     """
  5.     from itertools import permutations
  6.     temp = sorted([each[:2]+":"+each[2:] for each in [''.join(map(str, each)) for each in list(permutations(digits))]])
  7.     result = []
  8.     for each in temp:
  9.         if int(each[0:2]) < 24 and int(each[3:]) < 60:
  10.             result.append(each)
  11.     return result[-1] if result else ''
复制代码


再发送几个评分比较高的代码。给大家参考。
  1. 这是和我比较相似的,筛选类的。
  2. from itertools import permutations

  3. def late_clock(digits):
  4.   for p in permutations(sorted(digits, reverse=True)):
  5.     if p[0] > 2 or (p[0] == 2 and p[1] > 3) or p[2] > 5: continue
  6.     return '{}{}:{}{}'.format(*p)
复制代码

  1. 这是正则筛选的。
  2. import re
  3. from itertools import permutations
  4. def late_clock(digits):
  5.     s = max(a for a in (''.join(map(str, a)) for a in permutations(digits)) if re.fullmatch(r'(?:2[0-3]|[01]d)[0-5]d', a))
  6.     return '%s:%s' % (s[:2], s[2:])
复制代码

  1. 这是递归式的。我自己最开始写的也是一个递归式的。但是无法通过[1,2,8,9]这种组合,不过这位完成了,不过从代码上面来看,不太好懂。
  2. def late_clock(digits):
  3.     def loc_num(arr, n):
  4.         req_num = req_idx = -1
  5.         for k in range(len(arr)):
  6.             if arr[k] <= n:
  7.                 if arr[k] >= req_num:
  8.                     req_num = arr[k]
  9.                     req_idx = k
  10.         arr[req_idx] = -1
  11.         return req_num

  12.     from copy import deepcopy
  13.     digits_copy = deepcopy(digits)

  14.     t = [0] * 4
  15.     nl = [2, 3, 5, 9]
  16.     for i in range(len(nl)):
  17.         if i == 1:
  18.             t[i] = loc_num(digits, nl[i]) if t[i - 1] == nl[i - 1] else loc_num(digits, nl[i] ** 2)
  19.         else:
  20.             t[i] = loc_num(digits, nl[i])

  21.     if min(t) < 0:
  22.         nl = [1, 9, 5, 9]
  23.         t = [loc_num(digits_copy, nl[i]) for i in range(len(nl))]

  24.     return '%s%s:%s%s' % (t[0], t[1], t[2], t[3])
复制代码

  1. 还有这种,使用datetime进行格式筛选的。也是不错的方法。
  2. from datetime import time
  3. from itertools import permutations

  4. def late_clock(digits):
  5.     ret = []
  6.     for ll in permutations(digits):
  7.         try:
  8.             hr = ll[0] * 10 + ll[1]
  9.             mm = ll[2] * 10 + ll[3]
  10.             ret.append(time(hr, mm))
  11.         except ValueError: pass
  12.     return max(ret).strftime('%H:%M')
发表评论
评论通过审核后显示。