This repository has been archived by the owner on Mar 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
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
Showing
51 changed files
with
13,657 additions
and
7 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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,101 @@ | ||
"""This module contains a code example related to | ||
Think Python, 2nd Edition | ||
by Allen Downey | ||
http://thinkpython2.com | ||
Copyright 2015 Allen Downey | ||
License: http://creativecommons.org/licenses/by/4.0/ | ||
This file is used in exercise 14-2 | ||
""" | ||
|
||
from __future__ import print_function, division | ||
|
||
|
||
def signature(s): | ||
"""Returns the signature of this string. | ||
Signature is a string that contains all of the letters in order. | ||
s: string | ||
""" | ||
# TODO: rewrite using sorted() | ||
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) | ||
|
||
# TODO: rewrite using defaultdict | ||
if t not in d: | ||
d[t] = [word] | ||
else: | ||
d[t].append(word) | ||
return d | ||
|
||
|
||
def print_anagram_sets(d): | ||
"""Prints the anagram sets in d. | ||
d: map from words to list of their anagrams | ||
""" | ||
for v in d.values(): | ||
if len(v) > 1: | ||
print(len(v), v) | ||
|
||
|
||
def print_anagram_sets_in_order(d): | ||
"""Prints the anagram sets in d in decreasing order of size. | ||
d: map from words to list of their anagrams | ||
""" | ||
# make a list of (length, word pairs) | ||
t = [] | ||
for v in d.values(): | ||
if len(v) > 1: | ||
t.append((len(v), v)) | ||
|
||
# sort in ascending order of length | ||
t.sort() | ||
|
||
# print the sorted list | ||
for x in t: | ||
print(x) | ||
|
||
|
||
def filter_length(d, n): | ||
"""Select only the words in d that have n letters. | ||
d: map from word to list of anagrams | ||
n: integer number of letters | ||
returns: new map from word to list of anagrams | ||
""" | ||
res = {} | ||
for word, anagrams in d.items(): | ||
if len(word) == n: | ||
res[word] = anagrams | ||
return res | ||
|
||
|
||
if __name__ == '__main__': | ||
anagram_map = all_anagrams('words.txt') | ||
print_anagram_sets_in_order(anagram_map) | ||
|
||
eight_letters = filter_length(anagram_map, 8) | ||
print_anagram_sets_in_order(eight_letters) | ||
|
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
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,18 @@ | ||
''' | ||
According to http://stackoverflow.com/questions/3900054/python-strip-multiple-characters, | ||
string.translate(string.maketrans()) is the fastest way to remove undesired chars | ||
maketrans syntax: str.maketrans(intab, outtab) | ||
''' | ||
import string | ||
|
||
def read_file(filename): | ||
'''Reads a file and return stripped strings | ||
filename: a string | ||
''' | ||
remove_char = string.punctuation + string.whitespace | ||
with open(filename) as fin: | ||
for line in fin: | ||
yield line.translate(line.maketrans("", "", ), remove_char).lower() | ||
|
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,19 @@ | ||
from string import maketrans | ||
|
||
def sed(s, replace_string, input, output): | ||
fin = open(input, 'r') | ||
fout = open(output, 'w') | ||
|
||
trantab = maketrans(s, replace_string) # translate table | ||
|
||
|
||
for word in fin: | ||
re_word = word.translate(trantab) | ||
fout.write(re_word) | ||
|
||
fin.close() | ||
fout.close() | ||
|
||
|
||
if __name__ == '__main__': | ||
sed('p', 'q', 'test.txt', 'output.txt') #success with this one |
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,47 @@ | ||
''' pickle is GREAT for this saving specified data structure task. | ||
''' | ||
import pickle | ||
from collections import defaultdict | ||
|
||
def load_words(filename = "words.txt"): | ||
with open(filename) as f: | ||
for word in f: | ||
yield word.rstrip() # return a generator | ||
|
||
def all_anagram(l): | ||
'''Reads a list and return a set of anagram words | ||
l: list | ||
Returns: set | ||
''' | ||
d = defaultdict(list) # avoid KeyError in dict() | ||
for word in l: | ||
signature = "".join(sorted(word)) #sorted: leave the original word untouched | ||
d[signature].append(word) | ||
|
||
for k, v in d.items(): #remove d[k] if there is only one value corresponding to the key which means there is no anagram for the word | ||
if len(v) == 1: | ||
del d[k] | ||
|
||
return d | ||
|
||
def save_dict(d): | ||
with open('shelf.pkl', 'wb') as f: #wb for write byte | ||
pickle.dump(d, f, pickle.HIGHEST_PROTOCOL) | ||
|
||
def look_up_dict(word): | ||
#look up a word and return its anagram in the 'shelf' | ||
with open('shelf.pkl', 'rb') as f: #rb for read byte | ||
d = pickle.load(f) | ||
for k, v in d.iteritems(): | ||
for i in v: | ||
if i == word: | ||
return v | ||
|
||
if __name__ == '__main__': | ||
books = all_anagram(load_words()) | ||
save_dict(books) | ||
print '' | ||
print look_up_dict('tired') | ||
print look_up_dict('cosets') |
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,4 @@ | ||
Aqqle | ||
Banana | ||
Cheery | ||
qitaya |
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,7 @@ | ||
# change the name to fit Github listing convention | ||
import os | ||
|
||
for file in os.listdir(): | ||
if file.startswith(("ex3", "ex4", "ex5", "ex6", "ex7", "ex8", "ex9")): | ||
os.rename(file, file.replace("ex", "ex0")) | ||
|
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,4 @@ | ||
Apple | ||
Banana | ||
Cheery | ||
pitaya |