Skip to content

Commit 1fc4706

Browse files
author
gsp
committed
1 parent e1b02c1 commit 1fc4706

File tree

1 file changed

+101
-0
lines changed
  • days/07-09-data-structures/code/bite89

1 file changed

+101
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
'''
2+
from https://codechalleng.es/bites/89/
3+
'''
4+
us_state_abbrev = {'Alabama': 'AL', 'Alaska': 'AK', 'Arizona': 'AZ',
5+
'Arkansas': 'AR', 'California': 'CA', 'Colorado': 'CO',
6+
'Connecticut': 'CT', 'Delaware': 'DE', 'Florida': 'FL',
7+
'Georgia': 'GA', 'Hawaii': 'HI', 'Idaho': 'ID',
8+
'Illinois': 'IL', 'Indiana': 'IN', 'Iowa': 'IA',
9+
'Kansas': 'KS', 'Kentucky': 'KY', 'Louisiana': 'LA',
10+
'Maine': 'ME', 'Maryland': 'MD', 'Massachusetts': 'MA',
11+
'Michigan': 'MI', 'Minnesota': 'MN', 'Mississippi': 'MS',
12+
'Missouri': 'MO', 'Montana': 'MT', 'Nebraska': 'NE',
13+
'Nevada': 'NV', 'New Hampshire': 'NH', 'New Jersey': 'NJ',
14+
'New Mexico': 'NM', 'New York': 'NY',
15+
'North Carolina': 'NC', 'North Dakota': 'ND',
16+
'Ohio': 'OH', 'Oklahoma': 'OK', 'Oregon': 'OR',
17+
'Pennsylvania': 'PA', 'Rhode Island': 'RI',
18+
'South Carolina': 'SC', 'South Dakota': 'SD',
19+
'Tennessee': 'TN', 'Texas': 'TX', 'Utah': 'UT',
20+
'Vermont': 'VT', 'Virginia': 'VA', 'Washington': 'WA',
21+
'West Virginia': 'WV', 'Wisconsin': 'WI', 'Wyoming': 'WY'}
22+
23+
states = ['Oklahoma', 'Kansas', 'North Carolina', 'Georgia', 'Oregon',
24+
'Mississippi', 'Minnesota', 'Colorado', 'Alabama',
25+
'Massachusetts', 'Arizona', 'Connecticut', 'Montana',
26+
'West Virginia', 'Nebraska', 'New York', 'Nevada', 'Idaho',
27+
'New Jersey', 'Missouri', 'South Carolina', 'Pennsylvania',
28+
'Rhode Island', 'New Mexico', 'Alaska', 'New Hampshire',
29+
'Tennessee', 'Washington', 'Indiana', 'Hawaii', 'Kentucky',
30+
'Virginia', 'Ohio', 'Wisconsin', 'Maryland', 'Florida',
31+
'Utah', 'Maine', 'California', 'Vermont', 'Arkansas', 'Wyoming',
32+
'Louisiana', 'North Dakota', 'South Dakota', 'Texas',
33+
'Illinois', 'Iowa', 'Michigan', 'Delaware']
34+
35+
NOT_FOUND = 'N/A'
36+
37+
38+
def get_every_nth_state(states=states, n=10):
39+
"""Return a list with every nth item (default argument n=10, so every
40+
10th item) of the states list above (remember: lists keep order)"""
41+
nth_state = []
42+
try:
43+
for i in range(n - 1, 50, n):
44+
nth_state.append(states[i])
45+
except:
46+
IndexError
47+
pass
48+
return nth_state
49+
50+
51+
def get_state_abbrev(state_name, us_state_abbrev=us_state_abbrev):
52+
"""Look up a state abbreviation by querying the us_state_abbrev
53+
dict by full state name, for instance 'Alabama' returns 'AL',
54+
'Illinois' returns 'IL'.
55+
If the state is not in the dict, return 'N/A' which we stored
56+
in the NOT_FOUND constant (takeaway: dicts are great for lookups)"""
57+
if us_state_abbrev.get(state_name):
58+
return us_state_abbrev.get(state_name)
59+
else:
60+
return NOT_FOUND
61+
62+
63+
def get_longest_state(data):
64+
"""Receives data, which can be the us_state_abbrev dict or the states
65+
list (see above). It returns the longest state measured by the length
66+
of the string"""
67+
longest_name = ''
68+
if isinstance(data, list):
69+
for state_name in data:
70+
current_state = state_name
71+
if len(current_state) > len(longest_name):
72+
longest_name = current_state
73+
return longest_name
74+
elif isinstance(data, dict):
75+
for state_name, state_abbrev in us_state_abbrev.items():
76+
current_state = state_name
77+
if len(current_state) > len(longest_name):
78+
longest_name = current_state
79+
return longest_name
80+
81+
82+
def combine_state_names_and_abbreviations(us_state_abbrev=us_state_abbrev,
83+
states=states):
84+
"""Get the first 10 state abbreviations ('AL', 'AK', 'AZ', ...) from
85+
the us_state_abbrev dict, and the last 10 states from the states
86+
list (see above) and combine them into a new list. The resulting list
87+
has both sorted, so:
88+
['AK', 'AL', 'AZ', ..., 'South Dakota', 'Tennessee', 'Texas', ...]
89+
(see also test_combine_state_names_and_abbreviations)"""
90+
91+
sorted_abbrev = sorted([state_abbrev for state_abbrev in us_state_abbrev.values()])[:10]
92+
sorted_states = sorted(states)[-10:]
93+
94+
result = sorted_abbrev.extend(sorted_states)
95+
return sorted_abbrev
96+
97+
98+
if __name__ == '__main__':
99+
# print(get_every_nth_state(states, n=10))
100+
# print(get_longest_state(us_state_abbrev))
101+
print(combine_state_names_and_abbreviations())

0 commit comments

Comments
 (0)