-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubsegments2.py
executable file
·85 lines (73 loc) · 1.97 KB
/
subsegments2.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python3
"""
Given a paragraph of text, write a program to find the first shortest
sub-segment that contains each of the given k words at least once. A segment is said to be
shorter than other if it contains less number of words.
[Uncomplete BUGGY Version!!]
Makhtar Diouf
$Id$
"""
import sys
# import os
# import math
# main
try:
inp = sys.stdin
parag = inp.readline().strip()
allWords = parag.split(' ')
# Remove non-alpha chars
i = 0
strt = end = x = y = 0
for w in allWords:
j = 0
for c in w:
if not c.isalpha():
w = w.replace(c, "")
print(c, end='')
j += 1
# print(w)
allWords[i] = w.lower()
i += 1
print(allWords)
numWords = int(inp.readline().strip())
print(numWords, " words")
words = []
for c in range(numWords):
words.append(inp.readline().strip().lower())
print(words)
globLen = 10000
emptyTarget = False
cnt = 0
# This would miss words like "program"
# table[target] = allWords.count(target)
table = dict({tgt: 0 for tgt in words})
for w in allWords:
# emptyTarget = all(t == 0 for t in table)
# if not emptyTarget:
# y -= 1
# else:
for tgt in table:
if tgt in w:
table[tgt] += 1
break
print("w", w, ":\t", table)
emptyTarget = all(val == 0 for val in table.values())
if not emptyTarget:
#y -= 1
if (globLen > y - x):
globLen = y - x
strt = x
end = y
tgt = allWords[x]
if table.__contains__(tgt):
table[tgt] -= 1
x += 1
y += 1
if (end > strt):
for i in range(strt, end):
print(allWords[i], end='')
else:
print("NO SUBSEGMENT FOUND", x, y)
except Exception as ex:
print("Error: ", sys.exc_info()[0], ex)
raise