{ "metadata": { "name": "", "signature": "sha256:77aa3b046bf7d902f66b1b41d0d156ca76cc83fb058f165082693495f8ea4399" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "

Breakout 2 Solutions

\n", "\n", "

Sorting meetings

\n", "\n", "
\n", "\n", "

\n", "

Python Boot Camp

\n", "

June 9, 2014

\n", "\n", "\n", "
\n", "\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "\n", "### First, copy over the meeting and organizer information from [meetings.py](https://github.com/kialio/python-bootcamp/blob/master/DataFiles_and_Notebooks/02_AdvancedDataStructures/meetings.py). ###" ] }, { "cell_type": "code", "collapsed": true, "input": [ "organizers = { \"Extragalactic Journal Club\": \"Alaina Henry\", \"Gamma-Ray Burst Lunch\": \"Judy Racusin\",\\\n", " \"Astrophysics Colloquium\": \"Jeremy Schnittman\", \"Exoplanet Club\": \"Margaret Pan\",\\\n", " \"Python Users Group\": \"Terri Brandt\", \"IS&T Colloquium Series\": \"Ben Kobler\",\\\n", " \"NGAPS Happy Hour\": \"Toni Venters\" }" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "prompt_number": 5 }, { "cell_type": "code", "collapsed": true, "input": [ "# meeting, room, day, start time(decimal hours), end time\n", "meetings = [(\"Gamma-Ray Burst Lunch\",\"B34 E256\",\"Tue\",12.0,13.0), \\\n", " (\"Extragalactic Journal Club\",\"B34 S391\",\"Tue\",14.0,15.0), \\\n", " (\"Python Users Group\",\"B34 W120A/B\",\"Tue\",14.5,15.5), \\\n", " (\"Astrophysics Colloquium\",\"B34 E215\",\"Tue\",15.5,17.0), \\\n", " (\"NGAPS Happy Hour\",\"B34 E215\",\"Tue\",17.0,18.0), \\\n", " (\"Exoplanet Club\",\"B34 E215\",\"Tue\",11.5,12.5), \\\n", " (\"IS&T Colloquium Series\",\"B3 Auditorium\",\"Tue\",11.0,12.0) ]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "

Sorting by Meeting Name

\n", "### We can sort the meeting information by meeting name by running a simple sort on the list. ###\n" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Sort the list of meetings.\n", "meetings.sort() " ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "When we called flights.sort() we resorted the list based on the first element of each tuple (airline) and then, when multiple flights are operated by one ariline, by the flight number." ] }, { "cell_type": "code", "collapsed": false, "input": [ "for meeting in meetings:\n", " print meeting" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "('Astrophysics Colloquium', 'B34 E215', 'Tue', 15.5, 17.0)\n", "('Exoplanet Club', 'B34 E215', 'Tue', 11.5, 12.5)\n", "('Extragalactic Journal Club', 'B34 S391', 'Tue', 14.0, 15.0)\n", "('Gamma-Ray Burst Lunch', 'B34 E256', 'Tue', 12.0, 13.0)\n", "('IS&T Colloquium Series', 'B3 Auditorium', 'Tue', 11.0, 12.0)\n", "('NGAPS Happy Hour', 'B34 E215', 'Tue', 17.0, 18.0)\n", "('Python Users Group', 'B34 W120A/B', 'Tue', 14.5, 15.5)\n" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "

Printing the list

\n", "###Now we want to print out the sorted list in a nicer format.###\n", "\n", "Note that when printing, we lookup the destination name by the airport code key in the airports dictionary." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Print out the header. the \\t character prints a tab.\n", "print \"Meeting \\t\\tRoom No.\\tDay\\tTime\\tOrganizer\"\n", "print \"-\"*81 #81 instances of the \"-\" character\n", "\n", "# Loop through each of the flight tuples in the sorted list\n", "# Recall that each tuple contains the elements: (airline, number, destination lookup code, gate, time)\n", "for meeting in meetings:\n", " # Use the meeting name to lookup the organizer's name from the organizers directory\n", " talk_name = meeting[0]\n", " talk_name += \" \"*(32 - len(meeting[0])) # add the appropriate amount of whitespace after the Destination string\n", " organizer = organizers[meeting[0]]\n", " organizer += \" \"*(10 - len(organizer)) # add the appropriate amount of whitespace after the Destination string\n", " # Print the nicely formatted string. Don't forget to convert int and float types to strings using str()\n", " print talk_name + str(meeting[1]) + \"\\t\" + str(meeting[2]) + \"\\t\" + str(meeting[3]) + \"\\t\" + organizer " ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Meeting \t\tRoom No.\tDay\tTime\tOrganizer\n", "---------------------------------------------------------------------------------\n", "Astrophysics Colloquium B34 E215\tTue\t15.5\tJeremy Schnittman\n", "Exoplanet Club B34 E215\tTue\t11.5\tMargaret Pan\n", "Extragalactic Journal Club B34 S391\tTue\t14.0\tAlaina Henry\n", "Gamma-Ray Burst Lunch B34 E256\tTue\t12.0\tJudy Racusin\n", "IS&T Colloquium Series B3 Auditorium\tTue\t11.0\tBen Kobler\n", "NGAPS Happy Hour B34 E215\tTue\t17.0\tToni Venters\n", "Python Users Group B34 W120A/B\tTue\t14.5\tTerri Brandt\n" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "

Sorting by Meeting Time

\n", "### Sorting the information by time requires a bit more coding. ###\n", "First, we create a new list, time_ordered_meetings, which initially just contains the first element of the list meetings." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Create a new list, time_ordered, which initially just contains the first element of the list flights\n", "time_ordered_meetings = [meetings[0]]\n", "print time_ordered_meetings" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[('Astrophysics Colloquium', 'B34 E215', 'Tue', 15.5, 17.0)]\n" ] } ], "prompt_number": 10 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "It's easy to loop through the remaining meetings and insert them into the propoer position in time_ordered_meetings by comparing the day in each meeting tuple (at the fourth index position).\n", "\n", "We determine where the current meeting belongs by manually comparing the time of the meeting already added to time_ordered_meetings. (This is really trivial with lambda functions, which you'll learn later.)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Iterate through each of the remaining elements in flights to see where it should go in the sorted list\n", "for meeting in meetings[1:]:\n", " # Does it belong in the beginning?\n", " if meeting[3] < time_ordered_meetings[0][3]: #is the day of the meeting before the first list element?\n", " time_ordered_meetings.insert(0,meeting) # insert the meeting tuple at position 0 in the list \n", " continue\n", " ## ... or the end?\n", " if meeting[3] > time_ordered_meetings[-1][2]: #is the day of the meeting after the first list element?\n", " time_ordered_meetings.append(meeting) # insert the meeting tuple at the end of the list\n", " continue\n", " ## Or is it in the middle? Loop through each day to see if the current meeting is between two adjacent ones\n", " ## note that range(N) returns a list [0, 1, ... , N-1] \n", " for i in range(len(time_ordered_meetings) - 1): \n", " if meeting[3] >= time_ordered_meetings[i][3] and meeting[3] <= time_ordered_meetings[i+1][3]:\n", " time_ordered_meetings.insert(i+1,meeting) # insert the flight tuple at position i+1 in the list\n", " break" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 11 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "The printing procedure is the same as before." ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Meeting \\t\\tRoom No.\\tDay\\tTime\\tOrganizer\"\n", "print \"-\"*81 #81 instances of the \"-\" character\n", "for meeting in time_ordered_meetings:\n", " # Use the meeting name to lookup the organizer's name from the organizers directory\n", " talk_name = meeting[0]\n", " talk_name += \" \"*(32 - len(meeting[0])) # add the appropriate amount of whitespace after the Destination string\n", " organizer = organizers[meeting[0]]\n", " organizer += \" \"*(10 - len(organizer)) # add the appropriate amount of whitespace after the Destination string\n", " # Print the nicely formatted string. Don't forget to convert int and float types to strings using str()\n", " print talk_name + str(meeting[1]) + \"\\t\" + str(meeting[2]) + \"\\t\" + str(meeting[3]) + \"\\t\" + organizer \n" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Meeting \t\tRoom No.\tDay\tTime\tOrganizer\n", "---------------------------------------------------------------------------------\n", "IS&T Colloquium Series B3 Auditorium\tTue\t11.0\tBen Kobler\n", "Exoplanet Club B34 E215\tTue\t11.5\tMargaret Pan\n", "Gamma-Ray Burst Lunch B34 E256\tTue\t12.0\tJudy Racusin\n", "Extragalactic Journal Club B34 S391\tTue\t14.0\tAlaina Henry\n", "Python Users Group B34 W120A/B\tTue\t14.5\tTerri Brandt\n", "Astrophysics Colloquium B34 E215\tTue\t15.5\tJeremy Schnittman\n" ] } ], "prompt_number": 12 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### One line sorting solution. ###\n", "We can use the operator.itemgetter() function as the key in sort and sort by the time (4th) element." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import operator\n", "meetings.sort(key=operator.itemgetter(3))\n", "\n", "print \"Meeting \\t\\tRoom No.\\tDay\\tTime\\tOrganizer\"\n", "print \"-\"*81 #81 instances of the \"-\" character\n", "for meeting in meetings:\n", " talk_name = meeting[0]\n", " talk_name += \" \"*(32 - len(meeting[0]))\n", " organizer = organizers[meeting[0]]\n", " organizer += \" \"*(10 - len(organizer))\n", " print talk_name + str(meeting[1]) + \"\\t\" + str(meeting[2]) + \"\\t\" + str(meeting[3]) + \"\\t\" + organizer " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Meeting \t\tRoom No.\tDay\tTime\tOrganizer\n", "---------------------------------------------------------------------------------\n", "IS&T Colloquium Series B3 Auditorium\tTue\t11.0\tBen Kobler\n", "Exoplanet Club B34 E215\tTue\t11.5\tMargaret Pan\n", "Gamma-Ray Burst Lunch B34 E256\tTue\t12.0\tJudy Racusin\n", "Extragalactic Journal Club B34 S391\tTue\t14.0\tAlaina Henry\n", "Python Users Group B34 W120A/B\tTue\t14.5\tTerri Brandt\n", "Astrophysics Colloquium B34 E215\tTue\t15.5\tJeremy Schnittman\n", "NGAPS Happy Hour B34 E215\tTue\t17.0\tToni Venters\n" ] } ], "prompt_number": 15 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "###Alternate printing solution###\n", "Define how many spaces you want each string to occupy. Add enough trailing spaces to each element to fill this number. We'll go over string formatting more tomorrow. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"%.27s %.14s %.4s %.5s %.20s\" % (\"Meeting\"+27*' ', \"Room No.\"+14*' ', \"Day\"+4*' ', \"Time\"+5*' ', \"Organizer\"+20*' ')\n", "print \"-\"*72\n", "for meeting in meetings:\n", " print \"%.27s %.14s %.4s %.5s %.20s\" % (meeting[0]+27*' ', meeting[1]+14*' ', meeting[2]+4*' ', \\\n", " str(meeting[3])+5*' ', organizers[meeting[0]]+20*' ')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Meeting Room No. Day Time Organizer \n", "------------------------------------------------------------------------\n", "Astrophysics Colloquium B34 E215 Tue 15.5 Jeremy Schnittman \n", "Engineering Colloquium B3 Auditorium Mon 15.5 Brent Warner \n", "Exoplanet Club B34 E215 Tue 11.5 Margaret Pan \n", "Extragalactic Journal Club B34 S391 Tue 14.0 Alaina Henry \n", "Fermi Journal Club B34 E256 Wed 15.5 David Green \n", "Gamma-Ray Burst Lunch B34 E256 Tue 12.0 Judy Racusin \n", "Goddard Scientific Colloqui B3 Auditorium Fri 15.5 David Thompson \n", "IS&T Colloquium Series B3 Auditorium Tue 11.0 Ben Kobler \n", "NGAPS Happy Hour B34 E215 Tue 17.0 Toni Venters \n", "Python Users Group B34 W120A/B Tue 14.5 Terri Brandt \n" ] } ], "prompt_number": 24 }, { "cell_type": "markdown", "metadata": {}, "source": [ "###Extra Credit###\n", "What if we had meetings on other days of the week? How would we sort our meetings by both day of the week and time? (The one-liner solution comes tomorrow.)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "organizers = { \"Extragalactic Journal Club\": \"Alaina Henry\", \"Gamma-Ray Burst Lunch\": \"Judy Racusin\",\\\n", " \"Astrophysics Colloquium\": \"Jeremy Schnittman\", \"Exoplanet Club\": \"Margaret Pan\",\\\n", " \"Python Users Group\": \"Terri Brandt\", \"IS&T Colloquium Series\": \"Ben Kobler\",\\\n", " \"NGAPS Happy Hour\": \"Toni Venters\", \"Engineering Colloquium\": \"Brent Warner\", \\\n", " \"Fermi Journal Club\": \"David Green\", \"Goddard Scientific Colloquium\": \"David Thompson\" }\n", "\n", "# meeting, room, day, start time(decimal hours), end time\n", "meetings = [(\"Gamma-Ray Burst Lunch\",\"B34 E256\",\"Tue\",12.0,13.0), \\\n", " (\"Extragalactic Journal Club\",\"B34 S391\",\"Tue\",14.0,15.0), \\\n", " (\"Python Users Group\",\"B34 W120A/B\",\"Tue\",14.5,15.5), \\\n", " (\"Astrophysics Colloquium\",\"B34 E215\",\"Tue\",15.5,17.0), \\\n", " (\"NGAPS Happy Hour\",\"B34 E215\",\"Tue\",17.0,18.0), \\\n", " (\"Exoplanet Club\",\"B34 E215\",\"Tue\",11.5,12.5), \\\n", " (\"Fermi Journal Club\",\"B34 E256\",\"Wed\",15.5,16.5),\\\n", " (\"Engineering Colloquium\",\"B3 Auditorium\",\"Mon\",15.5,16.5), \\\n", " (\"Goddard Scientific Colloquium\",\"B3 Auditorium\",\"Fri\",15.5,16.5), \\\n", " (\"IS&T Colloquium Series\",\"B3 Auditorium\",\"Tue\",11.0,12.0) ]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 25 }, { "cell_type": "code", "collapsed": false, "input": [ "days_of_week=('Mon','Tue','Wed','Thu','Fri')\n", "meetings.sort(key=operator.itemgetter(3))\n", "meetings2 = [(meeting[0], meeting[1], meeting[2], meeting[3], meeting[4], days_of_week.index(meeting[2]) ) for meeting in meetings]\n", "meetings2.sort(key=operator.itemgetter(5))\n", "print \"%.27s %.14s %.4s %.5s %.20s\" % (\"Meeting\"+27*' ', \"Room No.\"+14*' ', \"Day\"+4*' ', \"Time\"+5*' ', \"Organizer\"+20*' ')\n", "print \"-\"*72\n", "for meeting in meetings2:\n", " print \"%.27s %.14s %.4s %.5s %.20s\" % (meeting[0]+27*' ', meeting[1]+14*' ', meeting[2]+4*' ', \\\n", " str(meeting[3])+5*' ', organizers[meeting[0]]+20*' ')\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Meeting Room No. Day Time Organizer \n", "------------------------------------------------------------------------\n", "Engineering Colloquium B3 Auditorium Mon 15.5 Brent Warner \n", "IS&T Colloquium Series B3 Auditorium Tue 11.0 Ben Kobler \n", "Exoplanet Club B34 E215 Tue 11.5 Margaret Pan \n", "Gamma-Ray Burst Lunch B34 E256 Tue 12.0 Judy Racusin \n", "Extragalactic Journal Club B34 S391 Tue 14.0 Alaina Henry \n", "Python Users Group B34 W120A/B Tue 14.5 Terri Brandt \n", "Astrophysics Colloquium B34 E215 Tue 15.5 Jeremy Schnittman \n", "NGAPS Happy Hour B34 E215 Tue 17.0 Toni Venters \n", "Fermi Journal Club B34 E256 Wed 15.5 David Green \n", "Goddard Scientific Colloqui B3 Auditorium Fri 15.5 David Thompson \n" ] } ], "prompt_number": 32 } ], "metadata": {} } ] }