鱼C论坛

 找回密码
 立即注册
123
返回列表 发新帖
楼主: ooxx7788

[技术交流] Python:每日一题 39

[复制链接]
发表于 2017-5-10 10:42:06 | 显示全部楼层
开始return用的是
  1. return str(bx[i][0]*10+bx[i][1])+':'+str(bx[i][2]*10+bx[i][3])
复制代码
,发现这样多个0的时候会造成秒数显示不对,改成了上面这样,这个方法感觉还行
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-6-27 19:37:07 | 显示全部楼层
按照排列组合来处理
  1. import itertools

  2. # 生成全排列也就 4! 次,然后按时间格式过滤,再按数字对比大小
  3. def clock_1(a):
  4.     """传入列表a,尝试用a中有且仅有的4个元素组成最晚时间(24小时制)"""
  5.     pers = itertools.permutations(a,4)
  6.     per = list(filter(lambda x: 10*x[0]+x[1] <24 and 10*x[2]+x[3]<60, pers))
  7.     if len(per)!=0:
  8.         output = "".join([str(i) for i in max(per)])
  9.         return output[:2]+":"+output[2:]
  10.     else:
  11.         return ""
复制代码

不用工具,几重嵌套循环除了外观不好,结果还是可以的
  1. def clock_2(a):
  2.     s = []
  3.     for i in a:
  4.         b = a[:]
  5.         b.remove(i)
  6.         for j in b:
  7.             c = b[:]
  8.             c.remove(j)
  9.             for k in c:
  10.                 d = c[:]
  11.                 d.remove(k)
  12.                
  13.                 hours,minutes = i*10+j, k*10+d[0]
  14.                 if hours<24 and minutes<60:
  15.                     s.append((hours,minutes))
  16.                 else:
  17.                     continue

  18.     if s ==[]:
  19.         return ""
  20.     else:
  21.         output = max(s) if len(s)!=0 else ""        
  22.         return str(output[0])+":"+str(output[1])
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-12-5 13:56:19 | 显示全部楼层
  1. def assert_equals(func, target, *args):
  2.     if func == target:
  3.         print('Success!')
  4.     else:
  5.         print('Fail!{0} not equals {1}'.format(func, target))
  6.         print(*args)

  7. def late_clock(lst):
  8.     allTimes = []
  9.     for i in range(4):
  10.         for j in range(4):
  11.             for k in range(4):
  12.                 for l in range(4):
  13.                     if (i!=j and i!=k and i!=l) and (j!=k and j!=l) and k!=l:
  14.                         allTimes.append((str(lst[i])+str(lst[j])+str(lst[k])+str(lst[l])))
  15.     legalTimes = []
  16.     for time in allTimes:
  17.         if not (int(time[0:2]) > 23 or int(time[2:4]) > 59):
  18.             legalTimes.append(time)
  19.     if legalTimes == []:
  20.         return ''
  21.     else:
  22.         legalTimes = sorted(legalTimes, key = lambda x:int(x))
  23.         return legalTimes[-1][0:2]+':'+legalTimes[-1][2:4]
  24.    
  25.    

  26. assert_equals(late_clock([9, 1, 2, 5]), '21:59')
  27. assert_equals(late_clock([0, 2, 2, 2]), '22:20')
  28. assert_equals(late_clock([9, 0, 1, 1]), '19:10')
  29. assert_equals(late_clock([2, 3, 2, 4]), '23:42')
  30. assert_equals(late_clock([1, 2, 8, 9]), '19:28')
  31. assert_equals(late_clock([0, 0, 0, 0]), '00:00')
  32. assert_equals(late_clock([4, 7, 8, 9]), '')
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-11 23:46:13 | 显示全部楼层
  1. import random, re, itertools

  2. randomnum = [random.randint(0, 9) for x in range(4)]
  3. print('\n当前随机数:{0}'.format(randomnum), end='    ')
  4. pac = list(set(itertools.permutations(randomnum, 4)))
  5. timestr = [str(i[0]) + str(i[1]) + str(i[2]) + str(i[3]) for i in pac]
  6. restr = re.finditer(r"(20|21|22|23|[0-1]\d)[0-5]\d", ' '.join(timestr))
  7. result = sorted([x.group() for x in restr], reverse=True)
  8. if len(result) > 0:
  9.     print("所能组成的最晚时间是:{0}:{1}".format(result[0][0:2], result[0][2:4]))
  10. else:
  11.     print("当前随机数不能组成有效的时间")
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-16 16:06:29 | 显示全部楼层
from itertools import permutations

def M_clock(list1):
        P_clock = list(permutations(list1))
        tuple1 = (0,0,0,0)
        if (2,4,0,0) in P_clock:
                print('24:00')
        else:
                for each in P_clock:
                        print(each)
                        h = each[0] * 10 + each[1]
                        m = each[2] * 10 + each[3]
                        if h < 24 and m <60 and each > tuple1:
                                tuple1 = each
        if tuple1 == (0,0,0,0):
                return '4个数字无法组成时间'
        return '%s%s:%s%s' % (tuple1[0],tuple1[1],tuple1[2],tuple1[3])
       
print(M_clock([9,6,3,1]))
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-21 12:53:07 | 显示全部楼层
  1. '''
  2.         一个列表,有四个数,例如[2,3,1,6],把这四个数字组成一个时间格式
  3.         并使得这个时间是所有组合中最晚的组合。例如[2,3,1,6]可以组成,13:26,16:23等等
  4.         但是最晚的时间是23:16。所以请返回23:16这个结果。如果四个数无法组成时间格式,返回""。
  5. '''
  6. def late_clock(listx):
  7.     lenx = len(listx)
  8.     c = 0
  9.     lx=sorted(listx)
  10.     for i in range(lenx):
  11.         if listx[i] > 5:
  12.             c = c+1
  13.     if c > 2 or min(listx) >2:
  14.         print ('四个数字无法组成时间格式')
  15.         return ''
  16.     if (lx[0] == 2 and lx[1] > 4) or lx[2] > 5:
  17.         print ('四个数字无法组成时间格式')
  18.         return ''
  19.     else:
  20.         ls=str(lx[0])+str(lx[1])+':'+str(lx[2])+str(lx[3])
  21.         print(ls)
  22.         return ls

  23. if __name__ == '__main__':
  24.     late_clock([2,0,2,2])
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-6-6 20:30:52 | 显示全部楼层
又回来喽
  1. import test
  2. import itertools

  3. def late_clock(x):
  4.     result = itertools.permutations(x,4)
  5.     ans = {}
  6.     for i in result:
  7.         shi = ''
  8.         fen = ''
  9.         for j in range(4):
  10.             if j < 2 :
  11.                 shi += str(i[j])
  12.             else :
  13.                 fen += str(i[j])
  14.         time = '%s:%s' %(shi,fen)
  15.         if int(shi) <= 23 and int(fen) <= 59:
  16.             ans[time] = int(shi) * 3600 + int(fen) * 60
  17.     try :
  18.         return max(ans)
  19.     except(ValueError):
  20.         return ''
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-1-7 19:41:26 | 显示全部楼层
好像我的思路和大佬们的不同,递归不会用orz

  1. def fun39(list1):   
  2.     hour1 = sorted(list1,key = lambda x :x if x>=0 and x<=2 else -1 ).pop()
  3.     list1.remove(hour1)
  4.     hour2 = sorted(list1,key = lambda x :x if x>=0 and x<=3 else -1 ).pop()
  5.     list1.remove(hour2)
  6.     min1 = sorted(list1,key = lambda x :x if x>=0 and x<=5 else -1 ).pop()
  7.     list1.remove(min1)
  8.     min2 = list1[0]

  9.     hour = str(hour1) + str(hour2)
  10.     minute = str(min1) + str(min2)
  11.     if 0 <= int(hour) <= 23 and 0 <= int(minute) <= 59:
  12.         print(hour + ":" + minute)
  13.     else:
  14.         print(r'""')
  15.    


  16. fun39([3,5,2,9])
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-8 08:33:36 | 显示全部楼层
写的好粗糙
  1. def late_clock(time_list):
  2.           result = []
  3.           if 2 in time_list:
  4.                     a = 20
  5.                     time_list.remove(2)
  6.           elif 1 in time_list:
  7.                     a = 10
  8.                     time_list.remove(1)
  9.           else:
  10.                     return ''
  11.           if a == 10:
  12.                     #print(time_list)
  13.                     b = max(time_list)
  14.                     time_list.remove(b)
  15.           else:
  16.                     temp = []
  17.                     for each in time_list:
  18.                               if 0 < each and each <4:
  19.                                         temp.append(each)
  20.                     b = max(temp)
  21.                     time_list.remove(b)
  22.           #print(a)
  23.           temp = []
  24.           for each in time_list:
  25.                     if 0<= each and each <= 6:
  26.                              temp.append(each)
  27.           #print(time_list)
  28.           #print(temp)
  29.           if temp == []:
  30.                     time_list.append(2)
  31.                     a =10
  32.                     time_list.append(b)
  33.                     b = max(time_list)
  34.                     time_list.remove(b)
  35.                     temp = []
  36.                     for each in time_list:
  37.                               if 0<= each and each <= 6:
  38.                                        temp.append(each)

  39.           c = max(temp)
  40.           time_list.remove(c)
  41.           d = max(time_list)
  42.           time_list.remove(d)
  43.           clock = a+b
  44.           time = c*10+d
  45.           result = str(clock)+':'+str(time)
  46.           return result
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-10 23:58:13 | 显示全部楼层
from itertools import permutations
num=list(input("请输入四个整数:\n"))
arr=[2,3,5,9]
brr=[]
for i in arr:
    t=max([int(j) for j in num if int(j)<=i])
    brr.append(t)
    num.remove(str(t))
print("The biggest time is "+str(brr[0])+str(brr[1])+":"+str(brr[2])+str(brr[3]))
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-12-7 14:42:15 | 显示全部楼层
import itertools as t
def Fun39(a,b,c,d):
    L = []
    L1 = []
    L2 = []
    L3 = []
    S = ""
    Maximumtime = 0
    a =  t.permutations([a,b,c,d],4)
    for i in a:
        L.append(i)
    for k in range(len(L)):
        if L[k][0]<=2 and L[k][1]<=4 and L[k][2]<=5:
            L1.append(L[k])
    if L1==[]:
        print("所给的数不能组成时间格式。")
    else:
        for j in L1:
            print(j)
            for g in range(len(j)):
                S +=(str(j[g]))
            L2.append(int(S))
            S = ""
            Maximum =max(L2)
            Maximumtime = str(Maximum)[0:2] + ":" + str(Maximum)[2:4]
    return Maximumtime
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-30 17:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表