Skip to content

Commit f4d19f4

Browse files
committed
Data Structures states.py
1 parent 39a632a commit f4d19f4

File tree

2 files changed

+165
-0
lines changed

2 files changed

+165
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
# noinspection DuplicatedCode
21+
states = ['Oklahoma', 'Kansas', 'North Carolina', 'Georgia', 'Oregon',
22+
'Mississippi', 'Minnesota', 'Colorado', 'Alabama',
23+
'Massachusetts', 'Arizona', 'Connecticut', 'Montana',
24+
'West Virginia', 'Nebraska', 'New York', 'Nevada', 'Idaho',
25+
'New Jersey', 'Missouri', 'South Carolina', 'Pennsylvania',
26+
'Rhode Island', 'New Mexico', 'Alaska', 'New Hampshire',
27+
'Tennessee', 'Washington', 'Indiana', 'Hawaii', 'Kentucky',
28+
'Virginia', 'Ohio', 'Wisconsin', 'Maryland', 'Florida',
29+
'Utah', 'Maine', 'California', 'Vermont', 'Arkansas', 'Wyoming',
30+
'Louisiana', 'North Dakota', 'South Dakota', 'Texas',
31+
'Illinois', 'Iowa', 'Michigan', 'Delaware']
32+
33+
NOT_FOUND = 'N/A'
34+
35+
36+
def main():
37+
print(get_every_nth_state())
38+
print(get_every_nth_state(n=20))
39+
print(get_state_abbrev('Oklahoma'))
40+
print(get_state_abbrev('Washington'))
41+
print(get_state_abbrev('Whatever'))
42+
print(get_longest_state(us_state_abbrev))
43+
print(get_longest_state(states))
44+
print(combine_state_names_and_abbreviations())
45+
46+
47+
def get_every_nth_state(states=states, n=10):
48+
"""Return a list with every nth item (default argument n=10, so every
49+
10th item) of the states list above (remember: lists keep order)"""
50+
result = states[n - 1::n]
51+
return result
52+
53+
54+
def get_state_abbrev(state_name, us_state_abbrev=us_state_abbrev):
55+
"""Look up a state abbreviation by querying the us_state_abbrev
56+
dict by full state name, for instance 'Alabama' returns 'AL',
57+
'Illinois' returns 'IL'.
58+
If the state is not in the dict, return 'N/A' which we stored
59+
in the NOT_FOUND constant (takeaway: dicts are great for lookups)"""
60+
result = us_state_abbrev.get(state_name)
61+
if result is None:
62+
return NOT_FOUND
63+
64+
return result
65+
66+
67+
def get_longest_state(data):
68+
"""Receives data, which can be the us_state_abbrev dict or the states
69+
list (see above). It returns the longest state measured by the length
70+
of the string"""
71+
if type(data) == dict:
72+
state_dict = {len(state): state for state in data.keys()}
73+
longest_state = max(state_dict.keys())
74+
return state_dict[longest_state]
75+
elif type(data) == list:
76+
state_dict = {len(state): state for state in data}
77+
longest_state = max(state_dict.keys())
78+
return state_dict[longest_state]
79+
80+
81+
def combine_state_names_and_abbreviations(us_state_abbrev=us_state_abbrev,
82+
states=states):
83+
"""Get the first 10 state abbreviations from the (sorted) us_state_abbrev dict,
84+
and the last 10 states from the (sorted) states list (see also above).
85+
Combine these two lists into a new list and return it.
86+
Here is the truncated resulting list (see also the tests):
87+
['AK', 'AL', 'AZ', ..., 'West Virginia', 'Wisconsin', 'Wyoming']"""
88+
sorted_abbrvs = list(us_state_abbrev.values())
89+
sorted_abbrvs.sort()
90+
sorted_abbrvs = sorted_abbrvs[:10]
91+
92+
states.sort()
93+
sorted_states = states[-10:]
94+
sorted_abbrvs.extend(sorted_states)
95+
return sorted_abbrvs
96+
97+
98+
if __name__ == "__main__":
99+
main()
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
for i, state in enumerate(states, 1):
39+
if i % n == 0:
40+
yield state
41+
42+
43+
def get_state_abbrev(state_name, us_state_abbrev=us_state_abbrev):
44+
"""Look up a state abbreviation by querying the us_state_abbrev
45+
dict by full state name, for instance 'Alabama' returns 'AL',
46+
'Illinois' returns 'IL'.
47+
If the state is not in the dict, return 'N/A' which we stored
48+
in the NOT_FOUND constant (takeaway: dicts are great for lookups)"""
49+
return us_state_abbrev.get(state_name) or NOT_FOUND
50+
51+
52+
def get_longest_state(data):
53+
"""Receives data, which can be the us_state_abbrev dict or the states
54+
list (see above). It returns the longest state measured by the length
55+
of the string"""
56+
return max(data, key=len)
57+
58+
59+
def combine_state_names_and_abbreviations(us_state_abbrev=us_state_abbrev,
60+
states=states):
61+
"""Get the first 10 state abbreviations from the (sorted) us_state_abbrev dict,
62+
and the last 10 states from the (sorted) states list (see also above).
63+
Combine these two lists into a new list and return it.
64+
Here is the truncated resulting list (see also the tests):
65+
['AK', 'AL', 'AZ', ..., 'West Virginia', 'Wisconsin', 'Wyoming']"""
66+
return sorted(us_state_abbrev.values())[:10] + sorted(states)[-10:]

0 commit comments

Comments
 (0)