Skip to content

Commit fa72a88

Browse files
committed
More medium & hard solutions
1 parent 69e59c5 commit fa72a88

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

chapter_16/p17_20.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def zeroes_in_fact(n: int):
2+
five_pow = 1
3+
nr_z = 0
4+
while (5**five_pow) <= n:
5+
nr_z += (n // 5 ** five_pow)
6+
five_pow += 1
7+
return nr_z
8+
9+
10+
if __name__ == "__main__":
11+
exs = [1, 10, 34, 55, 642, 1000]
12+
for ex in exs:
13+
print(f"Number of zeroes in {ex}! is {zeroes_in_fact(ex)}")

chapter_17/p17_26.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from typing import List, Tuple
2+
from collections import defaultdict
3+
4+
5+
def sparse_similarity(docs_raw: List[Tuple[int, List[int]]]) -> None:
6+
doc_dict = dict(docs_raw)
7+
rev_index = defaultdict(set)
8+
9+
for doc_id, doc_words in doc_dict.items():
10+
for word in doc_words:
11+
rev_index[word].add(doc_id)
12+
13+
intersect_of_pairs = defaultdict(int)
14+
15+
for doc_id, doc_words in doc_dict.items():
16+
for word in doc_words:
17+
for other_doc in rev_index[word]:
18+
# We assume an ordering of the doc ids,
19+
# as to prevent duplicate counting
20+
if other_doc <= doc_id:
21+
continue
22+
23+
intersect_of_pairs[(doc_id, other_doc)] += 1
24+
25+
print(f"ID1, ID2:\tSIMILARITY")
26+
for (doc1, doc2), in_common in intersect_of_pairs.items():
27+
union_size = len(doc_dict[doc1]) + len(doc_dict[doc2]) - in_common
28+
docs_simil = in_common / union_size
29+
30+
print(f"{doc1:3}, {doc2:3}:\t{docs_simil}")
31+
32+
33+
if __name__ == "__main__":
34+
ex = [
35+
(13, [14, 15, 100, 9, 3]),
36+
(16, [32, 1, 9, 3, 5]),
37+
(19, [15, 29, 2, 6, 8, 7]),
38+
(24, [7, 10])
39+
]
40+
41+
sparse_similarity(ex)

chapter_17/p17_5.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
def longest_subarr_number_letter_equal(arr: str) -> int:
3+
more_letters_now = 0
4+
seen_offsets = {0: -1}
5+
max_arr_size = 0
6+
max_arr_indexes = (-1, -1)
7+
8+
for i in range(len(arr)):
9+
x = arr[i]
10+
11+
if x.isdigit():
12+
more_letters_now -= 1
13+
else:
14+
more_letters_now += 1
15+
16+
# print(f"Offsets: {seen_offsets}, letters more now: {more_letters_now}, elem now {x}")
17+
18+
if more_letters_now in seen_offsets:
19+
max_arr_now = i - seen_offsets[more_letters_now]
20+
if max_arr_now > max_arr_size:
21+
max_arr_size = max_arr_now
22+
max_arr_indexes = (seen_offsets[more_letters_now]+1, i)
23+
else:
24+
seen_offsets[more_letters_now] = i
25+
26+
return max_arr_size, max_arr_indexes
27+
28+
29+
if __name__ == "__main__":
30+
exs = [
31+
"aaa2222aa22aa222aaaa",
32+
"aaa222aa",
33+
"e4ee2a2a2a",
34+
"z0123456789a"
35+
]
36+
for ex_arr in exs:
37+
max_arr, (start, end) = longest_subarr_number_letter_equal(ex_arr)
38+
print(f"Max arr with eq is of len {max_arr}, "
39+
f"namely: {ex_arr[start:end+1]}, from {ex_arr}")

0 commit comments

Comments
 (0)