Reputation: 4135
I have a list representing the year which is filled with a sub-list for each day of the year.
year = []
for i in range(0,52*7):
day = [i,0] #[day number, 0 = empty, 1 = something is planned]
year.append(day)
I also have a variable list of activities created by a class.
class Activities:
def __init__(self,name,weeks,weekends):
self.name = name
self.weeks = weeks
self.weekends = weekends
def __repr__(self):
return repr((self.name,self.weeks,self.weekends))
def activityMaker(activityList):
a= []
for i in range(0, len(activityList)):
a.append(Activities(activityList[i][0], activityList[i][1], activityList[i][2]))
a = sorted(a, key=lambda Activities: Activities.weeks)
activityList = a
return activityList
As an example;
>>> activityList = [['Tennis', 3, 0], ['Baseball', 4, 0], ['Swimming', 2, 0]]
>>>activities= activityMaker(activityList)
Which returns 'activities', sorted on Activities.weeks:
>>>activities[0].name
activities[0].week
activities[0].weekend
>>> 'Swimming' # activity name
2 #"i want to do this activity once every x weeks
0 # 0 = no preferance, 1 = not in weekends
Now here my dilemma. I wish to create an algorithm to fill year with the activities with as much rhythm as possible.
My current approach is not working properly. What I'm doing now is as follows.
for y in range(0,len(year), int(7*activities[0].weeks)):
year[y][1] = activities[i].name
Now the first activity is planned for each y. If I had two activities, which I each want planned once a week, I could plan the first on 0th, 7th, 14th etc, and the second on 3rd, 10th, 17th etcetera.
The problem with this approach is exemplified if activities[0] and activities[1] are respectively 2 and 3. If I apply the previous approach, activities[0] would be planned on the 0th, 14th, 28th etc, which is fine on its own. Between the 0th and 14th, the 2nd activity would be ideally placed on the 7th position, meaning the next time would be the 28th. On the 28 the first activity is already planned, however. This means that's there's nothing planned for two weeks and then suddenly 2 activities in a day. The second activity could be pushed to the 27th or 29th, but that would still mean that now activities are planned on the 0th, 7th, 14th, 28th, 29th. Aka, there's still 14 days between the 14th and the 28th, and then only 1 between the 28th and 29th.
In what way can I make sure that all activities are planned with as much average time in between activities?
Upvotes: 0
Views: 211
Reputation: 1594
Your problem is that unless the number of weeks is the same for all activities (so they all have the same rhythm, there will be some weeks with lots of activities and some weeks with none.
What I would suggest instead is this: As you walk through the weeks of the year, simply choose an activity (or two) at random for each week. That way, every week will have a modest amount of activity planned. Here's some example code:
import random
activities = ["Baseball", "Tennis", "Swimming", ... ]
skip_days = 3
year = {}
for y in range(0, 52*7, skip_days):
year[y] = random.choose(activities)
print year[0]
>>> "Swimming" (perhaps)
print year[15]
>>> "Baseball"
print year[17]
>>> None
If you want more activity, make skip_days smaller. If you want less, make it bigger. If you want a fixed amount of activity in each week, you could do something like
for y in range(0, 52*7, 7):
year[y] = random.choose(activities)
year[y+3] = random.choose(activities)
This would plan two days a week.
Upvotes: 1