1. """
  2. 题目:输入某年某月某日,判断这一天是这一年的第几天?
  3. """
  4. import datetime
  5. import time
  6. from functools import reduce
  7.  
  8. def calculate1(t):
  9. """
  10. 直接利用python的datetime模块计算
  11. :param t:
  12. :return:
  13. """
  14. print("计算一", end=":")
  15. print(t.strftime("%j"))
  16.  
  17. def calculate2(t):
  18. """
  19. 自己手动计算一下
  20. :param t:
  21. :return:
  22. """
  23. print("计算二", end=":")
  24. days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  25. daysLeap = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  26. year = t.year
  27. if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):
  28. print(sum(daysLeap[:t.month - 1]) + t.day)
  29. else:
  30. print(sum(days[:t.month - 1]) + t.day)
  31.  
  32. def calculate3(t):
  33. """
  34. 高手简化后的calculate2
  35. :param t:
  36. :return:
  37. """
  38. print("计算三", end=":")
  39. days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  40. year = t.year
  41. if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):
  42. days[1] = 29
  43. print(sum(days[0: t.month - 1]) + t.day)
  44.  
  45. def calculate4(t):
  46. """
  47. 利用字典来计算
  48. :param t:
  49. :return:
  50. """
  51. print("计算四", end=":")
  52. dayDict = {0: 0, 1: 31, 2: 59, 3: 90, 4: 120, 5: 151, 6: 181, 7: 212, 8: 243, 9: 273, 10: 304, 11: 334, 12: 365}
  53. year = t.year
  54. d = dayDict[t.month - 1] + t.day
  55. if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):
  56. d += 1
  57. print(d)
  58.  
  59. def calculate5(t):
  60. """
  61. 利用time模块来计算,注意和datetime模块进行区分
  62. :param t:
  63. :return:
  64. """
  65. print("计算五", end=":")
  66. t = time.strptime(t.strftime("%Y-%m-%d"), "%Y-%m-%d")
  67. print(t[7])
  68.  
  69. def calculate6(t):
  70. """
  71. 利用datetime的时间相减来计算
  72. :param t:
  73. :return:
  74. """
  75. print("计算六", end=":")
  76. t1 = datetime.date(t.year, 1, 1)
  77. t2 = t - t1
  78. print(t2.days + 1)
  79.  
  80. def calculate7(t):
  81. """
  82. 利用time的时间相减来计算,注意与datetime进行区分,它不能直接减,需要转成时间戳才能减
  83. 以为时间戳是以1970年为基点计算的,所以该方法只能计算1970以后(不包括1970)的时间
  84. :param t:
  85. :return:
  86. """
  87. print("计算七", end=":")
  88. t1 = time.strptime(t.strftime("%Y-01-01"), "%Y-%m-%d")
  89. t1 = time.mktime(t1)
  90. t = time.strptime(t.strftime("%Y-%m-%d"), "%Y-%m-%d")
  91. t = time.mktime(t)
  92. t2 = t - t1
  93. t2 = t2 // (3600 * 24)
  94. print(int(t2) + 1)
  95.  
  96. def calculate8(t):
  97. """
  98. 利用reduce函数来计算,中间有用到三元运算符
  99. 在Python 3里,reduce()函数已经被从全局名字空间里移除了,它现在被放置在fucntools模块里
  100. 用的话要 先引入 from functools import reduce
  101. :param t:
  102. :return:
  103. """
  104. print("计算八", end=":")
  105. year = t.year
  106. days = [0, 31, 28 if year % 4 else 29 if year % 100 else 28 if year % 400 else 29, 31, 30, 31, 30, 31, 31, 30, 31,
  107. 30, 31]
  108. print(reduce(lambda a, b: a + b, days[0: t.month]) + t.day)
  109.  
  110. def calculate9(t):
  111. """
  112. 利用位运算来计算闰年:
  113. 分析 year&3 等价于 year%4:因为二进制转十进制是:2**0+2**1+2**2+。。。,可见2**2之后的都可以被4整除
  114. 同理 year&15 等价 year%16
  115. 根据闰年计算规则我们可以知道:不能被4整除的年份肯定不是闰年,而能被4整除又能被25整数但不能再被16整数的也不是闰年,其余全是闰年
  116. 可得 year%4 or year%16 and !year%25 这些都不是闰年,反之!(year%4 or year%16 and !year%25)为闰年
  117. 转为位运算!(year&3 or year&15 and !(year%25))
  118. :param t:
  119. :return:
  120. """
  121. print("计算九", end=":")
  122. days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  123. year = t.year
  124. if not(year & 3 or year & 15 and not(year % 25)):
  125. days[1] = 29
  126. print(sum(days[0: t.month - 1]) + t.day)
  127.  
  128. def answer():
  129. """
  130. 通过try来判断输入的日期是否正确
  131. :return:
  132. """
  133.  
  134. year = input("输入年:")
  135. if year == "q":
  136. return
  137. month = input("输入月:")
  138. day = input("输入日:")
  139. try:
  140. t = datetime.date(int(year), int(month), int(day))
  141. calculate1(t)
  142. calculate2(t)
  143. calculate3(t)
  144. calculate4(t)
  145. calculate5(t)
  146. calculate6(t)
  147. calculate7(t)
  148. calculate8(t)
  149. calculate9(t)
  150. except ValueError:
  151. print("输入的日期错误")
  152. print("继续,或输入q推出")
  153. answer()
  154.  
  155. answer()

  

python学习——练习题(4)的更多相关文章

  1. python学习——练习题(10)

    """ 题目:暂停一秒输出,并格式化当前时间. """ import sys import time def answer1(): &quo ...

  2. python学习——练习题(9)

    """ 题目:暂停一秒输出. 程序分析:使用 time 模块的 sleep() 函数. http://www.runoob.com/python/python-date- ...

  3. python学习——练习题(6)

    """ 题目:斐波那契数列. 程序分析:斐波那契数列(Fibonacci sequence),又称黄金分割数列,指的是这样一个数列:0.1.1.2.3.5.8.13.21 ...

  4. python学习——练习题(1)

    """ 题目:有四个数字:1.2.3.4,能组成多少个互不相同且无重复数字的三位数?各是多少? """ import itertools d ...

  5. python学习——练习题(13)

    """ 题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身.例如:153是一个" ...

  6. python学习——练习题(12)

    """ 题目:判断101-200之间有多少个素数,并输出所有素数. 质数(prime number)又称素数,有无限个. 质数定义为在大于1的自然数中,除了1和它本身以外 ...

  7. python学习——练习题(11)

    """ 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 1 1 2 ...

  8. python学习——练习题(8)

    """ 题目:输出 9*9 乘法口诀表. """ def answer1(): """ 自己用最普通的双重循环 ...

  9. python学习——练习题(7)

    """ 题目:将一个列表的数据复制到另一个列表中. """ import copy def validate(a, b): "&q ...

随机推荐

  1. iOS自动化探索(五)自动化测试框架pytest - Assert断言的使用

    使用assert语句进行断言 pytest允许使用标准的python assert语法,用来校验expectation and value是否一致 代码演示: def func(): def test ...

  2. monkey 原理,环境搭建、命令详解

    一.monkey测试的相关的原理 monkey测试的原理就是利用socket通讯的方式来模拟用户的按键输入,触摸屏输入,手势输入等,看设备多长时间会出异常.当Monkey程序在模拟器或设备运行的时候, ...

  3. 【CSAPP】一、计算机系统漫游

    一.位+上下文 文本文件 / 二进制文件: 文本文件是只由ASCII码构成的文件 二.从源代码到可执行文件的顺序 源代码 ——> 可执行文件(机器代码)共有四步: 全过程代码 gcc hello ...

  4. 解决webpack vue 项目打包生成的文件,资源文件均404问题

    最近在使用webpack + vue做个人娱乐项目时,发现npm run build后,css js img静态资源文件均找不到路径,报404错误...网上查找了一堆解决办法,总结如下 一.首先修改c ...

  5. AngularJs 中的transclude的理解

    Transclude是一个配置, 为了告诉AngularJs去获取当前指令模版内部的所有内容(实际使用ng-transclude), 更多关于怎么创建一个包含其他元素的指令: documentatio ...

  6. Spring-Kafka 2.0.0发送API翻译

    Kafka Template–2.2.0 api KafkaTemplate KafkaTemplate这个类包装了个生产者,来提供方便的发送数据到kafka的topic里面. 同步和异步的方法都有, ...

  7. centos7怎么把语言切换成英语

    一.简介 在Linux的系统中经常碰到字符集导致的错误,本文总结了设置修改系统语言环境的方法步骤. 二.操作步骤 执行如下指令,查看当前使用的系统语言 echo $LANG 执行如下指令,查看系统安装 ...

  8. leetcode_sql_4,196

    196. Delete Duplicate Emails Write a SQL query to delete all duplicate email entries in a table name ...

  9. 剑指offer-第四章解决面试题的思路(包含min函数的栈)

    题目:定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的min函数,在该栈中,调用min,push及pop的时间复杂度都是O(1) 思路:定义两个栈分别为dataStack和minStack ...

  10. 一分钟理解js闭包

    什么是闭包?先看一段代码: ? 1 2 3 4 5 6 7 8 9 10 function a(){   var n = 0;   function inc() {     n++;     cons ...