How to think like a Computer Scientist: 课后习题第十四章1

#-------------------------------------------------------------------------------
# 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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值