zoukankan      html  css  js  c++  java
  • python之判断合法日期

    年月日分别为自定义函数的参数,判断某一个日期是否为合法的日期;
    如: 2020年12月33日不是合法的日期
    2021年2月29日是不合法的日期

    看代码:方法一
    def fn3(year, month, day):
        if month > 12 or month <= 0:
            return "%s年%s月%s日不是合法日期"%(year,month,day)
        if num_6(year):
            months = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
            if day <= months[month-1]:
                return "合法日期"
            else:
                return "%s年%s月%s日不是合法日期" % (year, month, day)
        else:
            month2 = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
            if day <= month2[month - 1]:
                 return "合法日期"
            else:
                return "%s年%s月%s日不是合法日期" % (year, month, day)
    
    # print(fn3(2020, 1, 31))
    
    #方法二
    def youhua(year, month, day): if month < 1 or month >12: return False days = 31 if month in [4,6,9,11]: days = 30 elif month == 2: if num_6(year): days = 29 else: days = 28 if day < 1 and day>days: return False return True # print(youhua(2020, 1, 31))

    两种方法,前面的思路更加复杂
    后面的复杂度会好很多,哎,逻辑思维还需要提高....................
    写代码太难了,冲冲冲
    耐得住寂寞,守得住繁华
  • 相关阅读:
    二分搜素——(lower_bound and upper_bound)
    二分(搜索)查找
    算法复杂度中的O(logN)底数是多少
    hdu 1050 Moving Tables
    hdu 1010 Tempter of the Bone
    hud 3123 GCC
    “123”——> 123
    基本模运算
    101个MySQL的调节和优化的Tips
    一个最简单的图片缩略图
  • 原文地址:https://www.cnblogs.com/yunzhongjunlang/p/13967861.html
Copyright © 2011-2022 走看看