Skip to content

Commit 58c3831

Browse files
committed
Byte89 get_state_abbrev
1 parent 922c930 commit 58c3831

2 files changed

Lines changed: 36 additions & 9 deletions

File tree

days/07-09-data-structures/code/Byte89.ipynb

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@
4747
},
4848
{
4949
"cell_type": "code",
50-
"execution_count": 23,
50+
"execution_count": 25,
5151
"metadata": {},
5252
"outputs": [
5353
{
5454
"name": "stdout",
5555
"output_type": "stream",
5656
"text": [
57-
"30.5 µs ± 478 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n",
58-
"['Massachusetts', 'Missouri', 'Hawaii', 'Vermont', 'Delaware']\n"
57+
"30.4 µs ± 563 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n",
58+
"['Oregon', 'Massachusetts', 'Nebraska', 'Missouri', 'Alaska', 'Hawaii', 'Maryland', 'Vermont', 'South Dakota', 'Delaware']\n"
5959
]
6060
}
6161
],
@@ -72,7 +72,33 @@
7272
" return(listofnthstates) \n",
7373
"\n",
7474
"%timeit get_every_nth_state(10)\n",
75-
"print(get_every_nth_state(10))"
75+
"print(get_every_nth_state(5))"
76+
]
77+
},
78+
{
79+
"cell_type": "code",
80+
"execution_count": 26,
81+
"metadata": {},
82+
"outputs": [
83+
{
84+
"name": "stdout",
85+
"output_type": "stream",
86+
"text": [
87+
"295 ns ± 17.2 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n",
88+
"WI\n"
89+
]
90+
}
91+
],
92+
"source": [
93+
"def get_state_abbrev(abbrev):\n",
94+
" try:\n",
95+
" state_abbrev = us_state_abbrev[abbrev]\n",
96+
" return state_abbrev\n",
97+
" except KeyError:\n",
98+
" return NOT_FOUND\n",
99+
" \n",
100+
"%timeit get_state_abbrev('Wisconsin')\n",
101+
"print(get_state_abbrev('Wisconsin'))"
76102
]
77103
},
78104
{

days/07-09-data-structures/code/states.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ def get_every_nth_state(n=10):
4444
return(listofnthstates)
4545

4646

47-
4847
def get_state_abbrev(abbrev):
49-
"""Look up a state abbreviation by full name in
50-
us_state_abbrev, if not found return the string stored in the
51-
NOT_FOUND constant (takeaway: dicts are great for lookups)"""
52-
pass
48+
try:
49+
state_abbrev = us_state_abbrev[abbrev]
50+
return state_abbrev
51+
except KeyError:
52+
return NOT_FOUND
5353

5454

5555
def get_longest_state(data):
@@ -65,5 +65,6 @@ def combine_state_names_and_abbreviations():
6565
values and use sorted, list slicing and list concatenation)"""
6666
pass
6767

68+
6869
if __name__ == '__main__':
6970
print(get_every_nth_state(10))

0 commit comments

Comments
 (0)