Computer Science 15-110, Fall 2010
Class Notes:  Recitation 11


Recitation 11

  1. Quiz 8
    Quiz 8 will be administered at the start of recitation.

  2. File I/O Practice:  NearestCountry and FarthestCountry
    Here, we'll write some functions that find the nearest country to a given country or US state and then the farthest country from a country or US state.  You may wish to start with this file:  nearestFarthestCountries.py.  We'll proceed in the following steps:

    1. loadStateLocations()
      Write a function, loadStateLocations, that reads the data from the file state-lat-long.txt and returns a dictionary mapping a 2-letter state name to a (lat, long) tuple.  You may wish to start from fileIO.py from our course notes.  Here is a test function for you:

      def testLoadStateLocations():
          print "Testing loadStateLocations...",
          locationsMap = loadStateLocations()
          assert(len(locationsMap) == 55) # a few "extras"!
          assert(locationsMap["PA"] == (40.5773, -77.264) )
          assert("bogusState" not in locationsMap)
          print "Passed!"

    2. loadCountryLocations()
      Write a function, loadCountryLocations, that reads the data from the file country-lat-long.txt and returns a dictionary mapping a 2-letter country name to a (lat, long) tuple.  Here is a test function for you:

      def testLoadCountryLocations():
          print "Testing loadCountryLocations...",
          locationsMap = loadCountryLocations()
          assert(len(locationsMap) == 240)
          assert(locationsMap["AD"] == ( 42.5000,  1.5000) )
          assert(locationsMap["ZW"] == (-20.0000, 30.0000) )
          assert("bogusCountry" not in locationsMap)
          print "Passed!"

    3. loadCountryNames()
      Write a function, loadCountryNames, that reads the data from the file country-names.txt and returns a dictionary mapping a 2-letter country name to its full name.  Here is a test function for you:

      def testLoadCountryNames():
          print "Testing loadCountryNames...",
          namesMap = loadCountryNames()
          assert(len(namesMap) == 246)
          assert(namesMap["AF"] == "AFGHANISTAN")
          assert(namesMap["ZW"] == "ZIMBABWE")
          assert("bogusCountry" not in namesMap)
          print "Passed!"

    4. loadCountryCodes(countryNames)
      Write a function, loadCountryCodes, that takes the countryNames dictionary from the previous step and produces the inverse dictionary, mapping the full names of countries to their codes.  Here is a test function for you:

      def testLoadCountryCodes():
          print "Testing loadCountryCodes...",
          namesMap = loadCountryNames()
          codesMap = loadCountryCodes(namesMap)
          assert(codesMap["AFGHANISTAN"] == "AF")
          assert(codesMap["ZIMBABWE"] == "ZW")
          assert("bogusCountry" not in codesMap)
          print "Passed!"

    5. findNearestCountry(location, countryLocations, countryNames)
      Write a function that takes a (lat, long) location, along with with the countryLocations and countryNames dictionaries from the previous steps, and returns the full name of the nearest country to that location that is not exactly at that location.  Here is a test function for you:

      def testFindNearestCountry():
          print "Testing findNearestCountry...",
          countryLocations = loadCountryLocations()
          countryNames = loadCountryNames()
          countryCodes = loadCountryCodes(countryNames)
          # Nearest country to Canada is the United States
          canadaCode = countryCodes["CANADA"]
          canadaLocation = countryLocations[canadaCode]
          canadaNeighbor = findNearestCountry(canadaLocation, countryLocations, countryNames)
          assert(canadaNeighbor == "UNITED STATES")
          print "Passed!"


    6. findNearestCountryToCountry(countryName, countryLocations, countryNames)
      Write a function that takes a country full name, along with with the countryLocations and countryNames dictionaries from the previous steps, and returns the full name of the nearest country to that location that is not exactly at that location.  Here is a test function for you:

      def testFindNearestCountryToCountry():
          print "Testing findNearestCountryToCountry...",
          countryLocations = loadCountryLocations()
          countryNames = loadCountryNames()
          assert(findNearestCountryToCountry("CANADA", countryLocations, countryNames) \
                 == "UNITED STATES")
          assert(findNearestCountryToCountry("UNITED STATES", countryLocations, countryNames) \
                 == "MEXICO")
          assert(findNearestCountryToCountry("MEXICO", countryLocations, countryNames) \
                 == "GUATEMALA")
          assert(findNearestCountryToCountry("GUATEMALA", countryLocations, countryNames) \
                 == "EL SALVADOR")
          assert(findNearestCountryToCountry("EL SALVADOR", countryLocations, countryNames) \
                 == "GUATEMALA")
          print "Passed!"

    7. findNearestCountryToState(stateCode, stateLocations, countryLocations, countryNames)
      Write a function that takes a 2-letter state code, along with with the stateLocations, countryLocations and countryNames dictionaries from the previous steps, and returns the full name of the nearest country to that location that is not exactly at that location.  Here is a test function for you:

      def testFindNearestCountryToState():
          print "Testing findNearestCountryToState...",
          stateLocations = loadStateLocations()
          countryLocations = loadCountryLocations()
          countryNames = loadCountryNames()
          assert(findNearestCountryToState("FL", stateLocations, countryLocations, countryNames) \
                 == "CUBA")
          assert(findNearestCountryToState("CA", stateLocations, countryLocations, countryNames) \
                 == "MEXICO")
          assert(findNearestCountryToState("AK", stateLocations, countryLocations, countryNames) \
                 == "CANADA")
          assert(findNearestCountryToState("NC", stateLocations, countryLocations, countryNames) \
                 == "BAHAMAS")
          assert(findNearestCountryToState("DE", stateLocations, countryLocations, countryNames) \
                 == "BERMUDA")
          assert(findNearestCountryToState("IA", stateLocations, countryLocations, countryNames) \
                 == "UNITED STATES")
          print "Passed!"

    8. Extensions (time permitting, after Data Analysis Practice, or on your own time)
      1. Fix bug in findNearestCountryToState
        As described, the nearest country to states in the middle of the United States will in fact be the United States.  Fix this so that the United States is excluded from consideration.

      2. Farthest rather than nearest
        Write findFarthestCountryFromCountry and findFarthestCountryFromState.
    9. Sample Solutions
      Spoiler Alert:  Don't peek unless you really want to see it, but...
      Here are some sample solutions to 1-7:  soln-nearestFarthestCountries.py

  3. Data Analysis Practice
    Review the examples and all the terms used in the course notes on Introduction to Data Analysis.


carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem