-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path14.2.py
More file actions
66 lines (58 loc) · 1.65 KB
/
14.2.py
File metadata and controls
66 lines (58 loc) · 1.65 KB
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
#coding:utf-8
from random import *
import random
import time
import bisect
from bisect import bisect_left
import os
import shelve
import matplotlib.pyplot as pyplot
from string import *
print (os.path.abspath("words.txt"))
"""
写一个模块,导入 anagram_sets 然后提供两个函数:store_anagrams 可以把相同字母异序
词词典存储到一个『shelf』;read_anagrams 可以查找一个词,返回一个由其相同字母异序
词 组成的列表。
"""
fin = open('C:\Users\LzyRapx\PycharmProjects\untitled\words.txt')
fin2 = open("C:\Users\LzyRapx\PycharmProjects\untitled\kamasutra.txt")
def signature(s):
"""
Returns the signature of this string.
Signature is a string that contains all of the letters in order.
s: string
"""
t = list(s)
t.sort()
t = ''.join(t)
return t
def all_anagrams(filename):
"""
Finds all anagrams in a list of words.
filename: string filename of the word list
Returns: a map from each word to a list of its anagrams.
"""
d = {}
for line in open(filename):
word = line.strip().lower()
t = signature(word)
if t not in d:
d[t] = [word]
else:
d[t].append(word)
return d
def store_anagrams(d):
db = shelve.open('anagram_database.db')
for key, value in d.items():
db[key] = value
db.close()
def read_anagrams(word):
db = shelve.open('anagram_database.db')
word = signature(word)
try:
return db[word]
except:
return []
d = all_anagrams('words.txt')
store_anagrams(d)
print read_anagrams('post')