'''
Created on Oct 5, 2011, Modified Oct 20, 2014

@author: rodger
'''
import os
import tkFileDialog
import random

    
def choose_file_to_open():
    """
    prompt for existing file, return the file (open for reading)
    """
    file = tkFileDialog.askopenfile(title="choose a file", initialdir=os.getcwd())
    return file

def processFile(file):
    '''
    process the lines in a file (which has one name per line),
    return a list of names

    #TODO:
    lines = []
    for line in file:
        lines.append(line.strip())
    return lines
    '''
    return [line.strip()   for line in file]
    
    
    
    
    return 
    
def randomize(students):
    '''
    shuffle the students in the list and return this new list
    '''
    #TODO:
    for num in range(0, len(students)):
        randomPosition = random.randint(0, len(students)-1 )
        # swap the items in positions num and randomPosition
        '''
        These two lines do not swap a student 
        students[num] = students[randomPosition]
        students[randomPosition] = students[num]
        '''
        # these three lines do swap, store one in 
        # a temp spot first
        temp = students[num]
        students[num] = students[randomPosition]
        students[randomPosition] = temp
    return students

# NOTE IF you are going to change a list that is 
# also the increment in the for loop, then you should
# use a copy of the list
def randomize2(students): 
    studentsA = students[:]
    randomstuds = []
    print "length of students", len(students) 
    for stud in students:
        print "length of studentsA", len(studentsA)    
        ind = random.randint(0, len(studentsA)-1)
        randomstuds.append(studentsA[ind])
        studentsA.pop(ind)
    print "length of students", len(students) 
    print "length of randomstudents", len(randomstuds)
    return randomstuds


def printGroups(students):
    ''' print the students 3 per group with Group number starting w/1 '''
    count = 1
    print "Group 1:"
    for stud in students:
        print stud
        #TODO: add code to print 3 per group
        if count % 3 == 0:
            print
            print "Group "  + str(count/3 + 1)+ ":" 


        count += 1

    

students = processFile(choose_file_to_open()) 
print students
#rstudents = randomize(students)  
rstudents = randomize2(students)     
print students
print rstudents
printGroups(students)
