Introduction to Computer Science: The Way of the Program — Homework 9

Due by 11:59pm Tuesday, November 7

Reading

Instructions

Download and unzip the folder assign9_files.zip, which contains all of the files you'll need for this assignment. You should put your code for this assignment in the file assign9.py, which is included in the folder. Test cases for the Autotester are available for all exercises except Part 2 (playTTT).


Part 1: The Word Search Program

  1. Copy and paste your code for the five functions from Lab 9 listed below into your assign9.py file. You DO NOT need to include any other functions from Lab 9 (such as readGrid, numRows, numColumns, printGrid, directionNames, nextPosition, etc.). Checklist:


Part 2: Tic-Tac-Toe

  1. In class, we developed the function playTTT(size) to play the game of Tic-Tac-Toe (shown below). However, this function does not currently detect the case of a "cat's game" — that is, when all spaces on the board have been filled in without either player having won the game. Modify this program so that if a cat's game occurs, it will print "Sorry, cat's game" and then terminate the game.

    def playTTT(size):
        board = makeBoard(size)
        printBoard(board)
        gameOver = False
        while gameOver == False:
            makeMove(board, 'x')
            if checkForWin(board, 'x') == True:
                print("x wins!")
                gameOver = True
            else:
                makeMove(board, 'o')
                if checkForWin(board, 'o') == True:
                    print("o wins!")
                    gameOver = True
                else:
                    pass
        print("Thanks for playing.")
    

Part 3: Zip Code Database


  1. Write the function milesBetween(database), which takes the database dictionary as input, asks the user for two zip codes, and reports the distance in miles between the locations, as shown in the examples below. You should use the function lookupByZip that we wrote in class as a helper function to ask the user for a zip code and retrieve the appropriate record from the database. You can use the helper function geographicDistance (included in assign9.py) to calculate the distance in miles, based on the latitude/longitude coordinates of the two zip codes.

    >>> milesBetween(db)
    Enter zip code: 10708
    Enter zip code: 10001
    Bronxville, NY 10708 and New York, NY 10001 are 15.6 miles apart
    
    >>> milesBetween(db)
    Enter zip code: 90210
    Enter zip code: 12345
    Beverly Hills, CA 90210 and Schenectady, NY 12345 are 2451.3 miles apart
    
    >>> milesBetween(db)
    Enter zip code: 99701
    Enter zip code: 33330
    Fairbanks, AK 99701 and Fort Lauderdale, FL 33330 are 3935.0 miles apart
    

  2. Many cities have multiple zip codes. For example, the database contains 162 different zip codes for New York, NY. Write a function called samePlace(zip1, zip2, database) that takes two zip codes and the database dictionary as input, and returns True if the corresponding records have the same city and state, or False otherwise. For example:

    db['10003'] => {'city': 'New York', 'state': 'NY', 'zip': '10003', ...}
    db['10035'] => {'city': 'New York', 'state': 'NY', 'zip': '10035', ...}
    
    >>> print(samePlace("10003", "10035", db))
    True
    
    db['10701'] => {'city': 'Yonkers', 'state': 'NY', 'zip': '10701', ...}
    db['10708'] => {'city': 'Bronxville', 'state': 'NY', 'zip': '10708', ...}
    
    >>> print(samePlace("10701", "10708", db))
    False
    
    db['10601'] => {'city': 'White Plains', 'state': 'NY', 'zip': '10601', ...}
    db['23893'] => {'city': 'White Plains', 'state': 'VA', 'zip': '23893', ...}
    
    >>> print(samePlace("10601", "23893", db))
    False
    

  3. Write a function called allZips(database) that uses lookupByCity to ask the user for a location in "city, state" form, and prints out all zip codes in the database that represent the same city and state, along with the total number of such zip codes. Hint: use your samePlace function as a helper to determine whether two zip codes represent the same city and state. Your program should work as shown below (but remember that the order of items in a dictionary is unspecified, so your program might print out the zip codes in a different order, which is fine):

    >>> allZips(db)
    Enter city, state: Yonkers, NY
    Yonkers, NY 10701
    Yonkers, NY 10702
    Yonkers, NY 10703
    Yonkers, NY 10704
    Yonkers, NY 10705
    Yonkers, NY 10710
    6 zip codes found for Yonkers, NY
    
    >>> allZips(db)
    Enter city, state: New York, NY
    New York, NY 10001
    New York, NY 10002
    New York, NY 10003
    New York, NY 10004
    New York, NY 10005
    ...
    162 zip codes found for New York, NY
    

EXTRA CREDIT PROBLEMS (OPTIONAL)

You should finish the other problems first before attempting these.

  1. Write a function called neighbors(database) that uses lookupByZip to ask the user for a zip code, and prints out all zip codes in neighboring cities or towns up to 3 miles away. Zip codes that are 0 miles away or have the same city and state as the specified zip code should not be printed. If there are no neighboring cities within 3 miles, the message "No neighboring cities or towns" should be printed. Your program should work as shown below (but it might print out the neighboring cities in a different order, which is fine):

    >>> neighbors(db)
    Enter zip code: 10701
    Neighbors of Yonkers, NY 10701:
    Mount Vernon, NY 10550 is 3.0 miles away
    Hastings On Hudson, NY 10706 is 2.9 miles away
    Tuckahoe, NY 10707 is 2.8 miles away
    Bronxville, NY 10708 is 2.0 miles away
    Eastchester, NY 10709 is 2.9 miles away
    
    >>> neighbors(db)
    Enter zip code: 10003
    Neighbors of New York, NY 10003:
    Hoboken, NJ 07030 is 2.6 miles away
    Jersey City, NJ 07310 is 2.6 miles away
    Long Island City, NY 11101 is 3.0 miles away
    Brooklyn, NY 11201 is 2.5 miles away
    Brooklyn, NY 11206 is 2.9 miles away
    Brooklyn, NY 11211 is 2.7 miles away
    Brooklyn, NY 11222 is 2.2 miles away
    
    >>> neighbors(db)
    Enter zip code: 87901
    Neighbors of Truth Or Consequences, NM 87901:
    No neighboring cities or towns
    

  2. Write a function called closest(database) that uses lookupByCity to ask the user for a location in "city, state" form, and reports the closest big city with a population over 100,000. Remember that many cities have multiple zip codes. For example, the closest city with a population over 100,000 to New York, NY should not be New York, NY, but some other city. Examples:

    >>> closest(db)
    Enter city, state: Bronxville, NY
    Closest big city is Yonkers, NY (population 196086)
    
    >>> closest(db)
    Enter city, state: New York, NY
    Closest big city is Jersey City, NJ (population 240055)
    
    >>> closest(db)
    Enter city, state: Kansas City, KS
    Closest big city is Kansas City, MO (population 441545)
    
    >>> closest(db)
    Enter city, state: Truth Or Consequences, NM
    Closest big city is El Paso, TX (population 563662)
    

  3. Write a function called nearby(database) that prompts the user for a city and state, a distance in miles, and a minimum population size, and prints out all cities within the specified distance having a population greater than or equal to the specified size. List the cities by name, state, and population only (no zip codes or distances). You should not report the same city more than once. If there are no cities that match the search criteria, you should print out the message "None found". Examples:

    >>> nearby(db)
    Enter city, state: Lawton, OK
    Finding cities near Lawton, OK...
    Up to how many miles away? 100
    With at least how many people? 25000
    
    Cities of size 25000 or more within 100 miles of Lawton, OK:
    Edmond, OK (population 68315)
    Norman, OK (population 95694)
    Oklahoma City, OK (population 506132)
    Shawnee, OK (population 28692)
    Wichita Falls, TX (population 104197)
    
    >>> nearby(db)
    Enter city, state: Lawton, OK
    Finding cities near Lawton, OK...
    Up to how many miles away? 100
    With at least how many people? 750000
    
    Cities of size 750000 or more within 100 miles of Lawton, OK:
    None found
    

Turning in Your Homework