#!/usr/bin/python3 import sys,re if sys.version[0:3] < "3.6": from collections import OrderedDict anagrams = OrderedDict() else: anagrams = {} hashd = {'a':7,'b':61,'c':29,'d':41,'e':2,'f':67,'g':53,'h':47,'i':3,'j':101,'k':73,'l':23,'m':43,'n':11,'o':13,'p':37,'q':97,'r':17,'s':5,'t':19,'u':31,'v':71,'w':79,'x':83,'y':59,'z':89} f = open('/usr/share/dict/british-english-insane','rt') dictionary = f.read().split('\n') f.close() check = re.compile('^[a-z]+$') for word in dictionary: if check.match(word): keyl = list(word) key = 1 for ch in keyl: key *= hashd[ch] if key in anagrams: anagrams[key].append(word) else: anagrams[key] = [word] output = '' for v in anagrams.values(): if len(v) > 1: op = ', '.join(v) + '\n' output += op.replace(',',':',1) print(output,end='')