forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1376e3c
commit e514065
Showing
3 changed files
with
39 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |