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/
题目大义: 数一段日期间内的周六周日数
from datetime import date def checkio(from_date, to_date):
"""
Count the days of rest
"""
rel = 0
while from_date <= to_date:
if from_date.weekday() == 5 or from_date.weekday() == 6:
rel += 1
from_date += date.resolution return rel
date.resolution(day + 1)是时间步进1, 日+1
观摩veky的代码
def checkio(d1, d2):
w1, w2 = d1.weekday(), d2.weekday()
count = (d2 - d1).days // 7 * 2
while True:
count += w2 > 4
if w1 == w2: return count
w2 = (w2 - 1) % 7
思路不错, 首先计算两个日期间的天数, 地板除7, 再乘2; 再处理其他的天数, 第五行, 或许加个括号更好理解count += (w2 > 4)
Weekend counter的更多相关文章
- UNITY自带的PACKAGE的UTILITY 里面有一个自带的FPS COUNTER
UNITY自带的PACKAGE的UTILITY 里面有一个自带的FPS COUNTER 可用,但是脚本是保密的?
- [LeetCode] Design Hit Counter 设计点击计数器
Design a hit counter which counts the number of hits received in the past 5 minutes. Each function a ...
- collections 模块(namedtuple, deque, Counter )
基本介绍 我们都知道,Python拥有一些内置的数据类型,比如str, int, list, tuple, dict等, collections模块在这些内置数据类型的基础上,提供了几个额外的数据类型 ...
- Performance Monitor2:Peformance Counter
Performance Counter 是量化系统状态或活动的一个数值,Windows Performance Monitor在一定时间间隔内(默认的取样间隔是15s)获取Performance Co ...
- Python_Day_05 计数器(counter),有序字典(OrderDict),默认字典(defaultdict),可命名元祖(namedtuple),双向队列(deque),单项队列(deuqe.Queue)
Counter(计数器) 是一个字典的子类,存储形式同样为字典,其中存储的键为字典的元素,值为元素出现的次数,在使用之前我们需要先导入文件 import collections 初始化一个计数器 im ...
- 利用CSS计数函数counter()实现计数
要实现li列表计数比较简单,直接设置list-style-type即可,但是要实现非li列表计数该怎么办呢,counter()可以轻松实现 body{counter-reset:section 0 s ...
- 计数器(counter),有序字典(OrderDict),默认字典(defaultdict),可命名元祖(namedtuple),双向队列(deque),单项队列(deuqe.Queue)
Python_Day_05 计数器(counter),有序字典(OrderDict),默认字典(defaultdict),可命名元祖(namedtuple),双向队列(deque),单项队列(deuq ...
- LeetCode Design Hit Counter
原题链接在这里:https://leetcode.com/problems/design-hit-counter/. 题目: Design a hit counter which counts the ...
- CacheManager COUNTER
CacheClient.AddOrUpdate("COUNTER", 0, v => Convert.ToInt32(v) + 1);
- codeforces 495A. Digital Counter 解题报告
题目链接:http://codeforces.com/problemset/problem/495/A 这个题目意思好绕好绕~~好绕~~~~~,昨天早上做得 virtual 看不懂,晚上继续看还是,差 ...
随机推荐
- Loading Cargo
Loading Cargo "Look Stephen, here's a list of the items that need to be loaded onto the ship. W ...
- Oracle优化笔记
2016-11-22 子查询:标量子查询 内联视图(in-line view) 半连接/反连接 标量子查询 select 后跟子查询 类似自定义函数 可用开窗函数之类的改写 内联视图(in ...
- HDU 3586 : Information Disturbing
Problem Description In the battlefield , an effective way to defeat enemies is to break their commun ...
- Spring中给Bean注入集合
Spring中如果一个Bean里含有集合元素,需要给Bean里的集合元素注入元素时,可以采用如下方法,一个是构造器注入,一个是setter注入 JavaBean源代码: import java.uti ...
- 有趣的JavaScript
让你的js代码从一行开始,另一行结束: var s /*:String*/ = "Test \ multi \ line."; 看到没,只需要在你换行的字符串后面加上反斜线(bac ...
- shell脚本一条命令直接发送http请求(xjl456852原创)
我们知道nc命令是一个网络工具.可以连接tcp/udp.也能模拟发送http请求. 现在介绍通过shell脚本,一条命令直接发送http请求. 命令如下,可以对下面的地址等信息自行修改: #!/bin ...
- tomcat动态映射路径
写了一个工具类,将上传文件功能保存文件的目录移到webapps目录外面,通过动态生成xml映射文件到tomcat\conf\Catalina\localhost目录下从而实现目录映射.可以被http直 ...
- Hive2.0函数大全(中文版)
摘要 Hive内部提供了很多函数给开发者使用,包括数学函数,类型转换函数,条件函数,字符函数,聚合函数,表生成函数等等,这些函数都统称为内置函数. 目录 数学函数 集合函数 类型转换函数 日期函数 条 ...
- Ajax属性和函数以及 返回值之XML格式和文本格式(二)
(一) client请求文本之json格式:接收到json格式,再有js解析(详细先eval成对象,然后.就可以) var text = this.responseText; var book = e ...
- Android项目中gen文件下R文件无法生成的解决的方法
帮一个网友解决R文件无法生成的问题,搜集了些材料特整理例如以下,刚開始学习的人參考他人代码时极易出现此种问题,一般都是xml文件出错,无法被正确解析. gen文件夹无法更新,或者gen文件夹下的R.J ...