|
| 1 | +us_state_abbrev = { |
| 2 | + "Alabama": "AL", |
| 3 | + "Alaska": "AK", |
| 4 | + "Arizona": "AZ", |
| 5 | + "Arkansas": "AR", |
| 6 | + "California": "CA", |
| 7 | + "Colorado": "CO", |
| 8 | + "Connecticut": "CT", |
| 9 | + "Delaware": "DE", |
| 10 | + "Florida": "FL", |
| 11 | + "Georgia": "GA", |
| 12 | + "Hawaii": "HI", |
| 13 | + "Idaho": "ID", |
| 14 | + "Illinois": "IL", |
| 15 | + "Indiana": "IN", |
| 16 | + "Iowa": "IA", |
| 17 | + "Kansas": "KS", |
| 18 | + "Kentucky": "KY", |
| 19 | + "Louisiana": "LA", |
| 20 | + "Maine": "ME", |
| 21 | + "Maryland": "MD", |
| 22 | + "Massachusetts": "MA", |
| 23 | + "Michigan": "MI", |
| 24 | + "Minnesota": "MN", |
| 25 | + "Mississippi": "MS", |
| 26 | + "Missouri": "MO", |
| 27 | + "Montana": "MT", |
| 28 | + "Nebraska": "NE", |
| 29 | + "Nevada": "NV", |
| 30 | + "New Hampshire": "NH", |
| 31 | + "New Jersey": "NJ", |
| 32 | + "New Mexico": "NM", |
| 33 | + "New York": "NY", |
| 34 | + "North Carolina": "NC", |
| 35 | + "North Dakota": "ND", |
| 36 | + "Ohio": "OH", |
| 37 | + "Oklahoma": "OK", |
| 38 | + "Oregon": "OR", |
| 39 | + "Pennsylvania": "PA", |
| 40 | + "Rhode Island": "RI", |
| 41 | + "South Carolina": "SC", |
| 42 | + "South Dakota": "SD", |
| 43 | + "Tennessee": "TN", |
| 44 | + "Texas": "TX", |
| 45 | + "Utah": "UT", |
| 46 | + "Vermont": "VT", |
| 47 | + "Virginia": "VA", |
| 48 | + "Washington": "WA", |
| 49 | + "West Virginia": "WV", |
| 50 | + "Wisconsin": "WI", |
| 51 | + "Wyoming": "WY", |
| 52 | +} |
| 53 | + |
| 54 | +states = [ |
| 55 | + "Oklahoma", # 0 |
| 56 | + "Kansas", |
| 57 | + "North Carolina", |
| 58 | + "Georgia", |
| 59 | + "Oregon", |
| 60 | + "Mississippi", |
| 61 | + "Minnesota", |
| 62 | + "Colorado", |
| 63 | + "Alabama", |
| 64 | + "Massachusetts", # 9 |
| 65 | + "Arizona", |
| 66 | + "Connecticut", |
| 67 | + "Montana", |
| 68 | + "West Virginia", |
| 69 | + "Nebraska", |
| 70 | + "New York", |
| 71 | + "Nevada", |
| 72 | + "Idaho", |
| 73 | + "New Jersey", |
| 74 | + "Missouri", # 19 |
| 75 | + "South Carolina", |
| 76 | + "Pennsylvania", |
| 77 | + "Rhode Island", |
| 78 | + "New Mexico", |
| 79 | + "Alaska", |
| 80 | + "New Hampshire", |
| 81 | + "Tennessee", |
| 82 | + "Washington", |
| 83 | + "Indiana", |
| 84 | + "Hawaii", # 29 |
| 85 | + "Kentucky", |
| 86 | + "Virginia", |
| 87 | + "Ohio", |
| 88 | + "Wisconsin", |
| 89 | + "Maryland", |
| 90 | + "Florida", |
| 91 | + "Utah", |
| 92 | + "Maine", |
| 93 | + "California", |
| 94 | + "Vermont", # 39 |
| 95 | + "Arkansas", |
| 96 | + "Wyoming", |
| 97 | + "Louisiana", |
| 98 | + "North Dakota", |
| 99 | + "South Dakota", |
| 100 | + "Texas", |
| 101 | + "Illinois", |
| 102 | + "Iowa", |
| 103 | + "Michigan", |
| 104 | + "Delaware", # 49 |
| 105 | +] |
| 106 | + |
| 107 | +NOT_FOUND = "N/A" |
| 108 | + |
| 109 | + |
| 110 | +def get_every_nth_state(states=states, n=10): |
| 111 | + """Return a list with every nth item (default argument n=10, so every |
| 112 | + 10th item) of the states list above (remember: lists keep order)""" |
| 113 | + |
| 114 | + nth_states = [ |
| 115 | + state for index, state in enumerate(states) if not (index + 1) % n and index > 0 |
| 116 | + ] |
| 117 | + return nth_states |
| 118 | + |
| 119 | + |
| 120 | +def get_state_abbrev(state_name, us_state_abbrev=us_state_abbrev): |
| 121 | + """Look up a state abbreviation by querying the us_state_abbrev |
| 122 | + dict by full state name, for instance 'Alabama' returns 'AL', |
| 123 | + 'Illinois' returns 'IL'. |
| 124 | + If the state is not in the dict, return 'N/A' which we stored |
| 125 | + in the NOT_FOUND constant (takeaway: dicts are great for lookups)""" |
| 126 | + |
| 127 | + if not state_name in us_state_abbrev: |
| 128 | + return NOT_FOUND |
| 129 | + |
| 130 | + return us_state_abbrev[state_name] |
| 131 | + |
| 132 | + |
| 133 | +def get_longest_state(data): |
| 134 | + """Receives data, which can be the us_state_abbrev dict or the states |
| 135 | + list (see above). It returns the longest state measured by the length |
| 136 | + of the string""" |
| 137 | + longest_state = "" |
| 138 | + |
| 139 | + for state in data: |
| 140 | + if len(state) > len(longest_state): |
| 141 | + longest_state = state |
| 142 | + |
| 143 | + return longest_state |
| 144 | + |
| 145 | + |
| 146 | +def combine_state_names_and_abbreviations( |
| 147 | + us_state_abbrev=us_state_abbrev, states=states |
| 148 | +): |
| 149 | + """Get the first 10 state abbreviations ('AL', 'AK', 'AZ', ...) from |
| 150 | + the us_state_abbrev dict, and the last 10 states from the states |
| 151 | + list (see above) and combine them into a new list. The resulting list |
| 152 | + has both sorted, so: |
| 153 | + ['AK', 'AL', 'AZ', ..., 'South Dakota', 'Tennessee', 'Texas', ...] |
| 154 | + (see also test_combine_state_names_and_abbreviations)""" |
| 155 | + |
| 156 | + abbrevs = list(us_state_abbrev.values()) |
| 157 | + abbrevs.sort() |
| 158 | + first_10_abbrev = abbrevs[:10] |
| 159 | + |
| 160 | + states.sort() |
| 161 | + last_10_states = states[-10:] |
| 162 | + |
| 163 | + return first_10_abbrev + last_10_states |
| 164 | + |
| 165 | + |
| 166 | +def main(): |
| 167 | + nth_states = get_every_nth_state() |
| 168 | + alabama_abbrev = get_state_abbrev("Alabama") |
| 169 | + illinois_abbrev = get_state_abbrev("Illinois") |
| 170 | + nf_avbrev = get_state_abbrev("Ada") |
| 171 | + state_1 = get_longest_state(us_state_abbrev) |
| 172 | + state_2 = get_longest_state(states) |
| 173 | + combination = combine_state_names_and_abbreviations() |
| 174 | + |
| 175 | +if __name__ == "__main__": |
| 176 | + main() |
0 commit comments