Skip to content

Commit f7e32e5

Browse files
committed
tests!
1 parent 1df20f0 commit f7e32e5

File tree

6 files changed

+126
-22
lines changed

6 files changed

+126
-22
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
*.pytest_cache/
2+
13
# Byte-compiled / optimized / DLL files
24
__pycache__/
35
*.py[cod]

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

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 48,
6-
"metadata": {},
7-
"outputs": [
8-
{
9-
"name": "stdout",
10-
"output_type": "stream",
11-
"text": [
12-
"N/A\n"
13-
]
14-
}
15-
],
16-
"source": [
17-
"if 'Zebra' in us_state_abbrev.keys():\n",
18-
" print(us_state_abbrev['Zebra'])\n",
19-
"else:\n",
20-
" print(NOT_FOUND)"
21-
]
22-
},
23-
{
24-
"cell_type": "code",
25-
"execution_count": 125,
5+
"execution_count": 5,
266
"metadata": {},
277
"outputs": [],
288
"source": [
@@ -110,7 +90,7 @@
11090
},
11191
{
11292
"cell_type": "code",
113-
"execution_count": null,
93+
"execution_count": 11,
11494
"metadata": {},
11595
"outputs": [],
11696
"source": [
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
state_list = [i for idx, i in enumerate(states) if (idx + 1) % n == 0]
39+
40+
return state_list
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+
# if state_name in us_state_abbrev.keys():
50+
# return us_state_abbrev[state_name]
51+
# else:
52+
# return NOT_FOUND
53+
return us_state_abbrev.get(state_name) or NOT_FOUND
54+
55+
56+
def get_longest_state(data):
57+
"""Receives data, which can be the us_state_abbrev dict or the states
58+
list (see above). It returns the longest state measured by the length
59+
of the string"""
60+
return max(data, key=len)
61+
62+
63+
def combine_state_names_and_abbreviations(us_state_abbrev=us_state_abbrev,
64+
states=states, n=10):
65+
"""Get the first 10 state abbreviations ('AL', 'AK', 'AZ', ...) from
66+
the us_state_abbrev dict, and the last 10 states from the states
67+
list (see above) and combine them into a new list without losing
68+
alphabetical order"""
69+
# from dict
70+
list_a = [v for v in us_state_abbrev.values()]
71+
list_a = list_a[0:n]
72+
list_a.sort()
73+
74+
# from list
75+
76+
states.sort() # check to see if it only does it within the function scope
77+
list_b = states[-n:]
78+
79+
return list_a + list_b
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from challenge import (states, get_every_nth_state,
2+
get_longest_state,
3+
combine_state_names_and_abbreviations,
4+
get_state_abbrev)
5+
6+
7+
def test_get_every_nth_state():
8+
9+
test_states = ['a', 'b', 'c', 'd', 'e', 'f']
10+
11+
assert get_every_nth_state(states=test_states, n=3) == ['c', 'f']
12+
13+
14+
def test_get_state_abbrev():
15+
16+
assert get_state_abbrev(state_name='Alabama') == 'AL'
17+
18+
assert get_state_abbrev(state_name='Warrshington') == 'N/A'
19+
20+
21+
def test_get_longest_state():
22+
23+
data = {'Washington': 'WA', 'ThisIsAReallyLongStateName': 'ZZ'}
24+
25+
assert get_longest_state(data=data) == 'ThisIsAReallyLongStateName'
26+
27+
28+
def test_combine_state_names_and_abbreviations():
29+
30+
test_dict = {'c': 100, 'b': 2}
31+
test_list = ['z', 'y']
32+
test_result = [2, 100, 'y', 'z']
33+
34+
assert combine_state_names_and_abbreviations(us_state_abbrev=test_dict,
35+
states=test_list,
36+
n=2) == test_result

days/10-12-pytest/code/hello.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def hello_name(name):
2+
return f'hello {name}'

days/10-12-pytest/code/hello_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from hello import hello_name
2+
3+
4+
def test_hello_name():
5+
assert hello_name('bill') == 'hello bill'

0 commit comments

Comments
 (0)