• Python三种方法计算指定日期是今年的第几天


    今天早上和腾讯面试官进行了视频面试,由于音量和网络以及我的垃圾电脑的原因,个人感觉黄了...

    最后面试官给了我一道简单的计算题:指定日期是今年的第几年

    由于电脑卡到打字都打不动,我勉勉强强写了一点,虽然面试官知道了我的想法也了解我的设备情况,最后没让我写完

    但是心里惭愧还是时候补齐了...话不多说回到主题吧

    首先是输入的问题,个人认为分别输入年月份是一件很初级的要求,就实现了形如“2020-3-26”的字符串解析的两种方法,代码如下:

     1 def cal_date_str_spilt(date):
     2     ''''
     3     处理形如"2020-3-26"
     4     使用字符串的spilt方法解析
     5     '''
     6     _year = int(date.split('-')[0])
     7     _month = int(date.split('-')[1])
     8     _day = int(date.split('-')[2])
     9     return [_year, _month, _day]
    10 
    11 def cal_date_str_time(date):
    12     '''
    13     使用time库内置函数strptime(string, format) return struct_time对象
    14     传入参数:字符串 + 处理格式
    15     '''
    16     _date = time.strptime(date, '%Y-%m-%d')
    17     _year = _date.tm_year
    18     _month = _date.tm_mon
    19     _day = _date.tm_mday
    20     return [_year, _month, _day]

    然后判断是否闰年

    1 def judge_leap_year(year, month):
    2     # 只有闰年且月份大于2月才加多一天
    3     if year % 400 == 0 or year % 100 and year % 4 == 0 and month > 2:
    4         return 1
    5     else:
    6         return 0

    主函数

     1 def main():
     2     date = input("请输入日期,以'-'分隔:")
     3     sum_1, sum_2 = 0, 0
     4     date_list_1 = cal_date_str_spilt(date)
     5     date_list_2 = cal_date_str_time(date)
     6 
     7     month_day = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
     8     month_day_lep = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
     9 
    10     sum_1 += sum(month_day[:date_list_1[1] - 1]) + date_list_1[2] + judge_leap_year(date_list_1[0], date_list_1[1])
    11     sum_2 += sum(month_day[:date_list_2[1] - 1]) + date_list_2[2] + judge_leap_year(date_list_2[0], date_list_2[1])
    12     print('今天是今年的第' + str(sum_1) + '')
    13     print('今天是今年的第' + str(sum_2) + '')
    14     
    15     '''
    16     这一段是使用了datetime库的方法,python本身就有处理该类问题的方法
    17     '''
    18     _sum = datetime.date(date_list_1[0], date_list_1[1], date_list_1[2])
    19     sum_3 = _sum.strftime('%j')
    20     if sum_3[0] == '0' and sum_3[1] == '0':
    21         print('今天是今年的第' + str(sum_3[-1:]) + '')
    22     elif sum_3[0] == '0':
    23         print('今天是今年的第' + str(sum_3[-2:]) + '')
    24     else:
    25         print('今天是今年的第' + str(sum_3) + '')
    26 
    27 if __name__ == '__main__':
    28     main()

    以下是全部代码:

    import datetime
    import time
    
    def cal_date_str_spilt(date):
        ''''
        处理形如"2020-3-26"
        使用字符串的spilt方法解析
        '''
        _year = int(date.split('-')[0])
        _month = int(date.split('-')[1])
        _day = int(date.split('-')[2])
        return [_year, _month, _day]
    
    def cal_date_str_time(date):
        '''
        使用time库内置函数strptime(string, format) return struct_time对象
        传入参数:字符串 + 处理格式
        '''
        _date = time.strptime(date, '%Y-%m-%d')
        _year = _date.tm_year
        _month = _date.tm_mon
        _day = _date.tm_mday
        return [_year, _month, _day]
    
    def judge_leap_year(year, month):
        # 只有闰年且月份大于2月才加多一天
        if year % 400 == 0 or year % 100 and year % 4 == 0 and month > 2:
            return 1
        else:
            return 0
    
    def main():
        date = input("请输入日期,以'-'分隔:")
        sum_1, sum_2 = 0, 0
        date_list_1 = cal_date_str_spilt(date)
        date_list_2 = cal_date_str_time(date)
    
        month_day = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
        month_day_lep = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    
        sum_1 += sum(month_day[:date_list_1[1] - 1]) + date_list_1[2] + judge_leap_year(date_list_1[0], date_list_1[1])
        sum_2 += sum(month_day[:date_list_2[1] - 1]) + date_list_2[2] + judge_leap_year(date_list_2[0], date_list_2[1])
        print('今天是今年的第' + str(sum_1) + '')
        print('今天是今年的第' + str(sum_2) + '')
    
        '''
        这一段是使用了datetime库的方法,python本身就有处理该类问题的方法
        '''
        _sum = datetime.date(date_list_1[0], date_list_1[1], date_list_1[2])
        sum_3 = _sum.strftime('%j')
        if sum_3[0] == '0' and sum_3[1] == '0':
            print('今天是今年的第' + str(sum_3[-1:]) + '')
        elif sum_3[0] == '0':
            print('今天是今年的第' + str(sum_3[-2:]) + '')
        else:
            print('今天是今年的第' + str(sum_3) + '')
    
    if __name__ == '__main__':
        main()
  • 相关阅读:
    [WC2010]重建计划
    [POJ1741]Tree
    [NOI2008]志愿者招募
    [BZOJ2127]happiness
    「网络流 24 题」太空飞行计划
    [TJOI2015]线性代数
    [HDU2874]Connections between cities
    [POI2007]ZAP-Queries
    [SCOI2010]幸运数字
    POJ 2826 An Easy Problem?!
  • 原文地址:https://www.cnblogs.com/misaki-workshop/p/12573838.html
Copyright © 2020-2023  润新知