samedi 25 avril 2015

Python Random Seat Generator with Memory

So i've done some searching around for similar projects to this and haven't come across anything that has pushed me in the right direction yet as to how to tackle this project so I'm coming here to Stack to get some advice. I am not necessarily looking for the full solution to this project, as I would like to tackle it myself, just simply looking for some advice on how to approach it.

What I'm trying to do

I am trying to write a program to randomly generate seating charts for eight weeks of a class. It reads a list of 80 names from an excel file and spits out another excel file, within this output file are 8 worksheets, one for each week, each with a different randomly generated 8x10 seating chart. Easy right?

There are three additional criteria that I would like to achieve that make this a bit more perplexing:

  • I would like avoid having any one student sit next to (in front of, behind, or to the side of) the same student for any two weeks
  • I would not like to have any one student sit in the front or back rows for more than one week as well
  • These students live in dormitories together, and I would like to not have any students from the same room sitting next to one another during any week

This is for an 8 week MBA class and the whole reason why I am trying to do this is to introduce the students to new peers and spark up new conversations.

What I've done so far

from openpyxl import Workbook, load_workbook
import random
import itertools


load_wb = raw_input('What is the name of the file containing your students?\n')
num_of_weeks = int(raw_input('How many weeks would you like?\n'))


dest_filename = 'seating_chart.xlsx'
students = []


load_wb = load_workbook(load_wb).active

for cell in load_wb.iter_rows():
  students.append(cell[0].value)


def make_grid():
  #Make the 8 x 10 grid
  y_list = list(range(1, 11))
  x_list = list(range(1, 9))
  grid = []

  for y in y_list:
    for x in x_list:
      grid.append((x,y))

  return grid



save_wb = Workbook()
grid = make_grid()
for week in range(num_of_weeks):
  week +=1 
  if week == 1:
    ws = save_wb.active
  else:
    ws = save_wb.create_sheet()
  ws.title = 'Week '+str(week)

  #Randomly shuffle the students array
  random.shuffle(students)
  for x, student in itertools.izip(grid, students):
    x,y = x
    ws.cell(row=x, column=y, value=student)


save_wb.save(filename=dest_filename)

I know that I am obviously going to have to store the values of where each student is sitting each week and reference these when generating the random charts but I am relatively new to Python and not sure how to best approach this.

I appreciate everyone's advice in advance! :)

Joey




Aucun commentaire:

Enregistrer un commentaire