'''
Created on Oct 5, 2011

@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
    return a list of strings of all the lines from a file
    '''
    #TODO:
    '''
    lines = []
    for line in file:
        line = line.strip()
        lines.append(line)
    return lines
    '''
    return [line.strip()   for line in file]
    
    
    

''' This function was not part of class but I added it and
modified it after a student emailed it to me. 
Note it works a little differently as it returns a new list.
The other randomize function does not return anything, just
modifies the list
'''
def randomize2(students):   
    # student emailed me this alternate soln
    randomstuds = []
    for stud in students:
        ind = random.randint(0, len(students)-1)
        randomstuds.append(students[ind])
        students.pop(ind)
    return randomstuds
    
def randomize(students):
    '''
    shuffle the students in the list
    '''
    #TODO:
    for num in range(0, len(students)):
        randomPosition = random.randint(0, len(students)-1)
        ''' swap the list items in "num" and "randomPosition"  '''
        temp = students[num]
        students[num] = students[randomPosition]
        students[randomPosition] = temp
        
        # alternate Swap below here
        #students[num], students[randomPosition] = students[randomPosition], students[num]

# note no return needed but can put a simple return with no
# value if you want

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

# note next call changes the elements in students, 
# randomize does not return a value, but after calling it the 
# elements in list have been moved around.
randomize(students)  

# next line is if I want to call an alternate solution randomize2
# that creates a new list of random students and returns a list
# note the difference in how this is called and how randomize is called

#students = randomize2(students)     

print students
print
printGroups(students)