How to think like a Computer Scientist: 课后习题第八章

#-------------------------------------------------------------------------------
# Name:        module1
# Purpose:
#
# Author:      penglaixy
#
# Created:     27/07/2013
# Copyright:   (c) penglaixy 2013
# Licence:     <your licence>
#-------------------------------------------------------------------------------
import sys
import random
import string

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 reverse(words):
    length = len(words)
    new_words = ''

    while length > 0:
        length -= 1
        new_words += words[length]

    return new_words

def mirror(words):
    return words+reverse(words)

def remove_letter(char, words):
    new_words = ''
    for ch in words:
        if ch != char:
            new_words += ch
    return new_words

def is_palindrome(words):
    new_words = ''
    length = len(words)
    for i in range(length//2):
        new_words += words[i]
    if length % 2 == 0:
        new_words += reverse(new_words)
    else:
        new_words = new_words + words[length//2] + reverse(new_words)

    return new_words == words

def count(subwords, words):
    count = 0
    substr = ''
    sublen = len(subwords)
    length = len(words)
    index = 0
    while index < length:
        if (index + sublen) <= length:
            if words[index:index+sublen] == subwords:
                count += 1
        else:
            break
        index += 1

    return count

def remove(subwords, words):
    first_index = words.find(subwords)
    last_index = first_index + len(subwords)
    if -1 == first_index:
        return words
    else:
        return words[:first_index] + words[last_index:]

def remove_all(subwords, words):
    while True:
        first_index = words.find(subwords)
        last_index = first_index + len(subwords)
        if -1 == first_index:
            return words
        else:
            words = words[:first_index] + words[last_index:]

def test_suite():
    '''
    Run the suite of tests for code in this module
    '''
    test(reverse('happy') == 'yppah')
    test(reverse('Python') == 'nohtyP')
    test(reverse('') == "")
    test(reverse('a') == 'a')
    test(mirror('good') == 'gooddoog')
    test(mirror('Python') == 'PythonnohtyP')
    test(mirror('') == '')
    test(mirror('a') == 'aa')
    test(remove_letter('a','apple') == 'pple')
    test(remove_letter('a','banana') == 'bnn')
    test(remove_letter('z','banana') == 'banana')
    test(remove_letter('i','Mississippi') == 'Msssspp')
    test(remove_letter('b','') == '')
    test(remove_letter('b','c') == 'c')

    test(is_palindrome('abba'))
    test(not is_palindrome('abab'))
    test(is_palindrome('tenet'))
    test(not is_palindrome('banana'))
    test(is_palindrome('straw warts'))
    test(is_palindrome('a'))
    test(is_palindrome(''))

    test(count('is','Mississippi') == 2)
    test(count('an','banana') == 2)
    test(count('ana','banana') == 2)
    test(count('nana','banana') == 1)
    test(count('nanan','banana') == 0)
    test(count('aaa','aaaaaa') == 4)

    test(remove('an','banana') == 'bana')
    test(remove('cyc','bicycle') == 'bile')
    test(remove('iss','Mississippi') == 'Missippi')
    test(remove('eggs','bicycle') == 'bicycle')

    test(remove_all('an','banana') == 'ba')
    test(remove_all('cyc','bicycle') == 'bile')
    test(remove_all('iss','Mississippi') == 'Mippi')
    test(remove_all('eggs','bicycle') == 'bicycle')

def find2(strings, ch, index=0):
    ix = index
    length = len(strings)
    while ix < length:
        if strings[ix] == ch:
            return ix
        else:
            ix += 1
    return -1

def count_letters(strings, letter):
    count = 0
    index = 0
    while index < len(strings):
        index = find2(strings, letter, index)
        if -1 != index:
            count += 1
            print "find one letter in  index {0}".format(index)
            index += 1
        else:
            break
    return count

def modify_my_poem(poem):
    new_string = ''
    count_words = 0
    count_char_inwords = 0
    for words in poem:
        if words not in string.punctuation:
            new_string += words

    words_list = new_string.split()
    count_words = len(words_list)
    for words in words_list:
        if -1 != find2(words, 'e'):
            count_char_inwords += 1

    print "Your test contains {0} words, of which {1} ({2:.1f}%) \
contain an 'e'.".format(count_words, count_char_inwords, (count_char_inwords*100.0/count_words))
    return

def print_table():
    layout = "{0:>4}{1:>4}{2:>4}{3:>4}{4:>4}{5:>4}{6:>4}{7:>4}{8:>4}{9:>4}{10:>4}{11:>4}"

    print (layout.format("i*1","i*2",'i*3','i*4','i*5','i*6','i*7','i*8','i*9','i*10','i*11','i*12'))
    for i in range(1,13):
        print(layout.format(i*1,i*2,i*3,i*4,i*5,i*6,i*7,i*8,i*9,i*10,i*11,i*12))

    return

def main():

    print "'banana' has numbers of letter 'n' is {0}".format(count_letters('banana','v'))
    print ''
    favourate_poem = '''
no longer a baby,
but just not,
you held yourself up,
by the kitchen window,
backyards Brooklyn;

you said,
write me a poem,
called a map of the winds;

already you believed,
there was something I could do;

write me a poem;

my happy sense,
you believed I could,
even if this one,
wasn't mine to write.
'''
    modify_my_poem(favourate_poem)
    print ''
    print_table()
    print ''
    test_suite()
if __name__ == '__main__':
    main()

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值