-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
64 lines (56 loc) · 1.32 KB
/
main.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
from flask import Flask
from flask import render_template
from flask import request
import detect_mobile
app = Flask(__name__)
@app.route("/")
def index():
page = 'main.html'
if detect_mobile.is_mobile == True:
page = 'mobile.html'
return render_template(page)
@app.route("/results", methods=['POST', 'GET'])
def results():
words = []
word = request.args.get('word')
length = request.args.get('len')
if(length.isdigit()):
length = int(length)
else:
length = 0
words = get(word, length)
return render_template('list.html', words=words, word=word, length=length)
def cntchr(word):
r = {}
for c in word:
if c in r:
r[c] = r[c] + 1
else:
r[c] = 1
return r
dic, ana = [], []
def loadwords():
br = open('dic.txt')
for line in br:
line = line.strip()
dic.append(line)
ana.append(''.join(sorted(line)))
def get(word, length):
res = []
str = word.replace(' ', '')
str = ''.join(sorted(str)).lower()
strdic = cntchr(str)
for i in range(len(ana)):
sdic, has = strdic.copy(), True
for c in ana[i]:
if c in sdic and sdic[c] > 0:
sdic[c] = sdic[c] - 1
else:
has = False
break
if has and (length == 0 or len(dic[i]) == length):
res.append(dic[i])
return res
loadwords()
if __name__ == "__main__":
app.run(debug=True)