mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-17 14:58:10 +00:00
Initial
This commit is contained in:
parent
1376e3c3b9
commit
e51406583c
39
other/word_patterns.py
Normal file
39
other/word_patterns.py
Normal file
|
@ -0,0 +1,39 @@
|
|||
import pprint, time
|
||||
|
||||
def getWordPattern(word):
|
||||
word = word.upper()
|
||||
nextNum = 0
|
||||
letterNums = {}
|
||||
wordPattern = []
|
||||
|
||||
for letter in word:
|
||||
if letter not in letterNums:
|
||||
letterNums[letter] = str(nextNum)
|
||||
nextNum += 1
|
||||
wordPattern.append(letterNums[letter])
|
||||
return '.'.join(wordPattern)
|
||||
|
||||
def main():
|
||||
startTime = time.time()
|
||||
allPatterns = {}
|
||||
|
||||
fo = open('Dictionary.txt')
|
||||
wordList = fo.read().split('\n')
|
||||
fo.close()
|
||||
|
||||
for word in wordList:
|
||||
pattern = getWordPattern(word)
|
||||
|
||||
if pattern not in allPatterns:
|
||||
allPatterns[pattern] = [word]
|
||||
else:
|
||||
allPatterns[pattern].append(word)
|
||||
|
||||
fo = open('Word Patterns.txt', 'w')
|
||||
fo.write(pprint.pformat(allPatterns))
|
||||
fo.close()
|
||||
totalTime = round(time.time() - startTime, 2)
|
||||
print('Done! [', totalTime, 'seconds ]')
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Loading…
Reference in New Issue
Block a user