-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathusing_hashmap.py
70 lines (52 loc) · 1.97 KB
/
using_hashmap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/python
import hashmap
# 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 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 states
print '-' * 10
hashmap.list(cities)
print '-' * 10
# by default ruby says nil when something itsn't there
state = hashmap.get(states, 'Texas')
if not state:
print "Sorry, no Texas found"
# default values using ||= with the nil result set
# can you do this in one line challenge
city = hashmap.get(cities, 'TX', 'Does not exists')
print "The city for the state 'TX' is: %s" % city
# One line challenge answer
print
print "The city for the state 'TX' is: %s" % hashmap.get(cities, 'TX', 'Does not exists')
print "Collections.orderedDict are dictionaries where the elements are ordered like a lists."
print
print """Next exercise involved here is to make the values for keys
of the hashmap as a list, so it found the value will be appended to it,
rather than overwritten"""