Skip to content

Commit a45e450

Browse files
committed
add find_common_tags example
1 parent df64293 commit a45e450

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/dsa/various.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import re
24

35

@@ -114,3 +116,15 @@ def find_shortest_path(self, start: str, end: str) -> list[str]:
114116
queue.append(new_path)
115117

116118
return [] # No path found
119+
120+
121+
def find_common_tags(articles: list[dict[str, list[str]]]) -> set[str]:
122+
if not articles:
123+
return set()
124+
125+
common_tags = set(articles[0].get("tags", []))
126+
for article in articles[1:]:
127+
common_tags.intersection_update(article.get("tags", []))
128+
if not common_tags:
129+
break
130+
return common_tags

tests/test_common_tags.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from src.dsa.various import find_common_tags
2+
3+
def test_common_tags_1() -> None:
4+
articles_1 = [
5+
{"title": "Article 1", "tags": ["Python", "AI", "ML"]},
6+
{"title": "Article 2", "tags": ["Python", "Data Science", "AI"]},
7+
{"title": "Article 3", "tags": ["Python", "AI", "Big Data"]},
8+
]
9+
10+
expected = {"Python", "AI"}
11+
12+
assert find_common_tags(articles_1) == expected
13+
14+
articles_2 = [
15+
{"title": "Article 1", "tags": ["Python", "AI", "ML"]},
16+
{"title": "Article 2", "tags": ["Python", "Data Science", "AI"]},
17+
{"title": "Article 3", "tags": ["Python", "AI", "Big Data"]},
18+
{"title": "Article 4", "tags": ["Python", "AI", "ML"]},
19+
]
20+
21+
assert find_common_tags(articles_2) == expected

0 commit comments

Comments
 (0)