Skip to content

Commit c03d850

Browse files
committed
AlgoExpert Generate Document
1 parent 9a337dc commit c03d850

File tree

3 files changed

+100
-5
lines changed

3 files changed

+100
-5
lines changed

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -987,14 +987,17 @@ First column links to the problem in AlgoExpert, second is the problem's difficu
987987

988988
[🔝 Back to Top 🔝](#coding-challenges)
989989

990-
| AlgoExpert Link | Difficulty | Solutions |
991-
| ----------------------------------- | ------------ | --------------------------------------------- |
992-
| [Insertion Sort][ae&insertion-sort] | 🟢 Easy | [![python](res/py.png)][ae&insertion-sort#py] |
993-
| [Merge Sort][ae&merge-sort] | 🟣 Very Hard | [![python](res/py.png)][ae&merge-sort#py] |
994-
| [Remove Islands][ae&remove-islands] | 🟠 Medium | [![python](res/py.png)][ae&remove-islands#py] |
990+
| AlgoExpert Link | Difficulty | Solutions |
991+
| ----------------------------------------- | ------------ | ------------------------------------------------ |
992+
| [Generate Document][ae&generate-document] | 🟢 Easy | [![python](res/py.png)][ae&generate-document#py] |
993+
| [Insertion Sort][ae&insertion-sort] | 🟢 Easy | [![python](res/py.png)][ae&insertion-sort#py] |
994+
| [Merge Sort][ae&merge-sort] | 🟣 Very Hard | [![python](res/py.png)][ae&merge-sort#py] |
995+
| [Remove Islands][ae&remove-islands] | 🟠 Medium | [![python](res/py.png)][ae&remove-islands#py] |
995996

996997
[🔝 Back to Top 🔝](#coding-challenges)
997998

999+
[ae&generate-document]: https://www.algoexpert.io/questions/generate-document
1000+
[ae&generate-document#py]: algoexpert/generate-document.py
9981001
[ae&insertion-sort]: https://www.algoexpert.io/questions/merge-sort
9991002
[ae&insertion-sort#py]: algoexpert/insertion-sort.py
10001003
[ae&merge-sort]: https://www.algoexpert.io/questions/merge-sort

algoexpert/generate-document.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Generate Document
2+
# 🟢 Easy
3+
#
4+
# https://www.algoexpert.io/questions/generate-document
5+
#
6+
# Tags: String - Hash Table
7+
8+
import timeit
9+
from collections import Counter
10+
11+
12+
# Use two counters, frequencies needed and available, then make sure
13+
# that we have, at least, as many available of each as we need.
14+
#
15+
# Time complexity: O(m + n) - Where m and n are the lengths of the input
16+
# strings.
17+
# Space complexity: O(c) - Where c is the number of unique characters
18+
# between both input strings.
19+
class Solution:
20+
def generateDocument(self, characters: str, document: str) -> bool:
21+
need, available = Counter(document), Counter(characters)
22+
return all(available[c] >= need[c] for c in need)
23+
24+
25+
def test():
26+
executors = [Solution]
27+
tests = [
28+
["A", "a", False],
29+
["a", "a", True],
30+
["Bste!hetsi ogEAxpelrt x ", "AlgoExpert is the Best!", True],
31+
]
32+
for executor in executors:
33+
start = timeit.default_timer()
34+
for _ in range(1):
35+
for col, t in enumerate(tests):
36+
sol = executor()
37+
result = sol.generateDocument(t[0], t[1])
38+
exp = t[2]
39+
assert result == exp, (
40+
f"\033[93m» {result} <> {exp}\033[91m for"
41+
+ f" test {col} using \033[1m{executor.__name__}"
42+
)
43+
stop = timeit.default_timer()
44+
used = str(round(stop - start, 5))
45+
cols = "{0:20}{1:10}{2:10}"
46+
res = cols.format(executor.__name__, used, "seconds")
47+
print(f"\033[92m» {res}\033[0m")
48+
49+
50+
test()

algoexpert/solution.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# ####. Problem Name Here
2+
# 🟢 Easy
3+
# 🟠 Medium
4+
# 🔴 Hard
5+
# 🟣 Very Hard
6+
#
7+
# https://www.algoexpert.io/questions/problem-name
8+
#
9+
# Tags: ...
10+
11+
import timeit
12+
13+
14+
# This is a template that can be used as the starting point of a
15+
# solution with minimal changes.
16+
class Solution:
17+
def methodCall(self, args: int) -> bool:
18+
pass
19+
20+
21+
def test():
22+
executors = [Solution]
23+
tests = []
24+
for executor in executors:
25+
start = timeit.default_timer()
26+
for _ in range(1):
27+
for col, t in enumerate(tests):
28+
sol = executor()
29+
result = sol.methodCall(t[0])
30+
exp = t[1]
31+
assert result == exp, (
32+
f"\033[93m» {result} <> {exp}\033[91m for"
33+
+ f" test {col} using \033[1m{executor.__name__}"
34+
)
35+
stop = timeit.default_timer()
36+
used = str(round(stop - start, 5))
37+
cols = "{0:20}{1:10}{2:10}"
38+
res = cols.format(executor.__name__, used, "seconds")
39+
print(f"\033[92m» {res}\033[0m")
40+
41+
42+
test()

0 commit comments

Comments
 (0)