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
9 changed files
with
264 additions
and
73 deletions.
There are no files selected for viewing
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,9 @@ | ||
def sumall(*args): | ||
# use of gather opeartion | ||
# on page 142 | ||
sum_result = 0 | ||
for i in range(len(args)): | ||
sum_result = sum_result + args[i] | ||
return sum_result | ||
|
||
print sumall(1,2,3,4) |
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,45 @@ | ||
#version 2 for exercise 1 in chapter 12 | ||
|
||
def map_dict(s): | ||
''' | ||
Returns a dictionary mapping each letter with corresponding frequency | ||
s: string | ||
''' | ||
d = {} | ||
for i in s: | ||
d[i] = d.get(i, 0) + 1 | ||
return d | ||
|
||
def most_frequent(s): | ||
''' | ||
Sorts the letters in reverse order of frequence | ||
s: string | ||
Retruns a list of letters | ||
''' | ||
d = map_dict(s) | ||
|
||
l = [] | ||
|
||
for x, f in d.items(): | ||
l.append((f,x)) | ||
|
||
l.sort(reverse = True) | ||
|
||
result = [] | ||
|
||
for f, x in l: | ||
result.append(x) | ||
|
||
return result | ||
|
||
|
||
|
||
if __name__ == '__main__': | ||
# emma.txt is provided from author's code | ||
with open('emma.txt', 'r') as fin: | ||
string = fin.read() | ||
print most_frequent(string) |
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,44 @@ | ||
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(s): | ||
''' | ||
s: string | ||
''' | ||
d = defaultdict(list) | ||
for i in s: | ||
signature = ''.join(sorted(i)) # siganature is a after-sort string of chars in the word | ||
d[signature].append(i) | ||
|
||
return d | ||
|
||
def print_anagram_in_order(s): | ||
d = all_anagram(s) | ||
l = [] | ||
for signature, words in d.items(): | ||
if len(words) > 1: | ||
l.append((len(words),words)) | ||
l.sort(reverse=True) | ||
for x in l: | ||
print x | ||
|
||
def select_bingo(s, n=8): | ||
d = all_anagram(s) | ||
l = {} | ||
for signature, words in d.iteritems(): | ||
if len(words) > 1: | ||
if len(signature) == n: | ||
l[signature] = words | ||
res = [] | ||
for k, v in l.iteritems(): | ||
res.append((len(v),v)) | ||
|
||
for x in sorted(res, reverse=False): | ||
print x | ||
|
||
#print_anagram_in_order(load_words()) | ||
select_bingo(load_words()) |
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,48 @@ | ||
# mathesis pair | ||
# my idea: to start the pair of word need to be anagram (consist of all the same letter) | ||
# secondly only 1 char's position can be changed. | ||
|
||
|
||
''' | ||
According to author's answer it is easier to find the distance between a pair of word! | ||
''' | ||
from collections import defaultdict | ||
|
||
def load_words(filename = "words.txt"): | ||
with open(filename) as fin: | ||
for line in fin: | ||
yield line.rstrip() | ||
|
||
def all_anagram(s): | ||
''' | ||
s: string | ||
''' | ||
d = defaultdict(list) | ||
for i in s: | ||
signature = ''.join(sorted(i)) # siganature is a after-sort string of chars in the word | ||
d[signature].append(i) | ||
|
||
for k, v in d.items(): | ||
if len(v) == 1: | ||
del d[k] | ||
|
||
return d | ||
|
||
def pair_distance(a, b): | ||
# credit to http://www.greenteapress.com/thinkpython/code/metathesis.py | ||
assert len(a) == len(b) # if not equal raise AssertionError | ||
ctr = 0 | ||
for c1, c2 in zip(a,b): # zip(a,b) returns a tuple for each corresponding char pair eg. "apple" & "paple" returns [('a', 'p'), ('p', 'a'), ('p', 'p'), ('l', 'l'), ('e', 'e')] | ||
if c1 != c2: | ||
ctr += 1 | ||
return ctr | ||
|
||
def find_metathesis(s): | ||
d = all_anagram(s) | ||
for v in d.itervalues(): | ||
for a in v: | ||
for b in v: | ||
if a < b and pair_distance(a,b) == 2: | ||
print a, b | ||
|
||
find_metathesis(load_words()) |
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,5 @@ | ||
def child_of_word(word): | ||
''' | ||
Returns a list of words by removing one char | ||
''' | ||
|
Oops, something went wrong.