# 列表部分习题

def is_sorted(t):
for i in range(len(t)-1):
if t[i]>t[i+1]:
return False
return True

print is_sorted([1,2,2])
print is_sorted(['b','a'])</span>

def is_anagram(string1,string2):
if len(string1) != len(string2):
return False
for letter in string1:
if letter not in string2:
return False
for letter in string2:
if letter not in string1:
return False
return True

print is_anagram('string','ginrts')
print is_anagram('string','strngi')
print is_anagram('string','strinr')
print is_anagram('string','strin')
</span>

(所谓的)生日悖论：

1.编写一个函数has_duplicates接收一个列表，当其中任何一个元素出现多于一次时返回True。它不应当修改原始列表。

def has_duplicates(t):
s = t[:]
s.sort()
for i in range(len(s)-1):
if s[i] == s[i+1]:
return True
return False

print has_duplicates(['a','b','c','b'])
print has_duplicates(['a','b','c','d'])</span>
2.如果你的班级中有23个学生，那么其中有两人生日相同的几率有多大？你可以通过随机生成23个生日的样本并检查是否有相同的匹配来估计这个几率。提示：可以使用random模块的randint函数来生成随机生日。

import random

def has_duplicates(t):
s = t[:]
s.sort()
for i in range(len(s)-1):
if s[i]== s[i+1]:
return True
return False

def randomDays(n):
t = []
for i in range(n):
day = random.randint(1,365)
t.append(day)
return t

def count_match(students,samples):
count_equal = 0
for i in range(samples):
t = randomDays(students)
if has_duplicates(t):
count_equal += 1
return count_equal

num_students = 23
num_simulations = 1000
count = count_match(num_students,num_simulations)

print 'After %d simulations' % num_simulations
print 'with %d students' % num_students
print 'there were %d simulations with at least one match'% count


After 1000 simulations
with 23 students
there were 492 simulations with at least one match

def remove_duplicates(t):
s = t[:]
s.sort()
i = 0
while i <len(s)-1:
if s[i] == s[i+1]:
del s[i+1]
i = i - 1
i = i + 1
return s

list1 = [0,1,1,1,2,2,3,3]
print 'original lise is ',
print list1
print 'After remove is ',
print remove_duplicates(list1)


original lise is  [0, 1, 1, 1, 2, 2, 3, 3]
After remove is  [0, 1, 2, 3]

import time

def append_me():
t = []
fin = open("words.txt")
for line in fin:
word = line.strip()
t.append(word)
return t

def listplus():
t = []
fin = open("words.txt")
for line in fin:
word = line.strip()
t = t + [word]
return t

start1 = time.time()
t1 = append_me()
end1 = time.time()
time1 = end1 - start1
print "use append method ,the time is ",time1,"seconds"

start2 = time.time()
t2 = listplus()
end2 = time.time()
time2 =end2 - start2
print "use list plus method,the time is",time2,"seconds"


use append method ,the time is  0.106966018677 seconds
use list plus method,the time is 45.1239600182 seconds

def append_me():
t = []
fin = open("words.txt")
for line in fin:
word = line.strip()
t.append(word)
return t

def bisect(t,word):
start_index = 0
end_index = len(t)-1
while start_index <= end_index:
middle = (start_index + end_index) / 2
if word == t[middle]:
return middle
elif word < t[middle]:
end_index = middle - 1
else:
start_index = middle + 1
return None

list1 = append_me()
index = bisect(list1,'genets')
print index
print list1[index]
index2 = bisect(list1,'aa')
print index2
print list1[index2]
index2 = bisect(list1,'zymurgy')
print index2
print list1[index2]


40523
genets
0
aa
113808
zymurgy

def append_me():
t = []
fin = open("words.txt")
for line in fin:
word = line.strip()
t.append(word)
return t

def bisect(t,word):
start_index = 0
end_index = len(t)-1
while start_index <= end_index:
middle = (start_index + end_index) / 2
if word == t[middle]:
return True
elif word < t[middle]:
end_index = middle - 1
else:
start_index = middle + 1
return False

if __name__ == '__main__':
list1 = append_me()


from binary_search import *

def reverse_pair(list1,word):
rev_word = word[::-1]
return bisect(list1,rev_word)

if __name__ == '__main__':
list1 = append_me()

for word in list1:
if reverse_pair(list1,word):
print word,word[::-1]


wop pow
wort trow
worts strow
wos sow
wot tow
wots stow
wow wow
xis six
ya ay
yah hay
yak kay
yam may
yap pay
yaps spay
yar ray
yard dray
yaw way
yaws sway
yay yay
yeh hey

1 编写一个程序找到所有互锁的词。提示：不要穷举所有的词对。

2 能不能找到“三互锁”的单词？也就是，从第一，第二或者第三个字母开始，每第三个字母合起来可以形成一个单词。

from binary_search import *

def interlock(list1, word):
evens = word[::2]
odds = word[1::2]
return bisect(list1, evens) and bisect(list1, odds)

def interlock_general(word_list, word, n=3):
for i in range(n):
inter = word[i::n]
if not bisect(word_list, inter):
return False
return True

if __name__ == '__main__':
list1 = append_me()

for word in list1:
if interlock(list1, word):
print word, word[::2], word[1::2]

#    for word in word_list:
#        if interlock_general(word_list, word, 3):
#            print word, word[0::3], word[1::3], word[2::3]


wooded woe odd
woods wos od
woof wo of
woofs wos of
wooled woe old
woops wos op
woos wo os
wordy wry od
wormy wry om
worry wry or
wound wud on
wounds wud ons
wreath wet rah
wreaths wets rah
wries wis re
wriest wis ret
yaird yid ar
yairds yid ars
yeah ya eh
yean ya en
year ya er
yeas ya es
yuan ya un