File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed
Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change 1+ # ORIGINAL SLOW CODE
2+
3+
4+ def find_common_tags (news_articles ) -> set [str ]:
5+ if not news_articles :
6+ return set ()
7+
8+ common_tags = news_articles [0 ]["tags" ]
9+ for article in news_articles [1 :]:
10+ common_tags = [tag for tag in common_tags if tag in article .get ("tags" , [])]
11+ return set (common_tags )
12+
13+
14+ # OPTIMIZATION 1: Does not win - Is 33x faster
15+
16+
17+ def find_common_tags (news_articles ) -> set [str ]:
18+ if not news_articles :
19+ return set ()
20+
21+ common_tags = set (news_articles [0 ]["tags" ])
22+ for article in news_articles [1 :]:
23+ common_tags &= set (article .get ("tags" , []))
24+ return common_tags
25+
26+
27+ # OPTIMIZATION 2 : Winning Optimization - Is 90x faster
28+
29+
30+ def find_common_tags (news_articles ) -> set [str ]:
31+ if not news_articles :
32+ return set ()
33+
34+ common_tags = set (news_articles [0 ]["tags" ])
35+ for article in news_articles [1 :]:
36+ common_tags .intersection_update (article .get ("tags" , []))
37+ return common_tags
You can’t perform that action at this time.
0 commit comments