Skip to content

Commit c15c2a6

Browse files
new files
1 parent 1336945 commit c15c2a6

File tree

4 files changed

+271
-0
lines changed

4 files changed

+271
-0
lines changed
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
us_state_abbrev = {
2+
"Alabama": "AL",
3+
"Alaska": "AK",
4+
"Arizona": "AZ",
5+
"Arkansas": "AR",
6+
"California": "CA",
7+
"Colorado": "CO",
8+
"Connecticut": "CT",
9+
"Delaware": "DE",
10+
"Florida": "FL",
11+
"Georgia": "GA",
12+
"Hawaii": "HI",
13+
"Idaho": "ID",
14+
"Illinois": "IL",
15+
"Indiana": "IN",
16+
"Iowa": "IA",
17+
"Kansas": "KS",
18+
"Kentucky": "KY",
19+
"Louisiana": "LA",
20+
"Maine": "ME",
21+
"Maryland": "MD",
22+
"Massachusetts": "MA",
23+
"Michigan": "MI",
24+
"Minnesota": "MN",
25+
"Mississippi": "MS",
26+
"Missouri": "MO",
27+
"Montana": "MT",
28+
"Nebraska": "NE",
29+
"Nevada": "NV",
30+
"New Hampshire": "NH",
31+
"New Jersey": "NJ",
32+
"New Mexico": "NM",
33+
"New York": "NY",
34+
"North Carolina": "NC",
35+
"North Dakota": "ND",
36+
"Ohio": "OH",
37+
"Oklahoma": "OK",
38+
"Oregon": "OR",
39+
"Pennsylvania": "PA",
40+
"Rhode Island": "RI",
41+
"South Carolina": "SC",
42+
"South Dakota": "SD",
43+
"Tennessee": "TN",
44+
"Texas": "TX",
45+
"Utah": "UT",
46+
"Vermont": "VT",
47+
"Virginia": "VA",
48+
"Washington": "WA",
49+
"West Virginia": "WV",
50+
"Wisconsin": "WI",
51+
"Wyoming": "WY",
52+
}
53+
54+
states = [
55+
"Oklahoma", # 0
56+
"Kansas",
57+
"North Carolina",
58+
"Georgia",
59+
"Oregon",
60+
"Mississippi",
61+
"Minnesota",
62+
"Colorado",
63+
"Alabama",
64+
"Massachusetts", # 9
65+
"Arizona",
66+
"Connecticut",
67+
"Montana",
68+
"West Virginia",
69+
"Nebraska",
70+
"New York",
71+
"Nevada",
72+
"Idaho",
73+
"New Jersey",
74+
"Missouri", # 19
75+
"South Carolina",
76+
"Pennsylvania",
77+
"Rhode Island",
78+
"New Mexico",
79+
"Alaska",
80+
"New Hampshire",
81+
"Tennessee",
82+
"Washington",
83+
"Indiana",
84+
"Hawaii", # 29
85+
"Kentucky",
86+
"Virginia",
87+
"Ohio",
88+
"Wisconsin",
89+
"Maryland",
90+
"Florida",
91+
"Utah",
92+
"Maine",
93+
"California",
94+
"Vermont", # 39
95+
"Arkansas",
96+
"Wyoming",
97+
"Louisiana",
98+
"North Dakota",
99+
"South Dakota",
100+
"Texas",
101+
"Illinois",
102+
"Iowa",
103+
"Michigan",
104+
"Delaware", # 49
105+
]
106+
107+
NOT_FOUND = "N/A"
108+
109+
110+
def get_every_nth_state(states=states, n=10):
111+
"""Return a list with every nth item (default argument n=10, so every
112+
10th item) of the states list above (remember: lists keep order)"""
113+
114+
nth_states = [
115+
state for index, state in enumerate(states) if not (index + 1) % n and index > 0
116+
]
117+
return nth_states
118+
119+
120+
def get_state_abbrev(state_name, us_state_abbrev=us_state_abbrev):
121+
"""Look up a state abbreviation by querying the us_state_abbrev
122+
dict by full state name, for instance 'Alabama' returns 'AL',
123+
'Illinois' returns 'IL'.
124+
If the state is not in the dict, return 'N/A' which we stored
125+
in the NOT_FOUND constant (takeaway: dicts are great for lookups)"""
126+
127+
if not state_name in us_state_abbrev:
128+
return NOT_FOUND
129+
130+
return us_state_abbrev[state_name]
131+
132+
133+
def get_longest_state(data):
134+
"""Receives data, which can be the us_state_abbrev dict or the states
135+
list (see above). It returns the longest state measured by the length
136+
of the string"""
137+
longest_state = ""
138+
139+
for state in data:
140+
if len(state) > len(longest_state):
141+
longest_state = state
142+
143+
return longest_state
144+
145+
146+
def combine_state_names_and_abbreviations(
147+
us_state_abbrev=us_state_abbrev, states=states
148+
):
149+
"""Get the first 10 state abbreviations ('AL', 'AK', 'AZ', ...) from
150+
the us_state_abbrev dict, and the last 10 states from the states
151+
list (see above) and combine them into a new list. The resulting list
152+
has both sorted, so:
153+
['AK', 'AL', 'AZ', ..., 'South Dakota', 'Tennessee', 'Texas', ...]
154+
(see also test_combine_state_names_and_abbreviations)"""
155+
156+
abbrevs = list(us_state_abbrev.values())
157+
abbrevs.sort()
158+
first_10_abbrev = abbrevs[:10]
159+
160+
states.sort()
161+
last_10_states = states[-10:]
162+
163+
return first_10_abbrev + last_10_states
164+
165+
166+
def main():
167+
nth_states = get_every_nth_state()
168+
alabama_abbrev = get_state_abbrev("Alabama")
169+
illinois_abbrev = get_state_abbrev("Illinois")
170+
nf_avbrev = get_state_abbrev("Ada")
171+
state_1 = get_longest_state(us_state_abbrev)
172+
state_2 = get_longest_state(states)
173+
combination = combine_state_names_and_abbreviations()
174+
175+
if __name__ == "__main__":
176+
main()
Lines changed: 33 additions & 0 deletions
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

days/10-12-pytest/app/app.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
def add(x, y):
2+
return x + y
3+
4+
5+
def subtract(x, y):
6+
return x - y
7+
8+
9+
def multiply(x, y):
10+
return x * y
11+
12+
13+
def divide(x, y):
14+
return x * 1.0 / y
15+
16+
17+
def maximum(x, y):
18+
return x if x >= y else y
19+
20+
21+
def minimum(x, y):
22+
return x if x <= y else y

days/10-12-pytest/app/test_app.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import pytest
2+
import app
3+
4+
NUMBER_1 = 10
5+
NUMBER_2 = 2
6+
7+
8+
@pytest.fixture(scope="module")
9+
def numbers_setup():
10+
print("numbers_setup")
11+
12+
13+
def test_add(numbers_setup):
14+
result = app.add(NUMBER_1, NUMBER_2)
15+
assert result == 12
16+
17+
18+
def test_subtract(numbers_setup):
19+
result = app.subtract(NUMBER_1, NUMBER_2)
20+
assert result == 8
21+
22+
23+
def test_multiply(numbers_setup):
24+
result = app.multiply(NUMBER_1, NUMBER_2)
25+
assert result == 20
26+
27+
28+
def test_divide(numbers_setup):
29+
result = app.divide(NUMBER_1, NUMBER_2)
30+
assert result == 5
31+
32+
33+
def test_maximum(numbers_setup):
34+
result = app.maximum(NUMBER_1, NUMBER_2)
35+
assert result == NUMBER_1
36+
37+
38+
def test_minimum(numbers_setup):
39+
result = app.minimum(NUMBER_1, NUMBER_2)
40+
assert result == NUMBER_2

0 commit comments

Comments
 (0)