 # example for stackoverflow

Apr 25th, 2020 (edited)
539
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. # author: Bartlomiej "furas" Burek (https://blog.furas.pl)
2. # date: 2020.04.25
3. # https://stackoverflow.com/questions/61361457/repeating-the-pattern-of-numbers-thrice-in-a-month/
4.
5. import datetime
6. import numpy as np
7. from datetime import timedelta
8.
9. Holiday_List = [
10.     '2020-01-01',  # remove date to test 3 'NW'
11.     '2020-01-05',  # remove date to test 3 'NW'
12.     '2020-01-12',
13.     '2020-01-19',
14.     '2020-01-26',
15.     #'2020-01-13',  # add extra date to test `gap >= 9`
16.     #'2020-01-14',  # add extra date to test `gap >= 9`
17.     #'2020-01-15',  # add extra date to test `gap >= 9`
18. ]
19.
20. Start_date = datetime.datetime(year=2020, month=1, day=1)
21. end_date = datetime.datetime(year=2020, month=1, day=28)
22.
23. delta = end_date - Start_date
24. print(delta)
25.
26. hDay = "Holiday"
27. dummy = "NW"
28.
29. # --- numpy array ---
30.
31. 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
32.
33. arr = np.split(arr, 3)   #spilts the array to three equal parts
34.
35. for i in range(len(arr), 9):  # CHANGED: add 3 'NW' instead of 2 'NW'
36.     arr = np.insert(arr, i, dummy, axis=1)  # fill remaining slots with dummy value(NW)
37.
38. print("{}\t{}\t{}".format("Date", "Holiday", "Values"))
39.
40. # ---
41.
42. i = 0
43.
44. for numbers in arr:
45.
46.     gap = 0
47.     numbers_index = 0
48.     numbers_count = len(numbers) - 3 # count numbers without 3 `NW`
49.
50.     while i < delta.days + 1:
51.         day = Start_date + timedelta(days=i)
52.         i += 1
53.
54.         if day.strftime("%Y-%m-%d") in Holiday_List:
55.             print("{}\t{}\t{}".format(day.strftime("%d-%m-%Y"), 1, hDay))
56.             if numbers_index > 0: # don't count Holiday before displaying first number from list `data` (ie. '2020-01-01')
57.                 gap += 1
58.         else:
59.             value = numbers[numbers_index]
60.             # always put number (!='NW') or put 'NW' when gap is too small (<9)
61.             if value != 'NW' or gap < 9:
62.                 print("{}\t{}\t{}".format(day.strftime("%d-%m-%Y"), 0, value))
63.                 numbers_index += 1
64.                 gap += 1
65.             # IDEA: maybe it could use `else:` to put `NW` without adding `NW` to `arr`
66.
67.         # exit loop if all numbers are displayed and gap is big enough
68.         if numbers_index >= numbers_count and gap >= 9:
69.             break