3

I'm trying to make a random group generator splitting a list of students into n nearly-equal partitions (for example, if there are 11 students and three groups, we would want two groups of four and one group of 3) . We need to repeat this process for x number of assignments. We read the list of students in from a file and read the groups out from a file. Here's the code I have so far:

import csv
import unittest
def studentgenerator(num_asmt, num_stud, student_list, 
assignment_teams):
    with open(student_list , "r") as student:
        list_students = csv.reader(student)

    student_groups = []

    for x in range (0, num_asmt):
        random.shuffle(list_students)
        div = len(list_students)/float(num_stud)


        for x in xrange(num_stud):
            student_groups = lst[int(round(div * x)): int(round(div * 
            (x + 1))]\
   for group in student_groups:
        with open(assignment_teams, "w") as team:
            list_assignment_groups = csv.writer(team)
            list_assignment_groups.writerow(group)



student_list.close()
assignment-teams.close()

I can't seem to get the partitioner to work the way I want it to, and I think that something's going wrong with reading in/out from files, but I'm not sure exactly what I'm doing wrong.

mohan08p
  • 5,002
  • 1
  • 28
  • 36
Musette
  • 31
  • 1
  • 1
  • 2
  • 1
    Could you provide a few example lines from your CSV file ? – ma3oun Feb 14 '18 at 16:28
  • In your code, the function `studentgenerator()` is created, but it is never actually called. This means that none of your code within the function will execute. The following line `student_groups = lst[int(round(div * x)): int(round(div * (x + 1))]\` is looking for a variable called `lst` which does not exist in your code AND ends in a `\` which is not needed here. There may be more errors, but those come to mind first. Edit your code, run it & provide additional details as needed: sample input data, desired vs actual output, any error messages. All of these are part of a good question. – E. Ducateme Feb 14 '18 at 16:40

2 Answers2

7

You can define a chunker to do the splitting for you. In fact, this one is a genuine "generator". Courtesy of @Ned Batchelder (upvote there).

import random

def chunks(l, n):
    """Yield successive n-sized chunks from l."""
    for i in range(0, len(l), n):
        yield l[i:i + n]

n = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K']

random.shuffle(n)

list(chunks(n, 3))
# [['B', 'H', 'G'], ['D', 'A', 'C'], ['E', 'F', 'I'], ['J', 'K']]
jpp
  • 159,742
  • 34
  • 281
  • 339
  • 1
    Nice. I always forget that slicing of a list past the end is possible without raising an `IndexError`. – Graipher Feb 14 '18 at 17:15
0
import random
while True:
    n    = int(input("Enter the class strength"))
    students = [[i] for i in range(1,n+1)]
    x    = len(students)
    tNum     = 0

    #Nmber of teams
    print('The possible number of teams are')
    for i in range(1, x + 1):
        if x % i == 0:
            print(i)

   y        = int(input("Enter the no of teams"))

   if n == 0 or y == 0:
    print("Enter valid entries please")
    continue        

   z     = int(x/y)

   #Loop
   while x > 0:
       if x%z == 0:
            print("Team",tNum + 1)
            tNum +=1
            choice = random.choice(students)
            students.remove(choice)
            x = len(students)
            print(choice)

   q = input("Do you want to close or continue ? [y/n]")

   if q == "y":
       continue
   else:
       break