-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day_7-9.py
245 lines (204 loc) · 8.71 KB
/
Day_7-9.py
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
cars = {
'Ford': ['Falcon', 'Focus', 'Festiva', 'Fairlane'],
'Holden': ['Commodore', 'Captiva', 'Barina', 'Trailblazer'],
'Nissan': ['Maxima', 'Pulsar', '350Z', 'Navara'],
'Honda': ['Civic', 'Accord', 'Odyssey', 'Jazz'],
'Jeep': ['Grand Cherokee', 'Cherokee', 'Trailhawk', 'Trackhawk']
}
def get_all_jeeps():
"""return a comma separated string of jeep models (original order)"""
return ", ".join(cars['Jeep'])
def get_first_model_each_manufacturer():
"""return a list of matching models (original ordering)"""
lst = []
for make, model in cars.items():
lst.append(model[0])
return lst
def get_all_matching_models(grep='trail'):
"""return a list of all models containing the case insensitive
'grep' string which defaults to 'trail' for this exercise,
sort the resulting sequence alphabetically"""
lst = []
for make, model in cars.items():
for version in model:
if grep.upper() in version.upper():
lst.append(version)
return sorted(lst)
def sort_car_models():
"""sort the car models (values) and return the resulting cars dict"""
for make, model in cars.items():
cars[make] = sorted(model)
return cars
def main():
print(get_all_jeeps())
print(get_first_model_each_manufacturer())
print(get_all_matching_models())
print(sort_car_models())
main()
#============= Day 2=======================
us_state_abbrev = {'Alabama': 'AL', 'Alaska': 'AK', 'Arizona': 'AZ',
'Arkansas': 'AR', 'California': 'CA', 'Colorado': 'CO',
'Connecticut': 'CT', 'Delaware': 'DE', 'Florida': 'FL',
'Georgia': 'GA', 'Hawaii': 'HI', 'Idaho': 'ID',
'Illinois': 'IL', 'Indiana': 'IN', 'Iowa': 'IA',
'Kansas': 'KS', 'Kentucky': 'KY', 'Louisiana': 'LA',
'Maine': 'ME', 'Maryland': 'MD', 'Massachusetts': 'MA',
'Michigan': 'MI', 'Minnesota': 'MN', 'Mississippi': 'MS',
'Missouri': 'MO', 'Montana': 'MT', 'Nebraska': 'NE',
'Nevada': 'NV', 'New Hampshire': 'NH', 'New Jersey': 'NJ',
'New Mexico': 'NM', 'New York': 'NY',
'North Carolina': 'NC', 'North Dakota': 'ND',
'Ohio': 'OH', 'Oklahoma': 'OK', 'Oregon': 'OR',
'Pennsylvania': 'PA', 'Rhode Island': 'RI',
'South Carolina': 'SC', 'South Dakota': 'SD',
'Tennessee': 'TN', 'Texas': 'TX', 'Utah': 'UT',
'Vermont': 'VT', 'Virginia': 'VA', 'Washington': 'WA',
'West Virginia': 'WV', 'Wisconsin': 'WI', 'Wyoming': 'WY'}
states = ['Oklahoma', 'Kansas', 'North Carolina', 'Georgia', 'Oregon',
'Mississippi', 'Minnesota', 'Colorado', 'Alabama',
'Massachusetts', 'Arizona', 'Connecticut', 'Montana',
'West Virginia', 'Nebraska', 'New York', 'Nevada', 'Idaho',
'New Jersey', 'Missouri', 'South Carolina', 'Pennsylvania',
'Rhode Island', 'New Mexico', 'Alaska', 'New Hampshire',
'Tennessee', 'Washington', 'Indiana', 'Hawaii', 'Kentucky',
'Virginia', 'Ohio', 'Wisconsin', 'Maryland', 'Florida',
'Utah', 'Maine', 'California', 'Vermont', 'Arkansas', 'Wyoming',
'Louisiana', 'North Dakota', 'South Dakota', 'Texas',
'Illinois', 'Iowa', 'Michigan', 'Delaware']
NOT_FOUND = 'N/A'
def get_every_nth_state(n=10):
"""Return a list with every nth item (default 10th item)
of states (takeaway: lists keep order)"""
loops = int(50/n)
state_list = []
for i in range(loops):
state_list.append(states[(i+1)*n-1])
return state_list
def get_state_abbrev(abbrev):
"""Look up a state abbreviation by full name in
us_state_abbrev, if not found return NOT_FOUND
(takeaway: dicts are great for lookups)"""
return us_state_abbrev.get(abbrev,NOT_FOUND)
def get_longest_state(data):
"""Takes dict or list and determines the longest state name
(takeaways: use a dict method to get all keys, use sorted)"""
return max(data, key=len)
def combine_state_names_and_abbreviations():
"""Return a new list with the first 10 abbreviations from the
us_state_abbrev dict ordered values and the last 10 states from
the states list (takeaways: use another dict method to get all
values and use sorted, list slicing and list concatenation)"""
lst=[]
for k,v in us_state_abbrev.items():
lst.append(v)
lst = sorted(lst[:10])
state = sorted(states)
print(lst+state[-10:])
return
#____________ Py Bites solutions _____________
us_state_abbrev = {'Alabama': 'AL', 'Alaska': 'AK', 'Arizona': 'AZ',
'Arkansas': 'AR', 'California': 'CA', 'Colorado': 'CO',
'Connecticut': 'CT', 'Delaware': 'DE', 'Florida': 'FL',
'Georgia': 'GA', 'Hawaii': 'HI', 'Idaho': 'ID',
'Illinois': 'IL', 'Indiana': 'IN', 'Iowa': 'IA',
'Kansas': 'KS', 'Kentucky': 'KY', 'Louisiana': 'LA',
'Maine': 'ME', 'Maryland': 'MD', 'Massachusetts': 'MA',
'Michigan': 'MI', 'Minnesota': 'MN', 'Mississippi': 'MS',
'Missouri': 'MO', 'Montana': 'MT', 'Nebraska': 'NE',
'Nevada': 'NV', 'New Hampshire': 'NH', 'New Jersey': 'NJ',
'New Mexico': 'NM', 'New York': 'NY',
'North Carolina': 'NC', 'North Dakota': 'ND',
'Ohio': 'OH', 'Oklahoma': 'OK', 'Oregon': 'OR',
'Pennsylvania': 'PA', 'Rhode Island': 'RI',
'South Carolina': 'SC', 'South Dakota': 'SD',
'Tennessee': 'TN', 'Texas': 'TX', 'Utah': 'UT',
'Vermont': 'VT', 'Virginia': 'VA', 'Washington': 'WA',
'West Virginia': 'WV', 'Wisconsin': 'WI', 'Wyoming': 'WY'}
states = ['Oklahoma', 'Kansas', 'North Carolina', 'Georgia', 'Oregon',
'Mississippi', 'Minnesota', 'Colorado', 'Alabama',
'Massachusetts', 'Arizona', 'Connecticut', 'Montana',
'West Virginia', 'Nebraska', 'New York', 'Nevada', 'Idaho',
'New Jersey', 'Missouri', 'South Carolina', 'Pennsylvania',
'Rhode Island', 'New Mexico', 'Alaska', 'New Hampshire',
'Tennessee', 'Washington', 'Indiana', 'Hawaii', 'Kentucky',
'Virginia', 'Ohio', 'Wisconsin', 'Maryland', 'Florida',
'Utah', 'Maine', 'California', 'Vermont', 'Arkansas', 'Wyoming',
'Louisiana', 'North Dakota', 'South Dakota', 'Texas',
'Illinois', 'Iowa', 'Michigan', 'Delaware']
NOT_FOUND = 'N/A'
def get_every_nth_state(n=10):
"""Return a list with every nth item (default 10th item)
of states (takeaway: lists keep order)"""
new_states = []
for i, state in enumerate(states, 1):
if i % n == 0:
new_states.append(state)
return new_states
def get_state_abbrev(abbrev):
"""Look up a state abbreviation by full name in
us_state_abbrev, if not found return NOT_FOUND
(takeaway: dicts are great for lookups)"""
return us_state_abbrev.get(abbrev) or NOT_FOUND
def get_longest_state(data):
"""Takes dict or list and determines the longest state name
(takeaways: use a dict method to get all keys, use sorted)"""
if isinstance(data, dict):
data = data.keys()
return sorted(data, key=len)[-1]
def combine_state_names_and_abbreviations():
"""Return a new list with the first 10 abbreviations from the
us_state_abbrev dict ordered values and the last 10 states from
the states list (takeaways: use another dict method to get all
values and use sorted, list slicing and list concatenation)"""
return sorted(us_state_abbrev.values())[:10] + sorted(states)[-10:]
# numlist = [1,2,3,4,5]
# print(numlist)
# numlist.reverse()
# print(numlist)
# numlist.sort()
# print(numlist)
# for num in numlist:
# print(str(num))
# mystring = "Jarrod"
# list(mystring)
# l = list(mystring)
# print(l[0])
# print(l.pop())
# print(l)
# l.insert(5,"d")
# print(l)
# del l[0]
# print(l)
# l.pop(1)
# print(l)
# l.append('s')
# print(l)
# mystring = "Jarrod"
# l = list(mystring)
# t = tuple(mystring)
# print(l, t)
# l[0]= "t"
# # immutable -> t[0] = "t"
# for let in t:
# print (let)
#
# pybytes = {"jarrod": 33, "Sarah": 30 }
# print(pybytes)
# people = {}
# people["Jarrod"] = 33
# print(people)
# people["Sarah"] = 30
# print(people.keys())
# print(people.values())
# print(people.items())
# for keys in pybytes.keys():
# print(keys)
# for keys,values in pybytes.items():
# print("Keys:{} Values:{}".format(keys, values))
#
# l =list("Jarrod")
# n = [1,2,3,4,5,6]
# for values in n:
# people[values] = people.get(values,values+1)
#
# print(people.items())