首页 > 其他 > 详细

Weekend counter

时间:2014-08-08 17:36:46      阅读:361      评论:0      收藏:0      [点我收藏+]

Weekend counter

Sofia has given you a schedule and two dates and told you she needs help planning her weekends. She has asked you to count each day of rest (Saturday and Sunday) starting from the initial date to final date. You should count the initial and final dates if they fall on a Saturday or Sunday.

The dates are given as datetime.date (Read about this module here). The result is an integer.

Input: Start and end date as datetime.date.

Output: The quantity of the rest days as an integer.

原题链接: http://www.checkio.org/mission/weekend-counter/

题目大义: 数一段日期间内的周六周日数

 1 from datetime import date
 2 
 3 def checkio(from_date, to_date):
 4     """
 5         Count the days of rest
 6     """
 7     rel = 0
 8     while from_date <= to_date:
 9         if from_date.weekday() == 5 or from_date.weekday() == 6:
10             rel += 1
11         from_date += date.resolution
12 
13     return rel

date.resolution(day + 1)是时间步进1, 日+1

观摩veky的代码

1 def checkio(d1, d2):
2     w1, w2 = d1.weekday(), d2.weekday()
3     count = (d2 - d1).days // 7 * 2
4     while True:
5         count += w2 > 4
6         if w1 == w2: return count
7         w2 = (w2 - 1) % 7

思路不错, 首先计算两个日期间的天数, 地板除7, 再乘2; 再处理其他的天数, 第五行, 或许加个括号更好理解count += (w2 > 4)

 

Weekend counter,布布扣,bubuko.com

Weekend counter

原文:http://www.cnblogs.com/hzhesi/p/3899592.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!