Dance Card Example

Given a list of gentlemen and ladies, produce a list of partners for 8 dances for each of the dancers.

Approach

Use a simple random assortment, and rotate the list for each of the 8 dances.


In [ ]:
# comments are preceeded by a pound sign (this line is a comment)
# a python list is surrounded by backets, [ ], and is separated by commas
# manually assign the gentlemen's names:
gentlemen = [
    "Marshall Ruddock",
    "Emile Malsam",
    "Lloyd Summerlin",
    "Vern Pagaduan",
    "Chuck Maddux",
    "Edmundo Oleary",
    "Miquel Till",
    "Oswaldo Tidwell",
    "Elwood Yokoyama",
    "Leonardo Mcswain",
    "Roberto Averill",
    "Pierre Foose",
    "Saul Vannatta",
    "Woodrow Dunn",
    "Rene Mcelfresh",
    "Brett Ausmus",
    "Markus John",
    "Walker Kirk",
    "Warner Trunnell",
    "Keenan Tope"]

In [ ]:
# a list of ladies:
ladies = [
    "Grayce Schwandt",
    "Demetria Shiflet",
    "Leda Leavitt",
    "Julianne Tinnin",
    "Angela Turco",
    "Lu Rolfe",
    "Audry Hardnett",
    "Miranda Drapeau",
    "Ferne Vanscyoc",
    "Marceline Rochford",
    "Han Myer",
    "Karlene Emmer",
    "Agatha Cheever",
    "Shenna Riffle",
    "Vella Albanese",
    "Apryl Zieman",
    "Jacklyn Coronado",
    "Azzie Degreenia",
    "Sebrina Weddle",
    "Valda Pietila"]

In [ ]:
len(ladies), len(gentlemen) # let's see if they are the same length...
So, now that we have our lists, let's reverse the last and first names for better alphabetizing ...

In [ ]:
# Python provides an intuitive way to loop over a list...
gentlemen_last_first = [] # create an empty list
for man in gentlemen:  # A loop ... this is actually an extremely powerful concept
    first, last = man.split(" ") # indentation indicates a 'code block.' Whitespace is important in Python
    gentlemen_last_first.append("{0}, {1}".format(last, first)) # wait, what's going on here? ... string formatting

NB: There is a thourough introduction to string formatting at the excellent dive into python page. Among other things, it explains that in the above string "{0}, {1}", the curly brackets and numbers (0 and 1) are just placeholders whose values are replaced by the arguments of the format method that is called on the string.


In [ ]:
gentlemen_last_first

In [ ]:
# Python provides an intuitive way to loop over a list...
ladies_last_first = [] # create an empty list
for lady in ladies:  # A loop ... this is actually an extremely powerful concept
    first, last = lady.split(" ") # indentation indicates a 'code block.' Whitespace is important in Python
    ladies_last_first.append("{0}, {1}".format(last, first))

In [ ]:
ladies_last_first

In [ ]:
gentlemen_last_first.sort()
ladies_last_first.sort()

In [ ]:
ladies_last_first

In [ ]:
import random # let's bring in some code from the random built-in library of python

In [ ]:
random.shuffle(ladies_last_first)

In [ ]:
ladies_last_first

In [ ]:
dances = ["mix", "mix", "mix", "mix", "mix", "mix", "Shoe Dance"] # the list of dances

# initialize data structures
gentlemen_dances = {}
ladies_dances = {}

for man in gentlemen_last_first:
    gentlemen_dances[man] = []
for lady in ladies_last_first:
    ladies_dances[lady] = []

for dance in dances:
    #print "#####", dance, "########"
    count = 0
    for man in gentlemen_last_first:
        if dance == "mix":
            gentlemen_dances[man].append(ladies_last_first[count])
            ladies_dances[ladies_last_first[count]].append(man)
            
        else:
            gentlemen_dances[man].append(dance)
            ladies_dances[ladies_last_first[count]].append(dance)
        count += 1
    if dance == "mix":
        gentlemen_last_first.insert(0, gentlemen_last_first.pop(-1))

In [ ]:
gentlemen_dances

In [ ]:
#save result for dudes
fh = open("./dancecards-gentlemen.txt", 'wa')
for man in gentlemen_last_first:
    fh.write("".join(["\n~", man, "~\n"]))
    d_cnt = 1
    for partner in gentlemen_dances[man]:
        fh.write(str(d_cnt) + ". " + partner + "\n")
        d_cnt += 1
fh.close()

In [ ]:
#save result for ladies
fh = open("./dancecards-ladies.txt", 'wa')
for lady in ladies_last_first:
    fh.write("".join(["\n~", lady, "~\n"]))
    d_cnt = 1
    for partner in ladies_dances[lady]:
        fh.write(str(d_cnt) + ". " + partner + "\n")
        d_cnt += 1
fh.close()

In [ ]: