#-------------------------------------------------------------------------------
# Name: module1
# Purpose:
#
# Author: penglaixy
#
# Created: 31/08/2013
# Copyright: (c) penglaixy 2013
# Licence: <your licence>
#-------------------------------------------------------------------------------
import sys
def test(did_pass):
'''print the result of a test '''
linenum=sys._getframe(1).f_lineno
if did_pass:
msg = "Test at line{0} ok".format(linenum)
else:
msg = "Test at line{0} failed".format(linenum)
print msg
def in_second_not_first(vocab, wds):
result = []
index_one = 0
index_two = 0
length_one = len(vocab)
length_two = len(wds)
while True:
if index_two == length_two:
return result
if index_one == length_one:
result.extend(wds[index_two:])
return result
if vocab[index_one] > wds[index_two]:
result.append(wds[index_two])
index_two += 1
elif vocab[index_one] == wds[index_two]:
index_one += 1
index_two += 1
else:
index_one += 1
def in_first_not_second(vocab, wds):
result = []
index_one = 0
index_two = 0
length_one = len(vocab)
length_two = len(wds)
while True:
if index_two == length_two:
result.extend(vocab[index_one:])
return result
if index_one == length_one:
return result
if vocab[index_one] > wds[index_two]:
index_two += 1
elif vocab[index_one] == wds[index_two]:
index_one += 1
index_two += 1
else:
result.append(vocab[index_one])
index_one += 1
def in_both_first_and_second(vocab, wds):
result = []
index_one = 0
index_two = 0
length_one = len(vocab)
length_two = len(wds)
while True:
if index_two == length_two or index_one == length_one:
return result
if vocab[index_one] > wds[index_two]:
index_two += 1
elif vocab[index_one] == wds[index_two]:
result.append(wds[index_two])
index_one += 1
index_two += 1
else:
index_one += 1
def in_either_one(vocab, wds):
result = []
index_one = 0
index_two = 0
length_one = len(vocab)
length_two = len(wds)
while True:
if index_two == length_two:
result.extend(vocab[index_one:])
return result
if index_one == length_one:
result.extend(wds[index_two:])
return result
if vocab[index_one] > wds[index_two]:
result.append(wds[index_two])
index_two += 1
elif vocab[index_one] == wds[index_two]:
index_one += 1
index_two += 1
else:
result.append(vocab[index_one])
index_one += 1
def bagdiff(vocab,wds):
result = []
index_one = 0
index_two = 0
length_one = len(vocab)
length_two = len(wds)
while True:
if index_two == length_two:
result.extend(vocab[index_one:])
return result
if index_one == length_one:
return result
if vocab[index_one] > wds[index_two]:
index_two += 1
elif vocab[index_one] == wds[index_two]:
index_one += 1
index_two += 1
else:
result.append(vocab[index_one])
index_one += 1
def main():
xs = [1,3,5,7,9,11,13,15,17,19]
ys = [4,8,12,16,20,24]
zs = xs + ys
zs.sort()
test(in_second_not_first(xs,ys) == ys)
test(in_second_not_first(ys,xs) == xs)
test(in_first_not_second(xs,ys) == xs)
test(in_first_not_second(ys,xs) == ys)
test(in_both_first_and_second(xs,ys) == [])
test(in_both_first_and_second(ys,xs) == [])
test(in_either_one(xs,ys) == zs)
test(in_either_one(ys,xs) == zs)
test(bagdiff([5,7,11,11,11,12,13],[7,8,11]) == [5,11,11,12,13])
xs = [1,3,4,5,7,8,9,11,13,15,17,19]
ys = [4,5,7,8,12,16,20,24]
test(in_second_not_first(xs,ys) == [12,16,20,24])
test(in_second_not_first(ys,xs) == [1,3,9,11,13,15,17,19])
test(in_first_not_second(xs,ys) == [1,3,9,11,13,15,17,19])
test(in_first_not_second(ys,xs) == [12,16,20,24])
test(in_both_first_and_second(xs,ys) == [4,5,7,8])
test(in_both_first_and_second(ys,xs) == [4,5,7,8])
test(in_either_one(xs,ys) == [1,3,9,11,12,13,15,16,17,19,20,24])
test(in_either_one(ys,xs) == [1,3,9,11,12,13,15,16,17,19,20,24])
pass
if __name__ == '__main__':
main()
How to think like a Computer Scientist: 课后习题第十四章1
最新推荐文章于 2013-09-15 16:17:11 发布