Python练习(Q7) 日起的二进制转换

题目:(题目来源:程序员的算法趣题)

直接上代码:

import datetime
from dateutil import parser

def CountDate(startdate,enddate):
    result=[]
    
    deltaday=datetime.timedelta(days=1)
    while(startdate!=enddate):
        startdate_v=startdate.year*10**4+startdate.month*10**2+startdate.day    
        datedaybin=bin(startdate_v)
        datedaybin_str=str(datedaybin)
        datedaybin_str=datedaybin_str[2:]
        datedaybin_str=datedaybin_str[::-1]
        datedaybin_str="0b"+datedaybin_str       
        dateday_inv=int(datedaybin_str,2)
        if(dateday_inv==startdate_v):
            result.append((startdate))
        startdate=startdate+ deltaday  
    return result 

print("*"*100)
starttime=datetime.datetime.now()
predate=parser.parse('1964-10-10').date()    
afterdate=parser.parse('2021-7-10').date() 
result=CountDate(predate,afterdate)   
endtime=datetime.datetime.now()
spendtime=endtime-starttime  
print("Result is:") 
print(result)          
print("CountDate(predate,afterdate)  Spend time:{}".format(spendtime))   

结果:

 

 

 

posted on 2021-11-30 14:57  半路敲代码  阅读(45)  评论(0编辑  收藏  举报

导航