def main(): # create a mapping of state to abbreviation states = hashmap.new() hashmap.set(states, 'Oregon', 'OR') hashmap.set(states, 'Florida', 'FL') hashmap.set(states, 'California', 'CA') hashmap.set(states, 'New York', 'NY') hashmap.set(states, 'Michigan', 'MI') # create a basic set of states and some cities in them cities = hashmap.new() hashmap.set(cities, 'CA', 'San Francisco') hashmap.set(cities, 'MI', 'Detroit') hashmap.set(cities, 'Fl', 'Jacksonville') # add some more cities hashmap.set(cities, 'NY', 'New York') hashmap.set(cities, 'OR', 'Portland') # print out some cities print '-' * 10 print 'NY State has: %s' % hashmap.get(cities, 'NY') print "OR State has: %s" % hashmap.get(cities, 'OR') # print some states print '-' * 10 print "Michigan's abbreviation is: %s" % hashmap.get(states, 'Michigan') print "Florida's abbreviation is: %s" % hashmap.get(states, 'Florida') # do it by using the state then cities dict print '-' * 10 print "Michigan has: %s" % hashmap.get(cities, hashmap.get(states, "Michigan")) print "Florida has: %s" % hashmap.get(cities, hashmap.get(states, "Florida")) # print every state abbreviation print '-' * 10 hashmap.list(states) #print every city in state print '-' * 10 hashmap.list(cities) print '-' * 10 state = hashmap.get(states, "Texas") if not state: print "Sorry, no Texas." # default values using ||= with the nil result # can you do this in one line? city = hashmap.get(cities, 'TX', 'Does Not Exist') print "The city for the state 'TX' is: %s" % city
hashmap.set(cities, 'NY', 'New York') hashmap.set(cities, 'OR', 'Portland') # print out some cities print '-' * 10 print 'NY State has: %s' % hashmap.get(cities, 'NY') print 'OR State has: %s' % hashmap.get(cities, 'OR') # print some states print '-' * 10 print "Michigan's abbreviation is: %s" % hashmap.get(states, 'Michigan') print "Florida's abbreviation is: %s" % hashmap.get(states, 'Florida') # do it by using the state then cities dict print '-' * 10 print "Michigan has: %s" % hashmap.get(cities, hashmap.get(states, 'Michigan')) print "Florida has: %s" % hashmap.get(cities, hashmap.get(states, 'Florida')) #print every state abbreviation print '-' * 10 hashmap.list(cities) print '-' * 10 state = hashmap.get(states, 'Texas') if not state: print "Sorry, no Texas" city = hashmap.get(cities, 'TX', 'Does not exist') print "The city for the state 'TX' is: %s" % city
hashmap.set(cities, 'MI', 'Detroit') hashmap.set(cities, 'FL', 'Jacksonville') hashmap.set(cities, 'NY', 'New York') hashmap.set(cities, 'OR', 'Portland') print "1", '-' * 10 print "NY State has:", hashmap.get(cities, 'NY') print "OR State has:", hashmap.get(cities, 'OR') print "2", '-' * 10 print "Michigan's abbreviation is:", hashmap.get(states, 'Michigan') print "Florida's abbreviation is:", hashmap.get(states, 'Florida') print "3", '-' * 10 print "Michigan has:", hashmap.get(cities, hashmap.get(states, 'Michigan')) print "Florida has:", hashmap.get(cities, hashmap.get(states, 'Florida')) print "4", '-' * 10 print hashmap.list(states) print "5", '-' * 10 print hashmap.list(cities) print "6", '-' * 10 state = hashmap.get(states, 'Texas') if not state: print "Sorry,no Texas" city = hashmap.get(cities, 'TX', "Does Not Exit") print "The city for the state 'TX' is:%s" % city
print '-' * 10 print "NY State has: %s" % hashmap.get(cities, 'NY') print "OR State has: %s" % hashmap.get(cities, 'OR') print "广东省有%s市" % hashmap.get(cities, 'GD') print '-' * 10 print "Michigan's abbreviation is: %s" % hashmap.get(states, 'Michigan') print "Florida's abbreviation is: %s" % hashmap.get(states, 'Florida') print '-' * 10 print "Michigan has: %s" % hashmap.get(cities, hashmap.get(states, 'Michigan')) print "Florida has: %s" % hashmap.get(cities, hashmap.get(states, 'Florida')) print '-' * 10 hashmap.list(states) print '-' * 10 hashmap.list(cities) print '-' * 10 state = hashmap.get(states, 'Texas') if not state: print "Sorry, no Texas." city = hashmap.get(cities, 'TX', 'Does Not Exist') print "The city for the state 'TX' is: %s" % city back_cities = hashmap.dump(cities) hashmap.delete(cities, "NY")
import hashmap sample = hashmap.new() print "empty dict: " print sample hashmap.set(sample, "key1", "value1") hashmap.set(sample, "key2", "value2") hashmap.set(sample, "key3", "value3") hashmap.set(sample, "apples", "bananas") print "hash_key: " print hashmap.hash_key(sample, "key1") print hashmap.hash_key(sample, "key2") print hashmap.hash_key(sample, "key3") print hashmap.hash_key(sample, "apples") print "get_bucket: " print hashmap.get_bucket(sample, "key1") print "get_slot: " print hashmap.get_slot(sample, "key1") print hashmap.get_slot(sample, "key2") print hashmap.get_slot(sample, "key3") print "dictionary again: " print sample print "list: " print hashmap.list(sample)
print "NY State has: %s" % hashmap.get(cities, 'NY') print "OR State has: %s" % hashmap.get(cities, 'OR') #print some states print '-' * 10 print "Michigan's abbreviation is: %s" % hashmap.get(states, 'Michigan') print "Florida's abbreviation is: %s" % hashmap.get(states, 'Florida') #do it by using the stats then cities dict print '-' * 10 print "Michigan has: %s" % hashmap.get(cities, hashmap.get(states, 'Michigan')) print "Florida has: %s" % hashmap.get(cities, hashmap.get(states, 'Florida')) #print every state abbreviation print '-' * 10 hashmap.list(states) #printe every city in state print '-' * 10 hashmap.list(cities) print '-' * 10 state = hashmap.get(states, 'Texas') if not state: print "Sorry, no Texas." # default values using ||= with the nil result # can you do this on one line? city = hashmap.get(cities, 'TX', 'Does not exist') print "The city for the state 'TX' is: %s" % city
# Extra exercise. # Date: 2014-16-25 import hashmap # The tests that it will work. jazz = hashmap.new() hashmap.set(jazz, 'Miles Davis', 'Flamenco Sketches') # confirms set will replace previous one hashmap.set(jazz, 'Miles Davis', 'Kind Of Blue') hashmap.set(jazz, 'Duke Ellington', 'Beginning To See The Light') hashmap.set(jazz, 'Billy Strayhorn', 'Lush Life') print "---- List Test ----" hashmap.list(jazz) print "---- Get Test ----" print hashmap.get(jazz, 'Miles Davis') print hashmap.get(jazz, 'Duke Ellington') print hashmap.get(jazz, 'Billy Strayhorn') print "---- Delete Test ----" print "** Goodbye Miles" hashmap.delete(jazz, "Miles Davis") hashmap.list(jazz) print "** Goodbye Duke" hashmap.delete(jazz, "Duke Ellington") hashmap.list(jazz)
print "Tieflings abbreviation is: %s" % hashmap.get(races, 'Tiefling') print "Gnomes abbreviation is: %s" % hashmap.get(races, 'Gnome') # do it by using the state then cities dict print '-' * 10 # implementing assertions over prints assert (hashmap.get(traits,hashmap.get(races,'Tiefling'))), 'Tieflings have no traits' assert (hashmap.get(traits,hashmap.get(races,'Gnome'))), 'Gnomes have no traits' # prints of dict contents #print "Tieflings have: %s " % hashmap.get(traits, hashmap.get(races, 'Tiefling')) #print "Gnomes have: %s " % hashmap.get(traits, hashmap.get(races, 'Gnome')) # print every state abbreviation print '-' * 10 hashmap.list(races) # print every city in state print '-' * 10 hashmap.list(traits) print '-' * 10 # asserting race #assert hashmap.get(races,'Kobold'), 'There are no Kobold races' #race = hashmap.get(races, 'Kobold') #if not race: # print "Sorry, no Kobolds." # default values using || = with the nil result # can you do this on one line?
import hashmap provinces = hashmap.new() # Dutch provinces; key is province name, value is its largest city hashmap.set(provinces, 'North Holland', 'Amsterdam') hashmap.set(provinces, 'South Holland', 'Rotterdam') hashmap.set(provinces, 'Utrecht', 'Utrecht') hashmap.set(provinces, 'Zeeland', 'Middelburg') hashmap.set(provinces, 'Groningen', 'Groningen') hashmap.set(provinces, 'Limburg', 'Maastricht') hashmap.set(provinces, 'Flevoland', 'Almere') hashmap.set(provinces, 'North Brabant', 'Den Bosch') hashmap.set(provinces, 'Friesland', 'Leeuwarden') hashmap.set(provinces, 'Drenthe', 'Assen') hashmap.set(provinces, 'Gelderland', 'Nijmegen') hashmap.set(provinces, 'Overijssel', 'Enschede') hashmap.list(provinces) print '-' * 10 print hashmap.get(provinces, 'Drenthe')
import hashmap jazz = hashmap.new() hashmap.set(jazz, "Miles Davis", "Flamenco Sketches") hashmap.set(jazz, "Miles Davis", "Kind of Blue") hashmap.set(jazz, "Duke Ellington", "Beginning To See The Light") hashmap.set(jazz, "Billy Strayhorn", "Lush Life") print "----------list test-----------" print hashmap.list(jazz) print "----------get test-------------" print hashmap.get(jazz, "Miles Davis") print hashmap.get(jazz, "Duke Ellington") print hashmap.get(jazz, "Billy Strayhorn") print "--------delete test--------" print "**goodbye Miles" hashmap.delete(jazz, "Miles Davis") hashmap.list(jazz) print "** goodbye Duke" hashmap.delete(jazz, "Duke Ellington") hashmap.list(jazz) print "** goodbye Billy" hashmap.delete(jazz, "Billy Strayhorn") hashmap.list(jazz) print "** goodbye Pork Pie Hat"
import hashmap country = hashmap.new() hashmap.set(country, 'VietNam', 'VN') hashmap.set(country, 'ThaiLan', 'TL') hashmap.set(country, 'Malaysia', 'ML') city = hashmap.new() hashmap.set(country,'VN', 'HaNoi') hashmap.set(country, 'TL', 'BangKok') hashmap.set(country, 'ML', 'Kualalumpur') print "Viet Nam duoc viet gon la: ", hashmap.get(country, 'VietNam') print "Thu do cua Viet Nam la: ", hashmap.get(city, 'VN') print "Danh sach: " hashmap.list(country) hashmap.list(city)
print '-' * 10 print "Wolves make %s sounds" % hashmap.get(sounds, 'Canine') print "Humans make %s sounds" % hashmap.get(sounds, 'Sapien') # print some genii print '-' * 10 print "Dog is of genus %s" % hashmap.get(animals, 'Dog') print "Octopus is of genus %s" % hashmap.get(animals, 'Octopus') # do it by using the animals then sounds dict print '-' * 10 print "Jellyfish makes %s sounds" % hashmap.get(sounds, hashmap.get(animals, 'Octopus')) print "Cats make %s sounds" % hashmap.get(sounds, hashmap.get(animals, 'Cat')) # print every animal genus print '-' * 10 hashmap.list(animals) # print every sound by genus print '-' * 10 hashmap.list(sounds) print '-' * 10 animals = hashmap.get(animals, 'Unicorn') if not animals: print "Sorry, no Unicorns." # default values using ||= with the nil result sounds = hashmap.get(sounds, 'Sound of rainbows', 'Imaginary') print "The sound for the genus 'Equine, Magical' is: %s" % sounds
import hashmap aMap = hashmap.new() hashmap.set(aMap,10,"ten") hashmap.set(aMap,20,"twenty") print hashmap.get(aMap,10) print "Old hashmap:" hashmap.list(aMap) hashmap.delete(aMap,10) hashmap.set(aMap,20,"2*10") print "New hashmap:" hashmap.list(aMap)
print "NY State has: %s" % hashmap.get(cities, 'NY') print "OR State has: %s" % hashmap.get(cities, 'OR') # print some states print '-' * 10 print "Michigan's abbreviation is: %s" % hashmap.get(states, 'Michigan') print "Florida's abbreviation is: %s" % hashmap.get(states, 'Florida') # print cities using state then cities dict print '-' * 10 print "Michigan has: %s" % hashmap.get(cities, hashmap.get(states, 'Michigan')) print "Florida has: %s" % hashmap.get(cities, hashmap.get(states, 'Florida')) # print every state abbreviation print '-' * 10 hashmap.list(states) # print every city in state print '-' * 10 hashmap.list(cities) print '-' * 10 state = hashmap.get(states, 'Texas') if not state: print "Sorry, no Texas" # default values using ||= with the nil result # can you do this on one line? city = hashmap.get(cities, 'TX', 'Does not exist')
# create a basic set of states and some cities in them cities = hashmap.new() hashmap.set(cities, 'CA', 'San Francisco') hashmap.set(cities, 'MI', 'Detroit') hashmap.set(cities, 'FL', 'jacksonville') # add some more cities hashmap.set(cities, 'NY', 'New York') hashmap.set(cities, 'OR', 'Portland') print '*' * 20 toMap = [] hashmap.dump(cities, toMap) print toMap hashmap.set(toMap, 'CA', '*****************') hashmap.list(toMap) print '*' * 20 # print out some cities print '-' * 10 print "NY State has: %s" % hashmap.get(cities, 'NY') print 'OR State has: %s' % hashmap.get(cities, 'OR') # print some states print '-' * 10 print "Michigan's abbreviation is: %s" % hashmap.get(states, 'Michigan') print "Florida's abbreviation is: %s" % hashmap.get(states, 'Florida') # do it by using the state then cities dict print '-' * 10 print "Michigan has: %s" % hashmap.get(cities, hashmap.get(states, 'Michigan'))
print "CB has: %s" % hashmap.get(cities, 'CB') print "LZ has: %s" % hashmap.get(cities, 'LZ') # print some regions print '-' * 10 print "Calabria's abbreviation is: %s" % hashmap.get(regions, 'Calabria') print "Tuscany's abbreviation is: %s" % hashmap.get(regions, 'Tuscany') # do it by using the state the cities dict print '-' * 10 print "Lazio has: %s" % hashmap.get(cities, hashmap.get(regions, 'Lazio')) print "Lombardi has: %s" % hashmap.get(cities, hashmap.get(regions, 'Lombardi')) # print every state abbreviation print '-' * 10 hashmap.list(regions) # print every city in state print '-' * 10 hashmap.list(cities) print '-' * 10 state = hashmap.get(regions, 'Sicily') if not state: print "Sorry, no Sicily." # default values using ||= with the nil result # can you do this on one line? city = hashmap.get(cities, 'SC', 'Does Not Exist') print "The city for the region 'SC' is: %s" % city
hashmap.set(cities, 'CA', 'San Francisco') hashmap.set(cities, 'MI', 'Detroit') hashmap.set(cities, 'FL', 'Jacksonville') # add some more cities hashmap.set(cities, 'NY', 'New York') hashmap.set(cities, 'OR', 'Portland') # print out some cities print '-' * 10 print "NY State has: %s" % hashmap.get(cities, 'NY') print "OR State has: %s" % hashmap.get(cities, 'OR') print '-' * 10 print hashmap.dump ''' # print some states print '-' * 10 print "Michigan's abbreviation is: %s" % hashmap.get(states, 'Michigan') print "Florida's abbreviation is: %s" % hashmap.get(states, 'Florida') # do it by using the state then cities dict print '-' * 10 print "Michigan has: %s" % hashmap.get(cities, hashmap.get(states, 'Michigan')) print "Florida has: %s" % hashmap.get(cities, hashmap.get(states, 'Florida')) # print every state abbreviation print '-' * 10 hashmap.list(states) # print every city in state
hashmap.set(cities, 'MI', 'Detroit') hashmap.set(cities, 'FL', 'Jacksonville') hashmap.set(cities, 'NY', 'New York') hashmap.set(cities, 'OR', 'Portland') print "=" * 10 print "NY State has: %s" % hashmap.get(cities, 'NY') print "OR State has: %s" % hashmap.get(cities, 'OR') print "=" * 10 print "Michigan is: %s" % hashmap.get(states, 'Michigan') print "Florida is: %s" % hashmap.get(states, 'Florida') print "=" * 10 print "Michigan has: %s" % hashmap.get(cities, hashmap.get(states, 'Michigan')) print "Florida has: %s" % hashmap.get(cities, hashmap.get(states, 'Florida')) print "=" * 10 hashmap.list(cities) print "=" * 10 state = hashmap.get(states, "Texas") if not state: print "Sorry, no Texas." # default values useing ||= with the nil result # can you do this on one lin? city = hashmap.get(cities, "TX", "Does Not Exist") print "The City for the stete 'TX' is: %s" % city
hashmap.set(states, 'Michigan', 'MI') # create a basic set of states and some cities in them cities = hashmap.new() hashmap.set(cities, 'CA', 'San Francisco') hashmap.set(cities, 'MI', 'Detroit') hashmap.set(cities, 'FL', 'Jacksonville') # add some more cities hashmap.set(cities, 'NY', 'New York') hashmap.set(cities, 'OR', 'Portland') # print out some cities print '-' * 10 print "NY State has: %s" % hashmap.get(cities, 'NY') print "OR State has: %s" % hashmap.get(cities, 'OR') # print some states print '-' * 10 print "Michigan's abbreviation is: %s" % hashmap.get(states, 'Michigan') print "Florida's abbreviation is: %s" % hashmap.get(states, 'Florida') # do it by using the state then cities dict print '-' * 10 print "Michigan has: %s" % hashmap.get(cities, hashmap.get(states, 'Michigan')) print "Florida has: %s" % hashmap.get(cities, hashmap.get(states, 'Florida')) # print every state abbreviation print '-' * 10 hashmap.list(states)
print '-' * 10 print "NY State has: %s" % hashmap.get(cities, 'NY') print "OR State has: %s" % hashmap.get(cities, 'OR') # print some states print '-' * 10 print "Michigan's abbreviation is: %s" % hashmap.get(states, 'Michigan') print "Florida's abbreviation is: %s" % hashmap.get(states, 'Florida') # do it by using the state then cities dict print '-' * 10 print "Michigan has: %s" % hashmap.get(cities, hashmap.get(states, 'Michigan')) print "Florida has: %s" % hashmap.get(cities, hashmap.get(states, 'Florida')) # print every state abbreviation print '-' * 10 hashmap.list(states) # print every city in state print '-' * 10 state = hashmap.get(states, 'Texas') if not state: print "Sorry, no Texas." # default values using ||= with teh nil result # can you do this on one line? city = hashmap.get(cities, 'TX', 'Does Not Exist') print "The city for the state 'TX' is: %s" % city # on one line # print "The city for the state 'TX' is: ", hashmap.get(cities, 'TX', 'Does Not Exist')
hashmap.set(citiesList, 'FL', 'Jacksonville') #add some more cities hashmap.set(citiesList, 'NY', 'New York') hashmap.set(citiesList, 'OR', 'Portland') # print some states print '-' * 10 print "Michigan has: %s" % hashmap.get(citiesList, 'MI') print "Florida has: %s" % hashmap.get(citiesList, 'FL') # do it by using the state then cities dict print '-' * 10 print "Michigan has: %s" % hashmap.get(citiesList, hashmap.get(statesList, 'Michigan')) print "Florida has: %s" % hashmap.get(citiesList, hashmap.get(statesList, 'Florida')) # print every abbreviation print '-' * 10 hashmap.list(statesList) # print every city in state print '-' * 10 hashmap.list(citiesList) print '-' * 10 state = hashmap.get(statesList, 'Texas') if (not state): print "Sorry no Texas" print "The city for the state 'TX' is: %s" % hashmap.get(citiesList, 'TX', 'Does Not Exist')
#print some states print '_' * 10 print "Michigan's abbreviation is: %s" % hashmap.get(states, 'Michigan') print "Florida's abbreviation is: %s" % hashmap.get(states, 'Florida') # do it by using the state then cities dict print '_' * 10 print "Michigan has: %s" % hashmap.get(cities, hashmap.get(states, 'Michigan')) print "Florida has: %s" % hashmap.get(cities, hashmap.get(states, 'Florida')) # print every state abbreviation print '_' * 10 hashmap.list(states) # print every city in state print '_' * 10 hashmap.list(cities) print '_' * 10 state = hashmap.get(states, 'Texas') if not state: print "Sorry, no Texas." # default values using //= with the nil result # can you do this on one line? city = hashmap.get(cities, 'TX', 'Does Not Exist') print "The city for the state 'Tx' is: %s" % city
hashmap.set(cities, 'MI', 'Detroit') hashmap.set(cities, 'FL', 'Jacksonville') # add some more cities hashmap.set(cities, 'NY', 'New York') hashmap.set(cities, 'OR', 'Portland') # print out some cities print '-' * 10 print "NY State has: %s" % hashmap.get(cities, 'NY') print "OR State has: %s" % hashmap.get(cities, 'OR') print '-'*10 print hashmap.dump ''' # print some states print '-' * 10 print "Michigan's abbreviation is: %s" % hashmap.get(states, 'Michigan') print "Florida's abbreviation is: %s" % hashmap.get(states, 'Florida') # do it by using the state then cities dict print '-' * 10 print "Michigan has: %s" % hashmap.get(cities, hashmap.get(states, 'Michigan')) print "Florida has: %s" % hashmap.get(cities, hashmap.get(states, 'Florida')) # print every state abbreviation print '-' * 10 hashmap.list(states) # print every city in state