1
+ us_state_abbrev = {'Alabama' : 'AL' , 'Alaska' : 'AK' , 'Arizona' : 'AZ' ,
2
+ 'Arkansas' : 'AR' , 'California' : 'CA' , 'Colorado' : 'CO' ,
3
+ 'Connecticut' : 'CT' , 'Delaware' : 'DE' , 'Florida' : 'FL' ,
4
+ 'Georgia' : 'GA' , 'Hawaii' : 'HI' , 'Idaho' : 'ID' ,
5
+ 'Illinois' : 'IL' , 'Indiana' : 'IN' , 'Iowa' : 'IA' ,
6
+ 'Kansas' : 'KS' , 'Kentucky' : 'KY' , 'Louisiana' : 'LA' ,
7
+ 'Maine' : 'ME' , 'Maryland' : 'MD' , 'Massachusetts' : 'MA' ,
8
+ 'Michigan' : 'MI' , 'Minnesota' : 'MN' , 'Mississippi' : 'MS' ,
9
+ 'Missouri' : 'MO' , 'Montana' : 'MT' , 'Nebraska' : 'NE' ,
10
+ 'Nevada' : 'NV' , 'New Hampshire' : 'NH' , 'New Jersey' : 'NJ' ,
11
+ 'New Mexico' : 'NM' , 'New York' : 'NY' ,
12
+ 'North Carolina' : 'NC' , 'North Dakota' : 'ND' ,
13
+ 'Ohio' : 'OH' , 'Oklahoma' : 'OK' , 'Oregon' : 'OR' ,
14
+ 'Pennsylvania' : 'PA' , 'Rhode Island' : 'RI' ,
15
+ 'South Carolina' : 'SC' , 'South Dakota' : 'SD' ,
16
+ 'Tennessee' : 'TN' , 'Texas' : 'TX' , 'Utah' : 'UT' ,
17
+ 'Vermont' : 'VT' , 'Virginia' : 'VA' , 'Washington' : 'WA' ,
18
+ 'West Virginia' : 'WV' , 'Wisconsin' : 'WI' , 'Wyoming' : 'WY' }
19
+
20
+ states = ['Oklahoma' , 'Kansas' , 'North Carolina' , 'Georgia' , 'Oregon' ,
21
+ 'Mississippi' , 'Minnesota' , 'Colorado' , 'Alabama' ,
22
+ 'Massachusetts' , 'Arizona' , 'Connecticut' , 'Montana' ,
23
+ 'West Virginia' , 'Nebraska' , 'New York' , 'Nevada' , 'Idaho' ,
24
+ 'New Jersey' , 'Missouri' , 'South Carolina' , 'Pennsylvania' ,
25
+ 'Rhode Island' , 'New Mexico' , 'Alaska' , 'New Hampshire' ,
26
+ 'Tennessee' , 'Washington' , 'Indiana' , 'Hawaii' , 'Kentucky' ,
27
+ 'Virginia' , 'Ohio' , 'Wisconsin' , 'Maryland' , 'Florida' ,
28
+ 'Utah' , 'Maine' , 'California' , 'Vermont' , 'Arkansas' , 'Wyoming' ,
29
+ 'Louisiana' , 'North Dakota' , 'South Dakota' , 'Texas' ,
30
+ 'Illinois' , 'Iowa' , 'Michigan' , 'Delaware' ]
31
+
32
+ NOT_FOUND = 'N/A'
33
+
34
+
35
+ def get_every_nth_state (states = states , n = 10 ):
36
+ """Return a list with every nth item (default argument n=10, so every
37
+ 10th item) of the states list above (remember: lists keep order)"""
38
+ return states [n - 1 ::n ]
39
+
40
+
41
+ def get_state_abbrev (state_name , us_state_abbrev = us_state_abbrev ):
42
+ """Look up a state abbreviation by querying the us_state_abbrev
43
+ dict by full state name, for instance 'Alabama' returns 'AL',
44
+ 'Illinois' returns 'IL'.
45
+ If the state is not in the dict, return 'N/A' which we stored
46
+ in the NOT_FOUND constant (takeaway: dicts are great for lookups)"""
47
+ try :
48
+ result = us_state_abbrev [state_name ]
49
+ except KeyError :
50
+ result = NOT_FOUND
51
+ return result
52
+
53
+
54
+ def get_longest_state (data ):
55
+ """Receives data, which can be the us_state_abbrev dict or the states
56
+ list (see above). It returns the longest state measured by the length
57
+ of the string"""
58
+ return max (data , key = len )
59
+
60
+
61
+
62
+ def combine_state_names_and_abbreviations (us_state_abbrev = us_state_abbrev ,
63
+ states = states ):
64
+ """Get the first 10 state abbreviations ('AL', 'AK', 'AZ', ...) from
65
+ the us_state_abbrev dict, and the last 10 states from the states
66
+ list (see above) and combine them into a new list. The resulting list
67
+ has both sorted, so:
68
+ ['AK', 'AL', 'AZ', ..., 'South Dakota', 'Tennessee', 'Texas', ...]
69
+ (see also test_combine_state_names_and_abbreviations)"""
70
+ result = []
71
+ result = list (us_state_abbrev .values ())[:10 ] + list (us_state_abbrev .keys ())[- 10 :]
72
+ return sorted (result )
73
+
74
+ print (get_every_nth_state ())
0 commit comments