How can I make this code easy, I want to make groups of three people, and will return a list of groups of 3
Question:
how can I make the code easy?
The function which was developed contains the following inputs:
- students: a list with names of students
- events: an integer specifying how many group assignments should be created
- group size: an integer specifying how big a group can be at maximum.
The function should return a list of group assignments. The following shall apply to these:
Each student must appear in a group only once per group assignment.
A group assignment may not appear more than once in the result.
The group assignment must be random
import random
courseparticipants = [
'LeBron James',
'Giannis Antetokounmpo',
'Kevin Durant',
'Steph Curry',
'Kyrie Irving',
'Joel Embiid',
'Kawhi Leonard',
'Paul George',
'James Harden',
'Kemba Walker',
'Khris Middleton',
'Anthony Davis',
'Nikola Jokić',
'Klay Thompson',
'Ben Simmons',
'Damian Lillard',
'Blake Griffin',
'Russell Westbrook',
'D'Angelo Russell',
'LaMarcus Aldridge',
'Nikola Vučević',
'Karl-Anthony Towns',
'Kyle Lowry',
'Bradley Beal',
'Dwyane Wade',
'Dirk Nowitzki'
]
def groupclassification(students, events, groupsize):
group = []
counter = len(students) / groupsize
studis = []
studis = studis + students
if events > 0:
while counter >= 1:
part1 = int(random.random() * len(studis))
part2 = int(random.random() * len(studis))
part3 = int(random.random() * len(studis))
if part1 != part2 and part1 != part3 and part2 != part3:
group = group + [[studis[part1], studis[part2], studis[part3]]]
x = studis[part1]
y = studis[part2]
z = studis[part3]
studis.remove(x)
studis.remove(y)
studis.remove(z)
counter -= 1
print(group)
if len(studis) > 0:
print("The following students were lost along the way:", studis)
events -= 1
groupclassification(students, events, groupsize)
groupclassification(courseparticipants, 5, 3)
Does anyone have any suggestions as to how I can make this code easier on the eye? Be as ruthless as you please.
Answers:
Here you go my friend, learn from it 😀
import random
courseparticipants = [
'LeBron James','Giannis Antetokounmpo','Kevin Durant',
'Steph Curry','Kyrie Irving','Joel Embiid',
'Kawhi Leonard','Paul George', 'James Harden',
'Kemba Walker', 'Khris Middleton', 'Anthony Davis',
'Nikola Jokić', 'Klay Thompson', 'Ben Simmons',
'Damian Lillard', 'Blake Griffin', 'Russell Westbrook',
'DAngelo Russell', 'LaMarcus Aldridge', 'Nikola Vučević',
'Karl-Anthony Towns', 'Kyle Lowry', 'Bradley Beal',
'Dwyane Wade', 'Dirk Nowitzki'
]
def group(students,events,groupsize):
count = len(students)//groupsize
counter = events
groupofgroups = []
if events > 0 and groupsize > 0:
for i in range(count):
counter = counter - 1
if counter < 0:
break
group = []
for j in range(groupsize):
rnd_index = random.randint(0,len(students)-1)
group.append(students[rnd_index])
students.remove(students[rnd_index])
groupofgroups.append(group)
print(groupofgroups)
print('leftover', students)
group(courseparticipants,6,2)
how can I make the code easy?
The function which was developed contains the following inputs:
- students: a list with names of students
- events: an integer specifying how many group assignments should be created
- group size: an integer specifying how big a group can be at maximum.
The function should return a list of group assignments. The following shall apply to these:
Each student must appear in a group only once per group assignment.
A group assignment may not appear more than once in the result.
The group assignment must be random
import random
courseparticipants = [
'LeBron James',
'Giannis Antetokounmpo',
'Kevin Durant',
'Steph Curry',
'Kyrie Irving',
'Joel Embiid',
'Kawhi Leonard',
'Paul George',
'James Harden',
'Kemba Walker',
'Khris Middleton',
'Anthony Davis',
'Nikola Jokić',
'Klay Thompson',
'Ben Simmons',
'Damian Lillard',
'Blake Griffin',
'Russell Westbrook',
'D'Angelo Russell',
'LaMarcus Aldridge',
'Nikola Vučević',
'Karl-Anthony Towns',
'Kyle Lowry',
'Bradley Beal',
'Dwyane Wade',
'Dirk Nowitzki'
]
def groupclassification(students, events, groupsize):
group = []
counter = len(students) / groupsize
studis = []
studis = studis + students
if events > 0:
while counter >= 1:
part1 = int(random.random() * len(studis))
part2 = int(random.random() * len(studis))
part3 = int(random.random() * len(studis))
if part1 != part2 and part1 != part3 and part2 != part3:
group = group + [[studis[part1], studis[part2], studis[part3]]]
x = studis[part1]
y = studis[part2]
z = studis[part3]
studis.remove(x)
studis.remove(y)
studis.remove(z)
counter -= 1
print(group)
if len(studis) > 0:
print("The following students were lost along the way:", studis)
events -= 1
groupclassification(students, events, groupsize)
groupclassification(courseparticipants, 5, 3)
Does anyone have any suggestions as to how I can make this code easier on the eye? Be as ruthless as you please.
Here you go my friend, learn from it 😀
import random
courseparticipants = [
'LeBron James','Giannis Antetokounmpo','Kevin Durant',
'Steph Curry','Kyrie Irving','Joel Embiid',
'Kawhi Leonard','Paul George', 'James Harden',
'Kemba Walker', 'Khris Middleton', 'Anthony Davis',
'Nikola Jokić', 'Klay Thompson', 'Ben Simmons',
'Damian Lillard', 'Blake Griffin', 'Russell Westbrook',
'DAngelo Russell', 'LaMarcus Aldridge', 'Nikola Vučević',
'Karl-Anthony Towns', 'Kyle Lowry', 'Bradley Beal',
'Dwyane Wade', 'Dirk Nowitzki'
]
def group(students,events,groupsize):
count = len(students)//groupsize
counter = events
groupofgroups = []
if events > 0 and groupsize > 0:
for i in range(count):
counter = counter - 1
if counter < 0:
break
group = []
for j in range(groupsize):
rnd_index = random.randint(0,len(students)-1)
group.append(students[rnd_index])
students.remove(students[rnd_index])
groupofgroups.append(group)
print(groupofgroups)
print('leftover', students)
group(courseparticipants,6,2)