-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptimizing_code_common_books_solution.py
70 lines (38 loc) · 1.56 KB
/
optimizing_code_common_books_solution.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
#!/usr/bin/env python
# coding: utf-8
# # Optimizing Code: Common Books - Solution
# Here's the code your coworker wrote to find the common book ids in `books_published_last_two_years.txt` and `all_coding_books.txt` to obtain a list of recent coding books.
# In[1]:
import time
import pandas as pd
import numpy as np
# In[2]:
with open('books_published_last_two_years.txt') as f:
recent_books = f.read().split('\n')
with open('all_coding_books.txt') as f:
coding_books = f.read().split('\n')
# In[3]:
start = time.time()
recent_coding_books = []
for book in recent_books:
if book in coding_books:
recent_coding_books.append(book)
print(len(recent_coding_books))
print('Duration: {} seconds'.format(time.time() - start))
# ### Tip #1: Use vector operations over loops when possible
#
# Use numpy's `intersect1d` method to get the intersection of the `recent_books` and `coding_books` arrays.
# In[4]:
start = time.time()
recent_coding_books = np.intersect1d(recent_books, coding_books)
print(len(recent_coding_books))
print('Duration: {} seconds'.format(time.time() - start))
# ### Tip #2: Know your data structures and which methods are faster
# Use the set's `intersection` method to get the common elements in `recent_books` and `coding_books`.
# In[5]:
start = time.time()
recent_coding_books = set(recent_books).intersection(coding_books)
print(len(recent_coding_books))
print('Duration: {} seconds'.format(time.time() - start))
# ### Looks like using sets to compute the intersection is indeed most efficient in this case!
# In[ ]: