Posts: 6
Threads: 3
Joined: Nov 2019
Dec-10-2019, 11:53 AM
(This post was last modified: Dec-10-2019, 11:58 AM by Seneca260.)
Hi all,
I am learning Python and also teaching a class and I want to build a program that random assigns students a day to do their presentation.
dates = ['Jan 6', 'Jan 8', 'Jan 10']
students = ['Joey,' 'Henry', 'Daniel', 'James', 'Samantha', 'Jose', 'Salvator', 'Paul', 'Steve', 'Mary', 'Kyle', 'Marcus', 'Pat'] I am trying to randomly assign students to one of the days to do a presentation. I would thus want an item from List 1 to match with an item in List 2, but am unsure of how to do this.
Thanks in advance
Posts: 8,169
Threads: 160
Joined: Sep 2016
probably you also want to put some constrains - i.e. max number of presentations per day/in each different day if they vary.You don't want to have all presentations in a single day (extreme case, but possible if no other constrains)
Posts: 6
Threads: 3
Joined: Nov 2019
Thanks buran,
Yes, precisely. If they could be evenly distribute that would be great. I am just wondering how do I actually code this? I have been trying to find this online, but whenever I search Google or Stack Overflow, I can't seem to get it. Any suggestions on how to search for this or what code would be most appropriate?
Posts: 1,950
Threads: 8
Joined: Jun 2018
Dec-11-2019, 12:46 PM
(This post was last modified: Dec-11-2019, 12:46 PM by perfringo.)
If I understand the objective correctly then my approach would be:
- shuffle the list
- slice list to evenly distributed chunks
- assign chunks to dates
Something like that:
>>> import random
>>> students = ['Joey', 'Henry', 'Daniel', 'James', 'Samantha', 'Jose', 'Salvator', 'Paul', 'Steve', 'Mary', 'Kyle', 'Marcus', 'Pat']
>>> dates = ['Jan 6', 'Jan 8', 'Jan 10']
>>> step = len(students) // len(dates)
>>> random.shuffle(students) # shuffles list in place
>>> students
['Paul', 'Salvator', 'Daniel', 'Mary', 'Pat', 'Jose', 'Marcus', 'Steve', 'James', 'Samantha', 'Joey', 'Henry', 'Kyle']
>>> assignments = dict()
>>> for i, date in enumerate(dates):
... assignments[date] = students[i*step:i*step+step]
...
>>> assignments
{'Jan 6': ['Paul', 'Salvator', 'Daniel', 'Mary'], 'Jan 8': ['Pat', 'Jose', 'Marcus', 'Steve'], 'Jan 10': ['James', 'Samantha', 'Joey', 'Henry']} It assumes that step is integer i.e. there are even numbers of students for each day (currently 'Kyle' is left out). Should he be added to first or last or randomly? Are there constraints how many students can present in one day?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 8,169
Threads: 160
Joined: Sep 2016
there are plenty of possibilities, e.g.
from itertools import cycle
from collections import Counter
from random import shuffle
students = ['Joey', 'Henry', 'Daniel', 'James', 'Samantha', 'Jose', 'Salvator', 'Paul', 'Steve', 'Mary', 'Kyle', 'Marcus', 'Pat']
dates = ['Jan 6', 'Jan 8', 'Jan 10']
shuffle(students)
schedule = dict(zip(students, cycle(dates)))
print(schedule)
print(Counter(schedule.values()).most_common()) Output: {'Steve': 'Jan 6', 'Mary': 'Jan 8', 'Kyle': 'Jan 10', 'Jose': 'Jan 6', 'Henry': 'Jan 8', 'Marcus': 'Jan 10', 'Paul': 'Jan 6', 'James': 'Jan 8', 'Salvator': 'Jan 10', 'Samantha': 'Jan 6', 'Joey': 'Jan 8', 'Pat': 'Jan 10', 'Daniel': 'Jan 6'}
[('Jan 6', 5), ('Jan 8', 4), ('Jan 10', 4)]
Posts: 6
Threads: 3
Joined: Nov 2019
Thanks for the replies everyone. These are helpful!
Posts: 1,950
Threads: 8
Joined: Jun 2018
buran code can be extended to get list of students by dates. We have dictionary of students and we just reverse keys and values:
>>> from collections import defaultdict
>>> schedule = {'Steve': 'Jan 6', 'Mary': 'Jan 8', 'Kyle': 'Jan 10', 'Jose': 'Jan 6', 'Henry': 'Jan 8', 'Marcus': 'Jan 10', 'Paul': 'Jan 6', 'James': 'Jan 8', 'Salvator': 'Jan 10', 'Samantha': 'Jan 6', 'Joey': 'Jan 8', 'Pat': 'Jan 10', 'Daniel': 'Jan 6'}
>>> by_dates = defaultdict(list)
>>> for name, date in schedule.items():
... by_dates[date].append(name)
...
>>> by_dates
defaultdict(<class 'list'>, {'Jan 6': ['Steve', 'Jose', 'Paul', 'Samantha', 'Daniel'], 'Jan 8': ['Mary', 'Henry', 'James', 'Joey'], 'Jan 10': ['Kyle', 'Marcus', 'Salvator', 'Pat']})
>>> dict(by_dates)
{'Jan 6': ['Steve', 'Jose', 'Paul', 'Samantha', 'Daniel'], 'Jan 8': ['Mary', 'Henry', 'James', 'Joey'], 'Jan 10': ['Kyle', 'Marcus', 'Salvator', 'Pat']}
>>> by_dates['Jan 8']
['Mary', 'Henry', 'James', 'Joey']
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 4
Threads: 0
Joined: Dec 2019
Another one :p
import random
dates = ['Jan 6', 'Jan 8', 'Jan 10']
students = ['Joey', 'Henry', 'Daniel', 'James', 'Samantha', 'Jose', 'Salvator', 'Paul', 'Steve', 'Mary', 'Kyle',
'Marcus', 'Pat']
Nstud = (len(students) // len(dates))
l = [students[i * Nstud:((i + 1) * Nstud)] for i in range(len(students) // Nstud)]
final = {date: l[i] for i, date in enumerate(dates)}
print(final)
|