python学习——练习题(4)
"""
题目:输入某年某月某日,判断这一天是这一年的第几天?
"""
import datetime
import time
from functools import reduce def calculate1(t):
"""
直接利用python的datetime模块计算
:param t:
:return:
"""
print("计算一", end=":")
print(t.strftime("%j")) def calculate2(t):
"""
自己手动计算一下
:param t:
:return:
"""
print("计算二", end=":")
days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
daysLeap = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
year = t.year
if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):
print(sum(daysLeap[:t.month - 1]) + t.day)
else:
print(sum(days[:t.month - 1]) + t.day) def calculate3(t):
"""
高手简化后的calculate2
:param t:
:return:
"""
print("计算三", end=":")
days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
year = t.year
if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):
days[1] = 29
print(sum(days[0: t.month - 1]) + t.day) def calculate4(t):
"""
利用字典来计算
:param t:
:return:
"""
print("计算四", end=":")
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}
year = t.year
d = dayDict[t.month - 1] + t.day
if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):
d += 1
print(d) def calculate5(t):
"""
利用time模块来计算,注意和datetime模块进行区分
:param t:
:return:
"""
print("计算五", end=":")
t = time.strptime(t.strftime("%Y-%m-%d"), "%Y-%m-%d")
print(t[7]) def calculate6(t):
"""
利用datetime的时间相减来计算
:param t:
:return:
"""
print("计算六", end=":")
t1 = datetime.date(t.year, 1, 1)
t2 = t - t1
print(t2.days + 1) def calculate7(t):
"""
利用time的时间相减来计算,注意与datetime进行区分,它不能直接减,需要转成时间戳才能减
以为时间戳是以1970年为基点计算的,所以该方法只能计算1970以后(不包括1970)的时间
:param t:
:return:
"""
print("计算七", end=":")
t1 = time.strptime(t.strftime("%Y-01-01"), "%Y-%m-%d")
t1 = time.mktime(t1)
t = time.strptime(t.strftime("%Y-%m-%d"), "%Y-%m-%d")
t = time.mktime(t)
t2 = t - t1
t2 = t2 // (3600 * 24)
print(int(t2) + 1) def calculate8(t):
"""
利用reduce函数来计算,中间有用到三元运算符
在Python 3里,reduce()函数已经被从全局名字空间里移除了,它现在被放置在fucntools模块里
用的话要 先引入 from functools import reduce
:param t:
:return:
"""
print("计算八", end=":")
year = t.year
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,
30, 31]
print(reduce(lambda a, b: a + b, days[0: t.month]) + t.day) def calculate9(t):
"""
利用位运算来计算闰年:
分析 year&3 等价于 year%4:因为二进制转十进制是:2**0+2**1+2**2+。。。,可见2**2之后的都可以被4整除
同理 year&15 等价 year%16
根据闰年计算规则我们可以知道:不能被4整除的年份肯定不是闰年,而能被4整除又能被25整数但不能再被16整数的也不是闰年,其余全是闰年
可得 year%4 or year%16 and !year%25 这些都不是闰年,反之!(year%4 or year%16 and !year%25)为闰年
转为位运算!(year&3 or year&15 and !(year%25))
:param t:
:return:
"""
print("计算九", end=":")
days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
year = t.year
if not(year & 3 or year & 15 and not(year % 25)):
days[1] = 29
print(sum(days[0: t.month - 1]) + t.day) def answer():
"""
通过try来判断输入的日期是否正确
:return:
""" year = input("输入年:")
if year == "q":
return
month = input("输入月:")
day = input("输入日:")
try:
t = datetime.date(int(year), int(month), int(day))
calculate1(t)
calculate2(t)
calculate3(t)
calculate4(t)
calculate5(t)
calculate6(t)
calculate7(t)
calculate8(t)
calculate9(t)
except ValueError:
print("输入的日期错误")
print("继续,或输入q推出")
answer() answer()
python学习——练习题(4)的更多相关文章
- python学习——练习题(10)
""" 题目:暂停一秒输出,并格式化当前时间. """ import sys import time def answer1(): &quo ...
- python学习——练习题(9)
""" 题目:暂停一秒输出. 程序分析:使用 time 模块的 sleep() 函数. http://www.runoob.com/python/python-date- ...
- python学习——练习题(6)
""" 题目:斐波那契数列. 程序分析:斐波那契数列(Fibonacci sequence),又称黄金分割数列,指的是这样一个数列:0.1.1.2.3.5.8.13.21 ...
- python学习——练习题(1)
""" 题目:有四个数字:1.2.3.4,能组成多少个互不相同且无重复数字的三位数?各是多少? """ import itertools d ...
- python学习——练习题(13)
""" 题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身.例如:153是一个" ...
- python学习——练习题(12)
""" 题目:判断101-200之间有多少个素数,并输出所有素数. 质数(prime number)又称素数,有无限个. 质数定义为在大于1的自然数中,除了1和它本身以外 ...
- python学习——练习题(11)
""" 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 1 1 2 ...
- python学习——练习题(8)
""" 题目:输出 9*9 乘法口诀表. """ def answer1(): """ 自己用最普通的双重循环 ...
- python学习——练习题(7)
""" 题目:将一个列表的数据复制到另一个列表中. """ import copy def validate(a, b): "&q ...
随机推荐
- 【scala】构造器
和Java或C++一样,Scala可以有任意多的构造器. 不过Scala类有一个构造器比其他所有构造器都更为重要,它就是主构造器. 除了主构造器之外,类还可以有任意多的辅助构造器. 主构造器 在Sca ...
- ROS新版本Lunar Loggerhead
参考链接: 1 http://wiki.ros.org/lunar 2 http://wiki.ros.org/lunar/Installation 3 http://docs.ros.org/ 4 ...
- Hive——巧用transform处理复杂的字符串问题
相比于Map-Reduce,Hive对数据的处理相对简单,但是Hive本身提供的函数,对于处理复杂的字符串问题,就显得不是很方便,此时,可以借助transform,引入外界的Python程序对字符串进 ...
- grep 常用正则匹配
1.或操作 grep -E '123|abc' filename // 找出文件(filename)中包含123或者包含abc的行 egrep '123|abc' filename // 用egrep ...
- 在Wifi网络中嗅探明文密码(HTTP POST请求、POP等)
全世界,现在大约50%的网站没有使用SSL加密,天朝尤其多. 我们都知道通过HTTP发送的数据都是明文,没有使用任何加密,即使是在数据敏感的登录页面. 本文的目的是:如果你在不熟悉的网络环境中,要注意 ...
- 使用iptables nat进行端口转发
1.将发向HostA:PortA的请求转发到HostB:PortB iptables -t nat -A PREROUTING -p tcp -i eth0 -d HostA --dport Port ...
- PS常用美化处理方法大全
学习PS的同学都知道,我们日常生活中使用PS就是进行一些简单的图像美白,图像颜色的优化,其他的基本不用,在长时间的PS使用过程中本人总结了一些处理皮肤的方法,都是一些非常简单的方法,希望能够帮助那些刚 ...
- bzoj 4806 炮
Written with StackEdit. Description 众所周知,双炮叠叠将是中国象棋中很厉害的一招必杀技.炮吃子时必须隔一个棋子跳吃,即俗称"炮打隔子". 炮跟炮 ...
- iOS设置translucent 引发的坐标问题
iOS NavigationBar + 导航栏 tablevie时候的布局情况,之前迷惑了我很久,怎么也没法理解透明度会影响布局. 接下来看一下以下三种情况的运行结果 1.全部系统默认情况下利用m ...
- LG5055 【模板】可持久化文艺平衡树
题意 您需要写一种数据结构,来维护一个序列,其中需要提供以下操作(对于各个以往的历史版本): 在第 pp 个数后插入数 xx . 删除第 pp 个数. 翻转区间 [l,r][l,r],例如原序列是 { ...