首页 > 代码库 > Weekend counter
Weekend counter
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 += 111 from_date += date.resolution12 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 * 24 while True:5 count += w2 > 46 if w1 == w2: return count7 w2 = (w2 - 1) % 7
思路不错, 首先计算两个日期间的天数, 地板除7, 再乘2; 再处理其他的天数, 第五行, 或许加个括号更好理解count += (w2 > 4)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。