Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # author: Bartlomiej "furas" Burek (https://blog.furas.pl)
- # date: 2020.04.25
- # https://stackoverflow.com/questions/61361457/repeating-the-pattern-of-numbers-thrice-in-a-month/
- import datetime
- import numpy as np
- from datetime import timedelta
- Holiday_List = [
- '2020-01-01', # remove date to test 3 'NW'
- '2020-01-05', # remove date to test 3 'NW'
- '2020-01-12',
- '2020-01-19',
- '2020-01-26',
- #'2020-01-13', # add extra date to test `gap >= 9`
- #'2020-01-14', # add extra date to test `gap >= 9`
- #'2020-01-15', # add extra date to test `gap >= 9`
- ]
- Start_date = datetime.datetime(year=2020, month=1, day=1)
- end_date = datetime.datetime(year=2020, month=1, day=28)
- delta = end_date - Start_date
- print(delta)
- hDay = "Holiday"
- dummy = "NW"
- # --- numpy array ---
- arr = np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18], dtype=object) #Assumed that the array length of is divisible by 3 every time
- arr = np.split(arr, 3) #spilts the array to three equal parts
- for i in range(len(arr[0]), 9): # CHANGED: add 3 'NW' instead of 2 'NW'
- arr = np.insert(arr, i, dummy, axis=1) # fill remaining slots with dummy value(NW)
- print("{}\t{}\t{}".format("Date", "Holiday", "Values"))
- # ---
- i = 0
- for numbers in arr:
- gap = 0
- numbers_index = 0
- numbers_count = len(numbers) - 3 # count numbers without 3 `NW`
- while i < delta.days + 1:
- day = Start_date + timedelta(days=i)
- i += 1
- if day.strftime("%Y-%m-%d") in Holiday_List:
- print("{}\t{}\t{}".format(day.strftime("%d-%m-%Y"), 1, hDay))
- if numbers_index > 0: # don't count Holiday before displaying first number from list `data` (ie. '2020-01-01')
- gap += 1
- else:
- value = numbers[numbers_index]
- # always put number (!='NW') or put 'NW' when gap is too small (<9)
- if value != 'NW' or gap < 9:
- print("{}\t{}\t{}".format(day.strftime("%d-%m-%Y"), 0, value))
- numbers_index += 1
- gap += 1
- # IDEA: maybe it could use `else:` to put `NW` without adding `NW` to `arr`
- # exit loop if all numbers are displayed and gap is big enough
- if numbers_index >= numbers_count and gap >= 9:
- break
Add Comment
Please, Sign In to add comment