[Tutor] Group generator works!

Timothy Wilson wilson@visi.com
Thu, 29 Mar 2001 21:51:41 -0600 (CST)


Hi everyone,

Thanks to all who offered suggestions as I worked my way through one of my
first OOP attempts. My small program to create random student groups now
works! At the risk of filling you inboxes <g>, I've included the code and a
sample interpretor session below. (I haven't coded a user interface yet.)

Right now the program only allows the instructor to specify a certain number
of groups and requires the students to be read from a file.

Possible refinements:
1. Optional direct input of students at a prompt.
2. Create groups by specifying the group's size rather than number of
groups.
3. Require gender mixing

Current questions:
1. Do I need to seed the whrandom module to get more truly random numbers?
2. The assignGroups method looks rather crude to me. Can anyone suggest a
more elegant solution?
3. Anyone see an OOP faux pas?

Thanks again for the help. Here's the code:

#!/usr/bin/python

# makegroups.py by Tim Wilson <wilson@visi.com>
# Thanks to Benoit Dupire, Blake Winton, and others on the tutor list
# for their advice.

import string, whrandom

class Student:
	def __init__(self, ln, fn, gender):
		self.ln = ln
		self.fn = fn
		self.gender = gender
		self.group = 0
		self.assignedToGroup = 0
	def __str__(self):
		return "%s, %s" % (self.ln, self.fn)
	def setGroup(self, groupNumber):
		self.group = groupNumber

class Teacher:
	def __init__(self, name):
		self.name = name
		self.studentRoster = []
		self.numStudents = 0
		self.groupList = []
		self.numGroups = 0
	def __str__(self):
		return "%s has %s students" % (self.name, self.numStudents)
	def addStudentFromFile(self, datafile):
		list = open(datafile, 'r').readlines()
		for entry in list:
			s = string.split(entry, ',')
			self.studentRoster.append(Student(s[0], s[1],
s[2][:1]))
		self.numStudents = self.numStudents + len(list)
	def printStudentRoster(self):
		for st in self.studentRoster:
			print st		
	def makeGroup(self, groupID):
		self.groupList.append(groupID)
		self.numGroups = self.numGroups + 1
	def addStudentToGroup(self, st, grp):
		st.group = grp
		st.assignedToGroup = 1
	def unassignStudent(self, st):
		st.__init__(st.ln, st.fn, st.gender)
	def printGroups(self):
		for grp in self.groupList:
			print "Group #%s" % grp
			print '=========='
			for st in self.studentRoster:
				if st.group == grp:
					print st
			print
	def clearGroups(self):
		for st in self.studentRoster:
			self.unassignStudent(st)
		self.groupList = []
	def assignGroups(self, numGroups):
		self.numGroups = numGroups
		smallestGroupSize = self.numStudents/self.numGroups
		groupIndex = []
		for i in range(numGroups):
			groupIndex.append(smallestGroupSize)			
		for j in range(self.numStudents % self.numGroups):
			groupIndex[j] = groupIndex[j] + 1
		for k in range(1, self.numGroups + 1):
			self.makeGroup(k)
		for grp in self.groupList:
			for x in range(groupIndex[grp-1]):
				unassignedStudents = []
				for y in self.studentRoster:
					if not y.assignedToGroup:
						unassignedStudents.append(y)
				selected =
whrandom.choice(unassignedStudents)
				self.addStudentToGroup(selected, grp)

Python 1.5.2 (#0, Apr  3 2000, 14:46:48)  [GCC 2.95.2 20000313 (Debian
GNU/Linux)] on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import makegroups
>>> wilson = makegroups.Teacher('Tim Wilson')
>>> wilson.addStudentFromFile('class.dat')
>>> wilson.assignGroups(5)
>>> wilson.printGroups()
Group #1
==========
Boop, Betty
Watters, Aaron
Saoirse, Deirdre

Group #2
==========
Gauld, Alan
Rubble, Betty
Lutz, Mark

Group #3
==========
Flintstone, Wilma
Yoo, Danny
Van Laningham, Ivan

Group #4
==========
Wilson, Tim
Crawford, Cindy

Group #5
==========
Van Rossum, Guido
Ascher, David

>>> wilson.clearGroups()
>>> wilson.assignGroups(4)
>>> wilson.printGroups()
Group #1
==========
Van Rossum, Guido
Boop, Betty
Gauld, Alan
Saoirse, Deirdre

Group #2
==========
Yoo, Danny
Wilson, Tim
Van Laningham, Ivan

Group #3
==========
Flintstone, Wilma
Rubble, Betty
Lutz, Mark

Group #4
==========
Crawford, Cindy
Ascher, David
Watters, Aaron

>>>

--
Tim Wilson      | Visit Sibley online:         | Check out:
Henry Sibley HS | http://www.isd197.k12.mn.us/ | http://www.zope.org/
W. St. Paul, MN |                              | http://slashdot.org/
wilson@visi.com |   <dtml-var pithy_quote>     | http://linux.com/