How to think like a Computer Scientist: 课后习题第十四章1
2013-08-31 19:27
423 查看
#------------------------------------------------------------------------------- # 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: 课后习题第十四章4
- How to think like a Computer Scientist: 课后习题第十四章5
- How to think like a Computer Scientist: 课后习题第四章 10
- How to think like a Computer Scientist: 课后习题第六章 6-12
- How to think like a Computer Scientist: 课后习题第二十一章1-5
- How to think like a Computer Scientist: 课后习题第六章 13-19
- How to think like a Computer Scientist: 课后习题第十七章2
- How to think like a Computer Scientist: 课后习题第三章 12题
- How to think like a Computer Scientist: 课后习题第十八章5-8
- How to think like a Computer Scientist: 课后习题第六章 1-5
- How to think like a Computer Scientist: 课后习题第九章 第2题
- How to think like a Computer Scientist: 课后习题第十八章3
- How to think like a computer scientist: 第四章 课后习题 1
- How to think like a Computer Scientist: 课后习题第九章 第3题
- How to think like a Computer Scientist: 课后习题第十七章3
- How to think like a Computer Scientist: 课后习题第四章 2题
- How to think like a Computer Scientist: 课后习题第七章 14-17
- How to think like a Computer Scientist: 课后习题第九章 第4题
- How to think like a Computer Scientist: 课后习题第十二章 7、8
- How to think like a Computer Scientist: 课后习题第十八章9-11