-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStateCap.py
More file actions
166 lines (145 loc) · 4.73 KB
/
StateCap.py
File metadata and controls
166 lines (145 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
"""
We have an existing dictionary that maps US states to their capitals.
1. Print the state capital of Idaho
2. Print all states.
3. Print all capitals.
4. Create a single string 'Alabama -> Montgomery, Alaska -> Juneau, ...'
5. Ensure the string you created in 4. is alphabetically sorted by state
7. Now we want to add the reverse look up, given the name of a capital what state
is it in?
Implement the function def get_state(capital): below so it returns the state.
GOTCHAS: What happens if two states have the same capital name ?
==> YG added ==> Will return the last one from the dictionary,
how do you handle that?
==> YG added ==> code dev if several keys count... ==> OK DONE ;-)
git check
"""
import sys
import pytest
STATES_CAPITALS = {
'Alabama' : 'Montgomery',
'Alaska' : 'Juneau',
'Arizona' : 'Phoenix',
'Arkansas': 'Little Rock',
'California' : 'Sacramento',
'Colorado' : 'Denver',
'Connecticut' : 'Hartford',
'Delaware' : 'Dover',
'Florida' : 'Tallahassee',
'Georgia' : 'Atlanta',
'Hawaii' : 'Honolulu',
'Idaho' : 'Boise',
'Illinois' : 'Springfield',
# 'Indiana' : 'Indianapolis',
'Indiana' : 'Boise',
'Iowa' : 'Des Moines',
'Kansas' : 'Topeka',
'Kentucky' : 'Frankfort',
'Louisiana' : 'Baton Rouge',
'Maine' : 'Augusta',
'Maryland' : 'Annapolis',
'Massachusetts' : 'Boston',
'Michigan' : 'Lansing',
'Minnesota' : 'Saint Paul',
'Mississippi' : 'Jackson',
'Missouri' : 'Jefferson City',
'Montana' : 'Helena',
'Nebraska' : 'Lincoln',
'Nevada' : 'Carson City',
'New Hampshire' : 'Concord',
'New Jersey' : 'Trenton',
'New Mexico' : 'Santa Fe',
'New York' : 'Albany',
'North Carolina' : 'Raleigh',
'North Dakota' : 'Bismarck',
'Ohio' : 'Columbus',
'Oklahoma' : 'Oklahoma City',
'Oregon' : 'Salem',
'Pennsylvania' : 'Harrisburg',
'Rhode Island' : 'Providence',
'South Carolina' : 'Columbia',
'South Dakota' : 'Pierre',
'Tennessee' : 'Nashville',
'Texas' : 'Austin',
'Utah' : 'Salt Lake City',
'Vermont' : 'Montpelier',
'Virginia' : 'Richmond',
'Washington' : 'Olympia',
'West Virginia' : 'Charleston',
'Wisconsin' : 'Madison',
'Wyoming' : 'Cheyenne',
# 'Ababaaa' : 'YoavGUEZ'
}
def capital_of_Idaho():
print('Capital of Idaho = ' + STATES_CAPITALS['Idaho'])
def all_states():
print('All States:')
state_check_ordered = []
for key in STATES_CAPITALS.keys():
print(key)
def all_states_check():
print('All States: Checking if sorted alphabetically:...\n')
state_check_ordered = []
for key in STATES_CAPITALS.keys():
state_check_ordered.append(key)
# print(list(state_check_ordered) == sorted(state_check_ordered))
if list(state_check_ordered) == sorted(state_check_ordered):
print("OK GOOD !!! STATES are sorted alphabetically")
else:
print("ATTENTION !!! STATES are not sorted alphabetically!!!")
# print(state_check_ordered)
return
def all_capitals():
print('All Capitals:')
for value in STATES_CAPITALS.values():
print(value)
def states_capitals_string():
print('States and Capitals in a string:')
str = ''
for key,value in STATES_CAPITALS.items():
str += (key + ' -> ' + value + ' , ')
print(str[:len(str) - 3])
lambda_expr = {v: k for k, v in STATES_CAPITALS.items()}
def get_state(capital):
return lambda_expr[capital]
def get_state_yg(capital,value):
for k, v in STATES_CAPITALS.items():
if v == value:
yield k
# return lambda_expr[capital]
# Here, I test my code.
capital_of_Idaho()
print('\n\n\n')
all_states()
print('\n\n\n')
all_capitals()
print('\n\n\n')
states_capitals_string()
print('\n')
all_states_check()
print('\n\n\n')
print('The state of Madison Capital city = ' + get_state('Madison'))
# ==> YG Added:
print('\n\n\n')
capi = input("What is your Capital city? I will guess your state...")
keys = list(get_state_yg(STATES_CAPITALS, capi))
if len(keys) > 1:
print("you have more that one answer...\nThe possibilities are:\n\t\t\t\t\t", keys)
else:
print(f'The state of {capi} Capital city = ' + get_state(capi))
def test_state_to_capital():
assert 'Cheyenne' == STATES_CAPITALS['Wyoming']
def test_state_to_capital_unknown():
with pytest.raises(KeyError):
STATES_CAPITALS['']
def test_capital_to_state():
assert 'Wyoming' == get_state('Cheyenne')
def test_capital_to_state_unknown():
with pytest.raises(KeyError):
raise KeyError
"""
def main():
return pytest.main(_file_)
if _name_ == '_main_':
sys.exit(main())
"""