From 1a5c6a1253ef39bdef99fa74679d32ee0ec797e1 Mon Sep 17 00:00:00 2001 From: "codeflash-ai-dev[bot]" <157075493+codeflash-ai-dev[bot]@users.noreply.github.com> Date: Tue, 4 Mar 2025 22:07:42 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Speed=20up=20function=20`f?= =?UTF-8?q?ind=5Fcommon=5Ftags`=20by=2018,960%=20Certainly!=20The=20progra?= =?UTF-8?q?m=20can=20be=20optimized=20by=20utilizing=20sets=20for=20inters?= =?UTF-8?q?ection=20operations,=20which=20are=20generally=20faster=20than?= =?UTF-8?q?=20list=20comprehensions=20for=20this=20kind=20of=20task.=20Her?= =?UTF-8?q?e=E2=80=99s=20the=20optimized=20version.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### Changes Made. 1. **Utilized Set for Intersection**: Instead of a list comprehension inside the loop, using set operations (`intersection_update`) significantly optimizes the performance for finding common elements. 2. **Retained the Original Functionality**: The function signature and return value remain unchanged. This will run faster, especially when dealing with a large number of articles or tags, as set operations are generally more efficient for membership tests and intersections. --- common_tags.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 common_tags.py diff --git a/common_tags.py b/common_tags.py new file mode 100644 index 0000000..8cbb836 --- /dev/null +++ b/common_tags.py @@ -0,0 +1,13 @@ +from __future__ import annotations + + +def find_common_tags(articles: list[dict[str, list[str]]]) -> set[str]: + if not articles: + return set() + + # Using set intersection to find common tags + common_tags = set(articles[0]["tags"]) + for article in articles[1:]: + common_tags.intersection_update(article["tags"]) + + return common_tags