#! /usr/bin/env python3 # def states_test ( ): #*****************************************************************************80 # ## states_test() tests the use of a dict to store state capitals. # import pprint print ( '' ) print ( 'states_test():' ) print ( ' Demonstrate the use of a dict to store state information.' ) state_dict = create_state_dict ( ) print ( '' ) print ( ' Initial state:capital dict:' ) print ( '' ) pprint.pprint ( state_dict ) state_dict = fix_state_dict ( state_dict ) print ( '' ) print ( ' Corrected state:capital dict:' ) print ( '' ) pprint.pprint ( state_dict ) # # What is the length of the dictionary? # print ( '' ) print ( ' len ( state_dict ) = ', len ( state_dict ) ) # # Print keys and values. # print ( '' ) print ( ' Print keys, values, and items' ) print ( ' Use list() to convert to a list.' ) print ( '' ) analyze_state_dict ( state_dict ) # # Iterate. # print ( '' ) print ( ' Iterate state:capital dict:' ) print ( '' ) iterate_state_dict ( state_dict ) # # Sample. # sample_num = 5 sample_state_dict ( state_dict, sample_num ) # # Query. # print ( '' ) print ( ' Query state.dict for keys and values.' ) print ( '' ) query_state_dict ( state_dict ) return def create_state_dict ( ): #*****************************************************************************80 # ## create_state_dict() creates a Python dict with state capital information. # # Discussion: # # Note that one state has the wrong name, one capital has the wrong name, # there is one state missing, and one "state" should be removed. # state_dict = { \ 'Alabama' : 'Montgomery', \ 'Alaska' : 'Juneau', \ 'Arizona' : 'Phoenix', \ 'Arkansas' : 'Little Rock', \ 'California' : 'Sacramento', \ 'Colorado' : 'Denver', \ 'Connecticut' : 'Hartford', \ 'Delaware' : 'Dover', \ 'Florida' : 'Tallahassee', \ 'Georgia' : 'Atlanta', \ 'Hawaii' : 'Honolulu', \ 'Idaho' : 'Boise', \ 'Illinois' : 'Springfield', \ 'Indiana' : 'Indianapolis', \ 'Iowa' : 'Des Moines', \ 'Kansas' : 'Topeka', \ 'Kentucky' : 'Frankfort', \ 'Louisiana' : 'Baton Rouge', \ 'Maine' : 'Augusta', \ 'Maryland' : 'Annapolis', \ 'Massachusetts' : 'Boston', \ 'Michigan' : 'Lansing', \ 'Minnesota' : 'Saint Paul', \ 'Mississippi' : 'Jackson', \ 'Missouri' : 'Jefferson City', \ 'Montana' : 'Helena', \ 'Nebraska' : 'Lincoln', \ 'Nevada' : 'Carson City', \ 'New Hampshire' : 'Concord', \ 'New Jersey' : 'Trenton', \ 'New Mexico' : 'Santa Fe', \ 'New York' : 'Albany', \ 'North Carolina' : 'Raleigh', \ 'North Dakota' : 'Bismarck', \ 'Ohio' : 'Columbus', \ 'Oklahoma' : 'Oklahoma City', \ 'Ontario' : 'Toronto', \ 'Dennsylvania' : 'Harrisburg', \ 'Rhode Island' : 'Providence', \ 'South Carolina' : 'Columbia', \ 'South Dakota' : 'Pierre', \ 'Tennessee' : 'Nashville', \ 'Texas' : 'Dallas', \ 'Utah' : 'Salt Lake City', \ 'Vermont' : 'Montpelier', \ 'Virginia' : 'Richmond', \ 'Washington' : 'Olympia', \ 'West Virginia' : 'Charleston', \ 'Wisconsin' : 'Madison', \ 'Wyoming' : 'Cheyenne' \ } return state_dict def fix_state_dict ( state_dict ): #*****************************************************************************80 # ## fix_state_dict() fixes a Python dict with state capital information. # # Discussion: # # One state (Dennsylvania, Harrisburg) had the wrong name, # one capital (Texas, Austin) had the wrong name, # one state (Oregon, Salem) was missing, # one "state" (Ontario, Toronto) had to be removed. # # # Replace Dennsylvania by Pennsylvania. # del state_dict['Dennsylvania'] state_dict['Pennsylvania'] = 'Harrisburg' # # Correct the capital of Texas. # state_dict['Texas'] = 'Austin' # # Add Oregon and its capital. # state_dict['Oregon'] = 'Salem' # # Remove Ontario # del state_dict['Ontario'] return state_dict def analyze_state_dict ( state_dict ): #*****************************************************************************80 # ## analyze_state_dict() lists keys, values, and key,value pairs. # print ( '' ) print ( ' state_dict.keys():' ) print ( state_dict.keys ( ) ) print ( '' ) print ( ' state_dict.values():' ) print ( state_dict.values ( ) ) print ( '' ) print ( ' state_dict.items():' ) print ( state_dict.items ( ) ) return def iterate_state_dict ( state_dict ): #*****************************************************************************80 # ## iterate_state_dict() iterates a Python dict with state capital information. # import numpy as np for state in state_dict: print ( ' The capital of ' + state + ' is ' + state_dict[state] ) return def sample_state_dict ( state_dict, sample_num ): #*****************************************************************************80 # ## sample_state_dict() samples a Python dict with state capital information. # import numpy as np for test in range ( 0, sample_num ): state = np.random.choice ( list ( state_dict.keys ( ) ) ) print ( '' ) print ( ' What is the capital of ' + state + '?' ) print ( ' (Correct answer is ' + state_dict[state] + ').' ) return def query_state_dict ( state_dict ): #*****************************************************************************80 # ## query_state_dict() queries a Python dict with state capital information. # import numpy as np for state in [ 'Pennsylvania', 'Harrisburg', 'Oz' ]: print ( state + ' is', end = '' ) if ( not state in state_dict.keys ( ) ): print ( ' NOT', end = '' ) print ( ' a key in state.dict' ) for capital in [ 'Salem', 'Toronto' ]: print ( capital + ' is', end = '' ) if ( not capital in state_dict.values ( ) ): print ( ' NOT', end = '' ) print ( ' a value in state.dict' ) return if ( __name__ == "__main__" ): states_test ( )