print '-' * 10, "Reverse engineer part1", '-' * 10 aMap_bucket = [] aMap_bucket2 = [] aMap = [aMap_bucket] key = 'key1' value = 'value1' print '->' * 5, "def new(num_buckets)", '<-' * 5 print "getting new()" print "aMap =", aMap print '=' *50 print '->' * 5, "def hash_key(aMap, key)", '<-' * 5 print "getting hash_key(aMap, key)" hash_key = hashmap.hash_key(aMap, key) print "hash('key1') = ", hash(key) print "len(aMap) = ", len(aMap) print "returns hash_key =", hash_key print '=' *50 print '->' * 5, "def get_bucket(aMap, key)", '<-' * 5 print "getting get_bucket(aMap, key)" get_bucket = hashmap.get_bucket(aMap, key) bucket_id = hash_key aMap[bucket_id] print "get_bucket = ", get_bucket print "bucket_id = ", bucket_id print "returns aMap[bucket_id] =", aMap[bucket_id] print '=' *50
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)
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', 'Some Town') hashmap.set(cities, 'OR', 'Portland') # Print out some cities print '_' * 60 print "Let me play with hash key, print it out for cities:", hashmap.hash_key(cities, 'CA') print '_' * 60 print "This is 'cities' hashmap object: ", cities print "This is Bucket", cities[hashmap.hash_key(states, 'CA')] print "This is we get slot:", hashmap.get_slot(cities, 'MI') print print '_' * 40 print "NY State has: %s" % hashmap.get(cities, 'NY') print "OR State has: %s" % hashmap.get(cities, 'OR') # Print out some states print '_' * 40 print "Michigan's abbreviation is: %s" % hashmap.get(states, 'Michigan') print "Florida's abbreviation is %s" % hashmap.get(states, 'Florida')
import hashmap if __name__ == "__main__": amap = hashmap.new() print amap num = hashmap.hash_key(amap, 3) print num value = hashmap.get_bucket(amap, 3) print value print hashmap.get_slot(amap, 3)