29a.ch by Jonas Wagner

Getting the number of workdays in a given timeframe

This is a weird function I wrote once to get the number of workdays (all days except saturday and sunday). Now the objective here was to do it in O(1) and without the use of any flow control. So I came up with this:

def get_work_days(startday, days):
    freq = 7
    startday = startday+1%freq
    sundayPhase = (freq - startday) % freq
    saturdayPhase = (sundayPhase + 6) % freq
    remaining = days % freq
    hasSunday = 0**((sundayPhase+1)/(remaining+1))
    hasSaturday = 0**((saturdayPhase+1)/(remaining+1))
    return (days - remaining) / freq * 5 + remaining - hasSunday - hasSaturday

It's abusing the fact that in most programming languages 0^0 is defined to be 1.