Skip to content

Commit b48d0dc

Browse files
Guilherme DiegoGuilherme Diego
authored andcommitted
New problems
1 parent 4d0249c commit b48d0dc

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

cap1/.problem4.py.swp

-12 KB
Binary file not shown.

cap1/problem14.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from operator import attrgetter
2+
3+
class User:
4+
def __init__(self, user_id):
5+
self.user_id = user_id
6+
7+
def __repr__(self):
8+
return 'User({})'.format(self.user_id)
9+
10+
users = [User(23), User(3), User(99)]
11+
12+
def sort_users():
13+
return sorted(users, key=lambda u: u.user_id)
14+
15+
def sort_users_w_attrgetter():
16+
return sorted(users, key=attrgetter('user_id'))
17+
18+
def exec():
19+
print(sort_users())
20+
print(sort_users_w_attrgetter())
21+
22+
if __name__ == '__main__':
23+
exec()

cap1/problem15.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from itertools import groupby
2+
from operator import itemgetter
3+
from collections import defaultdict
4+
rows = [
5+
{'address': '5412 N CLARK', 'date': '07/01/2012'},
6+
{'address': '5148 N CLARK', 'date': '07/04/2012'},
7+
{'address': '5800 E 58TH', 'date': '07/02/2012'},
8+
{'address': '2122 N CLARK', 'date': '07/03/2012'},
9+
{'address': '5645 N RAVENSWOOD', 'date': '07/02/2012'},
10+
{'address': '1060 W ADDISON', 'date': '07/02/2012'},
11+
{'address': '4801 N BROADWAY', 'date': '07/01/2012'},
12+
{'address': '1039 W GRANVILLE', 'date': '07/04/2012'},
13+
]
14+
15+
def way_1():
16+
rows.sort(key=itemgetter('date'))
17+
18+
for date, items in groupby(rows, key=itemgetter('date')):
19+
print(date)
20+
for i in items:
21+
print(' ', i)
22+
23+
def way_2():
24+
rows_by_date = defaultdict(list)
25+
26+
for row in rows:
27+
rows_by_date[row['date']].append(row)
28+
29+
for r in rows_by_date['07/01/2012']:
30+
print(r)
31+
32+
if __name__ == '__main__':
33+
# way_1()
34+
way_2()

cap1/problem16.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import time
2+
3+
rows = [1, 4, -5, 10, -7, 2, 3, -1]
4+
5+
def list_comprehesion():
6+
rows_filtered_positive = [r for r in rows if r >= 0]
7+
rows_filtered_negative = [r for r in rows if r < 0]
8+
9+
print(rows_filtered_positive)
10+
print(rows_filtered_negative)
11+
12+
def generator():
13+
rows_filtered_positive = (r for r in rows if r >= 0)
14+
rows_filtered_negative = (r for r in rows if r < 0)
15+
16+
for row in rows_filtered_positive:
17+
print(row)
18+
19+
for row in rows_filtered_negative:
20+
print(row)
21+
22+
def filter_try_except():
23+
values = '1 2 -3 - 4 N/A'.split()
24+
def is_int(n):
25+
try:
26+
x = int(n)
27+
return True
28+
except:
29+
return False
30+
31+
vals = list(filter(is_int, values))
32+
print(vals)
33+
34+
35+
if __name__ == '__main__':
36+
print('List Comprehesion')
37+
t1 = time.time()
38+
list_comprehesion()
39+
print('spent: %.6f' % (time.time() - t1))
40+
41+
print('Generator')
42+
t1 = time.time()
43+
generator()
44+
print('spent: %.6f' % (time.time() - t1))
45+
46+
print('Filter try/except')
47+
t1 = time.time()
48+
filter_try_except()
49+
print('spent: %.6f' % (time.time() - t1))

0 commit comments

Comments
 (0)