Skip to content
This repository has been archived by the owner on Mar 8, 2020. It is now read-only.

Commit

Permalink
12
Browse files Browse the repository at this point in the history
  • Loading branch information
dexhunter committed Jan 13, 2017
1 parent 939c8b3 commit 70329b4
Show file tree
Hide file tree
Showing 9 changed files with 264 additions and 73 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
* __11-6 homophone (sound check)__
* 12-1 letter frequecy
* __12-2 anagram search and sort__
* 12-3 metathesis pairs



Expand Down Expand Up @@ -87,3 +88,9 @@ I was reviewing all the materials from school recently but figured out it could

### _31/12/2016_
The last day of this year, but I am still stuck with a lot of exercises in the book. I don't think I am able to finish in this year. So good luck to next year. 皆さんあけおめ!

### _11/01/2017_
Arrr, back to work.

### _11/01/2017_
Low effiency :/ I did 2 different versions for exercise 12-1, 12-2.
9 changes: 7 additions & 2 deletions TP reading notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,15 @@
# Chapter 11 Dictionaries

* key and values -> key-value pair (aka item)
* associative memories
* string and numbers can always be keys!!!
* keys are immutable
* Tuples can be used as keys if they contain only strings, numbers, or tuples; if a tuple contains any mutable object either directly or indirectly, it cannot be used as a key.
* list CANNOT be keys!!!!

# Chapter 12 Tuples



* The key



9 changes: 9 additions & 0 deletions ch12_ex.py
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)
45 changes: 45 additions & 0 deletions ex12_1_v2.py
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)
24 changes: 12 additions & 12 deletions ex12_2.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# http://stackoverflow.com/questions/8286554/using-python-find-anagrams-for-a-list-of-words

#hughdbrown did an amazing job and I tried to understand his code and found this one is the better so far.
'''http://stackoverflow.com/questions/8286554/using-python-find-anagrams-for-a-list-of-words
# hughdbrown did an amazing job and I tried to understand his code and found this one is the better so far.
'''
from collections import defaultdict

def load_words(filename = "words.txt"):
with open(filename) as f:
for word in f:
yield word.rstrip()
yield word.rstrip() # return a generator

def all_anagram(l):
'''Reads a list and return a set of anagram words
Expand All @@ -16,12 +16,12 @@ def all_anagram(l):
Returns: set
'''
d = defaultdict(list)
d = defaultdict(list) # avoid KeyError in dict()
for word in l:
key = "".join(sorted(word))
d[key].append(word)
signature = "".join(sorted(word)) #sorted: leave the original word untouched
d[signature].append(word)
return d

def print_anagram(l):
d = all_anagram(l)
for key, anagram in d.iteritems():
Expand All @@ -39,10 +39,10 @@ def print_anagram_in_order(l):

for x in new_list:
print x




if __name__ == '__main__':
l = load_words()
# print_anagram(l)
#print_anagram(l)
print_anagram_in_order(l)


44 changes: 44 additions & 0 deletions ex12_2_v2.py
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())
48 changes: 48 additions & 0 deletions ex12_3.py
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())
5 changes: 5 additions & 0 deletions ex12_4.py
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
'''

Loading

0 comments on commit 70329b4

Please sign in to comment.