furas

example for stackoverflow

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