month = [[0,1,0,1,0,1,1], [1,1,1,1,1,1,1], [0,0,0,0,0,0,1], [1,0,0,0,0,0,1]]
each nested list corresponds to a week and
each 1
corresponds to an "event" and each event has a random length between 2-14
Example:
I want to enter the event at month[0][5]
with a length of 6 days
how do I make it so that the next 6 days all the "events" (including the events that cross the current week) turn 0?
Expected Output:
month = [[0,1,0,1,0,1,0], [0,0,0,0,0,1,1], [0,0,0,0,0,0,1], [1,0,0,0,0,0,1]]
CodePudding user response:
here is one way :
week, day = 0, 5 #starting point eg: month[0][5]
event_days = 6
for _ in range(event_days):
week, day = (week 1, 0) if day == len(month[week])-1 else (week, day 1)
month[week][day] = 0
output:
>>
[
[0, 1, 0, 1, 0, 1, 0]
, [0, 0, 0, 0, 0, 1, 1]
, [0, 0, 0, 0, 0, 0, 1]
, [1, 0, 0, 0, 0, 0, 1]
]
CodePudding user response:
You can run a nested for loop starting from i = 0 and j =5 and let it run until j = 7 at that point increment i by 1. and loop until you complete iterations equal to the length of the event while replacing the values with 0.
month[0][5] = 6
for i in range(0,len(month)):
for j in range(6,8):
month[i][j] = 0
CodePudding user response:
instead of representing a month as a nested list you could also flatten your month into days. With this list most operations should be much more simple. Whenever needed you can simple select a week by days[7*week:7*(week 1)]
days = [0,1,0,1,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,1,0,0,0,0,0,1]
# set an event
event_week = 0
event_day_of_week = 5
event_days = 6
event_begin = 7 * event_week event_day_of_week 1
event_end = event_begin event_days
days[event_begin:event_end] = [0] * event_days
# get a week
week_begin = 1*7
week_end = 2*7
second_week = days[week_begin:week_end]
print(second_week)
>>>[0, 0, 0, 0, 0, 1, 1]