Created
December 17, 2019 21:51
-
-
Save mike-pete/2f680a3d1ca61948636daba0c6556c22 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# These functions are used to calculate how many work days have been in the previous N number of days | |
days = ['mon','tue','wed','thu','fri','sat','sun'] | |
# this function counts the workdays within the last n days | |
def countWorkDays(daysAgo, dayOfWeek): | |
# print(daysAgo, days[dayOfWeek]) | |
workDay = 1 if dayOfWeek < 5 else 0 | |
dayOfWeek = dayOfWeek - 1 if dayOfWeek > 0 else 6 | |
# recursion | |
if daysAgo == 1: | |
return workDay | |
else: | |
return workDay + countWorkDays(daysAgo-1, dayOfWeek) | |
# this function calculates the workdays within the last n days | |
def calculateWorkDays(daysAgo, dayOfWeek): | |
rDays = daysAgo%7 | |
weeks = (daysAgo-rDays)/7 | |
# this +1 is an offset to count current day | |
x = dayOfWeek+1 - rDays | |
if x < -1: rDays -= 2 | |
elif x < 0: rDays -= 1 | |
return int(weeks*5 + rDays) | |
#today = days.index('mon') | |
daysAgo = 14 | |
dayOfWeek = 0 | |
a = countWorkDays(daysAgo, dayOfWeek) | |
b = calculateWorkDays(daysAgo, dayOfWeek) | |
print(a,b) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment