Skip to content

Commit 4445032

Browse files
bites 89 - list and dict (Incomplete; Done: get_every_nth_state) - Add a bash script to unzip, remove zip file because I found myself need to type these commands everytime I download a new exercise.
1 parent 54c8a09 commit 4445032

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,4 @@
7373
/222/README.md
7474
/199/README.md
7575
/228/README.md
76+
/89/README.md

89/states.py

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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[pos] for pos in range(n - 1, len(states), 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+
pass
48+
49+
50+
def get_longest_state(data):
51+
"""Receives data, which can be the us_state_abbrev dict or the states
52+
list (see above). It returns the longest state measured by the length
53+
of the string"""
54+
pass
55+
56+
57+
def combine_state_names_and_abbreviations(us_state_abbrev=us_state_abbrev,
58+
states=states):
59+
"""Get the first 10 state abbreviations ('AL', 'AK', 'AZ', ...) from
60+
the us_state_abbrev dict, and the last 10 states from the states
61+
list (see above) and combine them into a new list. The resulting list
62+
has both sorted, so:
63+
['AK', 'AL', 'AZ', ..., 'South Dakota', 'Tennessee', 'Texas', ...]
64+
(see also test_combine_state_names_and_abbreviations)"""
65+
pass

89/test_states.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from states import (get_every_nth_state, get_state_abbrev,
2+
get_longest_state, combine_state_names_and_abbreviations,
3+
states, us_state_abbrev, NOT_FOUND)
4+
5+
6+
def test_get_every_nth_state():
7+
expected = ['Massachusetts', 'Missouri', 'Hawaii',
8+
'Vermont', 'Delaware']
9+
assert list(get_every_nth_state()) == expected
10+
expected = ['Missouri', 'Vermont']
11+
assert list(get_every_nth_state(n=20)) == expected
12+
13+
14+
def test_get_state_abbrev():
15+
assert get_state_abbrev('Illinois') == 'IL'
16+
assert get_state_abbrev('North Dakota') == 'ND'
17+
assert get_state_abbrev('bogus') == NOT_FOUND
18+
19+
20+
def test_get_longest_state():
21+
# depending the direction of the sort (reversed or not)
22+
# both North and South Carolina are correct
23+
correct_answers = ('North Carolina', 'South Carolina')
24+
assert get_longest_state(us_state_abbrev) in correct_answers
25+
assert get_longest_state(states) in correct_answers
26+
27+
28+
def test_combine_state_names_and_abbreviations():
29+
expected = ['AK', 'AL', 'AR', 'AZ', 'CA', 'CO', 'CT', 'DE', 'FL', 'GA',
30+
'South Dakota', 'Tennessee', 'Texas', 'Utah',
31+
'Vermont', 'Virginia', 'Washington', 'West Virginia',
32+
'Wisconsin', 'Wyoming']
33+
assert combine_state_names_and_abbreviations() == expected

import.sh

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env bash
2+
FILE1=$1
3+
unzip ${FILE1}".zip" -d ${FILE1}
4+
rm ${FILE1}".zip"

0 commit comments

Comments
 (0)