-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordListOneLook.py
executable file
·92 lines (71 loc) · 2.03 KB
/
WordListOneLook.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
86
87
88
89
90
91
92
import sys
def patternmatch(pat, word):
pat = pat.upper()
word = word.upper()
# check for pattern length
if '*' not in pat and len(pat) != len(word):
return False
# check for double *'s
l = 0
while l < len(pat):
if l > 0:
if pat[l] == '*' and pat[l - 1] == '*':
pat = pat[0:l] + pat[l + 1::]
l -= 1
l += 1
return submatch(pat, word)
def firstalphaindex(word):
for i in range(len(word)):
if word[i].isalpha():
return i
return -1
def indices(word, char):
inds = []
for i in range(len(word)):
if word[i] == char:
inds.append(i)
return inds
def submatch(pat, word):
if pat == '*':
return True
if len(pat) == 0:
return len(word) == 0
if len(word) == 0:
return False
if pat == word:
return True
# process *'s
if pat[0] == '*':
ind = firstalphaindex(pat)
if ind == -1:
return True
else:
if pat[ind] in word[ind - 1::]:
inds = indices(word, pat[ind])
return True in [submatch(pat[ind::], word[i::]) for i in inds]
else:
return False
# process wildcards
if pat[0].isalpha() and pat[0] != word[0]:
return False
return submatch(pat[1::], word[1::])
def onelook(args):
if len(args) != 3:
return 'Error: input wordlist filepath, followed by a space and then your search query.'
wordlist = args[1]
pattern = args[2]
words = []
try:
words = open(wordlist).read().splitlines()
words = [w[(w.index(' ') + 1)::] if ' ' in w else w for w in words]
except IOError:
return 'Error: file not found'
results = []
for w in words:
if patternmatch(pattern, w):
results.append(w)
if len(results) == 0:
return 'I have no words...'
else:
return 'I found {} results:\n'.format(len(results)) + '\n'.join(results)
print(onelook(sys.argv))