views:

315

answers:

7

I need all the weekdays between two days.

Example:

Wednesday - Friday  = Wednesday, Thursday, Friday  
        3 - 5       = 3, 4, 5

 Saturday - Tuesday = Saturday, Sunday, Monday, Tuesday
        6 - 2       = 6, 7, 1, 2

I'm pretty sure there is a clever algorithm out there to solve this. The only algorithms I can think of use either a loop or an if statement.

There has to be an elegant way to solve this. I use the numbers 1-7 for the weekdays, but 0-6 is fine too.

The best I could come up with:

def between(d1, d2):
     alldays = [0,1,2,3,4,5,6,0,1,2,3,4,5,6]    # or range(7) * 2
     offset = 8 if d1 > d2 else 1
     return alldays[d1:d2 + offset]

between(0, 4)
# [0,1,2,3,4]

between(5,2)
# [5,6,0,1,2]
+9  A: 

Hey,

How about (in pseudo code):

weekday[] = {"Mon" .. "Sun"}
for(i = wkday_start; (i % 7) != wkday_end; i = (i+1) % 7)
    printf("%s ", weekday[i]);

It works like a circular buffer, wkday_start being the index to start at (0-based), wkday_end being the end index.

Hope this helps

George.

George Shore
A: 

You asked for an algorithm, and I understand that should be language independent; however, following code works using C# and LINQ expressions:

DayOfWeek start = DayOfWeek.Wednesday;
DayOfWeek end = DayOfWeek.Friday;

IEnumerable<DayOfWeek> interval = 
    Enum.GetValues(typeof(DayOfWeek)).OfType<DayOfWeek>()
        .Where(d => d >= start && d <= end);

Console.WriteLine(
    String.Join(", ", 
        interval.Select(d => d.ToString()).ToArray()));

Probably, using any language, your should attribute values to each day (Sunday=0 and so on) and look for all values which matches your desired interval.

Rubens Farias
+8  A: 
>>> def weekdays_between(s, e):
...     return [n % 7 for n in range(s, e + (1 if e > s else 8))]
... 
>>> weekdays_between(2, 4)
[2, 3, 4]
>>> weekdays_between(5, 1)
[5, 6, 0, 1]

It's a bit more complex if you have to convert from/to actual days.

>>> days = 'Mon Tue Wed Thu Fri Sat Sun'.split()
>>> days_1 = {d: n for n, d in enumerate(days)}
>>> def weekdays_between(s, e): 
...     s, e = days_1[s], days_1[e]
...     return [days[n % 7] for n in range(s, e + (1 if e > s else 8))]
... 
>>> weekdays_between('Wed', 'Fri')
['Wed', 'Thu', 'Fri']
>>> weekdays_between('Sat', 'Tue')
['Sat', 'Sun', 'Mon', 'Tue']
Stephan202
I think your solution is the best of all answers, but I think I'll stick with the one I already have because it's a bit more legible.
Georg
A: 

The following code returns 1 for Monday - Monday.

bool isWeekday(int d) {
    return d >= 1 && d <= 5;
}

int f(int d1, int d2) {
    int res = isWeekday(d1) ? 1 : 0;
    return d1 == d2 ?
           res :
           res + f(d1 % 7 + 1, d2);
}
Olexiy
+1  A: 

The solutions provided already answer the question, but I want to suggest something extra. I don't know what you're doing, but maybe you want the actual dates instead?

>>> from datetime import timedelta, date
>>> from dateutil.rrule import rrule, DAILY
>>> today = date(2009, 10, 13) # A tuesday
>>> week = today - timedelta(days=6)
>>> list(rrule(DAILY, byweekday=xrange(5), dtstart=week, until=today))
[datetime.datetime(2009, 10, 7, 0, 0),
 datetime.datetime(2009, 10, 8, 0, 0),
 datetime.datetime(2009, 10, 9, 0, 0),
 datetime.datetime(2009, 10, 12, 0, 0),
 datetime.datetime(2009, 10, 13, 0, 0)]

That uses the excellent python-dateutil module.

nosklo
+1  A: 

Use the calendar module for your list of day names:

import calendar

def intervening_days(day1, day2):
    weektest = list(calendar.day_name)*2
    d1 = weektest.index(day1)
    d2 = weektest.index(day2,d1+1)
    return weektest[d1:d2+1]

print intervening_days("Monday","Sunday")
print intervening_days("Monday","Tuesday")
print intervening_days("Thursday","Tuesday")
print intervening_days("Monday","Monday")

Prints:

['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
['Monday', 'Tuesday']
['Thursday', 'Friday', 'Saturday', 'Sunday', 'Monday', 'Tuesday']
['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'Monday']

If you don't want Monday-to-Monday to return a full week of days, change the determination of d2 to d2 = weektest.index(day2,d1).

Paul McGuire
+2  A: 

Building on the excellent answer from Stephan202, you can generalize the concept of a circular slice.

>>> def circular_slice(r, s, e):
... return [r[n % len(r)] for n in range(s, e + (1 if e>s else len(r)+1))]
...
>>> circular_slice(range(0,7), 2, 4)
[2, 3, 4]
>>> circular_slice(range(0,7), 5, 1)
[5, 6, 0, 1]
>>> circular_slice('Mon Tue Wed Thu Fri Sat Sun'.split(), 5, 1)
['Sat', 'Sun', 'Mon', 'Tue']
Mark Ransom