forked from adnanaziz/EPIJudge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
is_string_decomposable_into_words.py
66 lines (53 loc) · 2.34 KB
/
is_string_decomposable_into_words.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import functools
from typing import List, Set
from test_framework import generic_test
from test_framework.test_failure import TestFailure
from test_framework.test_utils import enable_executor_hook
def decompose_into_dictionary_words(domain: str,
dictionary: Set[str]) -> List[str]:
# When the algorithm finishes, last_length[i] != -1 indicates domain[:i +
# 1] has a valid decomposition, and the length of the last string in the
# decomposition is last_length[i].
last_length = [-1] * len(domain)
for i in range(len(domain)):
# If domain[:i + 1] is a dictionary word, set last_length[i] to the
# length of that word.
if domain[:i + 1] in dictionary:
last_length[i] = i + 1
continue
# If domain[:i + 1] is not a dictionary word, we look for j < i such
# that domain[: j + 1] has a valid decomposition and domain[j + 1:i + 1]
# is a dictionary word. If so, record the length of that word in
# last_length[i].
for j in range(i):
if last_length[j] != -1 and domain[j + 1:i + 1] in dictionary:
last_length[i] = i - j
break
decompositions = []
if last_length[-1] != -1:
# domain can be assembled by dictionary words.
idx = len(domain) - 1
while idx >= 0:
decompositions.append(domain[idx + 1 - last_length[idx]:idx + 1])
idx -= last_length[idx]
decompositions = decompositions[::-1]
return decompositions
@enable_executor_hook
def decompose_into_dictionary_words_wrapper(executor, domain, dictionary,
decomposable):
result = executor.run(
functools.partial(decompose_into_dictionary_words, domain, dictionary))
if not decomposable:
if result:
raise TestFailure('domain is not decomposable')
return
if any(s not in dictionary for s in result):
raise TestFailure('Result uses words not in dictionary')
if ''.join(result) != domain:
raise TestFailure('Result is not composed into domain')
if __name__ == '__main__':
exit(
generic_test.generic_test_main(
'is_string_decomposable_into_words.py',
'is_string_decomposable_into_words.tsv',
decompose_into_dictionary_words_wrapper))