Compare commits

..

2 Commits

Author SHA1 Message Date
25a5331b47 feat: add task 8 with inverted index 2026-04-10 19:54:08 +06:00
5087b2a308 feat: add task 7 naive approach 2026-04-10 19:53:47 +06:00
2 changed files with 76 additions and 0 deletions

40
task7/main.py Normal file
View File

@@ -0,0 +1,40 @@
from itertools import combinations, repeat
import random
from typing import Any, Final, Iterable
from collections import Counter
from icecream import ic
sets_count: Final[int] = random.randint(2, 1000)
elements_count: Final[Iterable[int]] = (random.randint(3, 10000) for _ in repeat(None, sets_count))
ic(sets_count)
sets: list[set[int]] = [
set(
random.sample(range(-2_000_000_000, 2_000_000_000 + 1), count)
)
for count in elements_count
]
elements_to_sets: dict[int, list[int]] = {}
pair_counts: Counter[tuple[int, int]] = Counter()
for i, set_ in enumerate(sets):
for element in set_:
elements_to_sets.setdefault(element, list()).append(i)
ic(len(elements_to_sets))
for sets_ in filter(lambda x: len(x) > 1, elements_to_sets.values()):
pair_counts.update(combinations(sets_, 2))
sets_with_most_intersections: list[tuple[Any, int]] = pair_counts.most_common(1)
if sets_with_most_intersections:
max_intersection: int = sets_with_most_intersections[0][1]
else:
max_intersection = 0
print(max_intersection)

36
task7/naive_approach.py Normal file
View File

@@ -0,0 +1,36 @@
from itertools import repeat
import random
from typing import Final, Iterable
from icecream import ic
sets_count: Final[int] = random.randint(2, 1000)
elements_count: Final[Iterable[int]] = (random.randint(3, 10000) for _ in repeat(None, sets_count))
ic(sets_count)
sets: list[set[int]] = [
set(
random.sample(range(-2_000_000_000, 2_000_000_000 + 1), count)
)
for count in elements_count
]
sets.sort(key=lambda x: len(x), reverse=True)
ic(list(map(len, sets)))
max_intersection = 0
for i in range(len(sets) - 1):
if max_intersection >= len(sets[i]):
ic(f"break on {i}")
break
for j in range(i + 1, len(sets)):
if max_intersection > len(sets[j]):
ic(f"break on {i};{j}")
break
if (intersection := len(sets[i].intersection(sets[j]))) > max_intersection:
max_intersection = intersection
ic(max_intersection)
print(max_intersection)