给定日期范围如何计算部分或全部在该范围内的周末数量? [英] Given a date range how to calculate the number of weekends partially or wholly within that range?

查看:234
本文介绍了给定日期范围如何计算部分或全部在该范围内的周末数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定日期范围如何计算部分或全部在该范围内的周末数量?

Given a date range how to calculate the number of weekends partially or wholly within that range?

(根据请求提供一些定义:
取周末 '意味着星期六和星期日
日期范围是包括性的,即结束日期是范围
的全部或部分的一部分意味着周末的任何部分落在日期范围内意味着整个周末被计算。)

(A few definitions as requested: take 'weekend' to mean Saturday and Sunday. The date range is inclusive i.e. the end date is part of the range 'wholly or partially' means that any part of the weekend falling within the date range means the whole weekend is counted.)

为了简化我想象你只需要知道持续时间,第一天的星期几是...

To simplify I imagine you only actually need to know the duration and what day of the week the initial day is...

我很好,现在它将涉及整数除以7和一些逻辑添加1,取决于剩余,但我不能完全解决什么...

I darn well now it's going to involve doing integer division by 7 and some logic to add 1 depending on the remainder but I can't quite work out what...

在Python中的答案的额外分数;-)

extra points for answers in Python ;-)

修改

这是我的最终代码。

周末是星期五和星期六(因为我们正在计算住夜晚),而从星期一开始,天数是0索引。我使用onebyone的算法和Tom的代码布局。感谢很多人。

Weekends are Friday and Saturday (as we are counting nights stayed) and days are 0-indexed starting from Monday. I used onebyone's algorithm and Tom's code layout. Thanks a lot folks.

def calc_weekends(start_day, duration):
    days_until_weekend = [5, 4, 3, 2, 1, 1, 6]
    adjusted_duration = duration - days_until_weekend[start_day]
    if adjusted_duration < 0:
        weekends = 0
    else:
        weekends = (adjusted_duration/7)+1
    if start_day == 5 and duration % 7 == 0: #Saturday to Saturday is an exception
        weekends += 1
    return weekends

if __name__ == "__main__":
    days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
    for start_day in range(0,7):
        for duration in range(1,16):
            print "%s to %s (%s days): %s weekends" % (days[start_day], days[(start_day+duration) % 7], duration, calc_weekends(start_day, duration))
        print


推荐答案

这种事情的一般方法:

对于本周的每一天,找出在当天开始的一段时间之前需要多少天包含周末。例如,如果包含周末是指包含星期六和星期日,那么我们有以下表格:

For each day of the week, figure out how many days are required before a period starting on that day "contains a weekend". For instance, if "contains a weekend" means "contains both the Saturday and the Sunday", then we have the following table:

星期日:8
星期一:7
星期二:6
星期三:5
星期四:4
星期五:3
星期六:2

Sunday: 8 Monday: 7 Tuesday: 6 Wednesday: 5 Thursday: 4 Friday: 3 Saturday: 2

对于部分或全部,我们有:

For "partially or wholly", we have:

星期日:1
星期一:6
星期二:5
星期三: 4
星期四:3
星期五:2
星期六:1

Sunday: 1 Monday: 6 Tuesday: 5 Wednesday: 4 Thursday: 3 Friday: 2 Saturday: 1

显然,这不一定要作为表格编码,现在,这显然是什么样的。

Obviously this doesn't have to be coded as a table, now that it's obvious what it looks like.

然后,给定你的期间开始的星期几,从长度上减去[*]魔术值的期间(可能是开始+ 1,包括两个fenceposts)。如果结果小于0,则包含0个周末。如果它等于或大于0,那么它至少包含1个周末。

Then, given the day-of-week of the start of your period, subtract[*] the magic value from the length of the period in days (probably start-end+1, to include both fenceposts). If the result is less than 0, it contains 0 weekends. If it is equal to or greater than 0, then it contains (at least) 1 weekend.

然后你必须处理剩下的日子。在第一种情况下,这很容易,每整整7天多一个周末。除了星期日以外的每个开始日,第二种情况也是如此,只需要另外6天的时间才能包括另外一个周末。所以在第二种情况下,从星期天开始的时间段,你可以在期间开始计算1个周末,然后从长度中减去1个周末。从

Then you have to deal with the remaining days. In the first case this is easy, one extra weekend per full 7 days. This is also true in the second case for every starting day except Sunday, which only requires 6 more days to include another weekend. So in the second case for periods starting on Sunday you could count 1 weekend at the start of the period, then subtract 1 from the length and recalculate from Monday.

更一般地那么这个全部或部分周末的情况是,我们正在检查我们是否通过有趣的一点(周末)中途开始。如果是这样,我们可以:

More generally, what's happening here for "whole or part" weekends is that we're checking to see whether we start midway through the interesting bit (the "weekend"). If so, we can either:


  • 1)计数一,将开始日期移动到有趣位的结尾,并重新计算。 / li>
  • 2)将开始日期移回到有趣位的开头,然后重新计算。

在周末的情况下,中途只有一个特殊情况,所以(1)看起来不错。但是,如果您将日期作为日期+秒(而不是日期),或者如果您对5天工作周而不是2天的周末感兴趣,那么(2)可能更容易理解。

In the case of weekends, there's only one special case which starts midway, so (1) looks good. But if you were getting the date as a date+time in seconds rather than day, or if you were interested in 5-day working weeks rather than 2-day weekends, then (2) might be simpler to understand.

[*]当然,除非你使用无符号类型。

[*] Unless you're using unsigned types, of course.

这篇关于给定日期范围如何计算部分或全部在该范围内的周末数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆