How to think like a Computer Scientist: 课后习题第七章 1-10

#-------------------------------------------------------------------------------
# Name:        module1
# Purpose:
#
# Author:      penglaixy
#
# Created:     27/07/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 count_odd(list_num):
    count = 0
    for i in list_num:
        if i % 2 == 1:
            count += 1
    return count

def sum_even(list_num):
    total = 0
    for i in list_num:
        if i % 2 == 0:
            total += i
    return total

def sum_neg(list_num):
    total = 0
    for i in list_num:
        if i < 0:
            total += i
    return total

def sum_word5(list_word):
    count = 0
    for i in list_word:
        if len(i) == 5:
            count += 1
    return count

def sum_until_even(list_num):
    total = 0
    for i in list_num:
        if i % 2 == 0:
            break
        else:
            total += i
    return total

def count_befor_sam(list_word):
    count = 0
    for i in list_word:
        count += 1
        if i == 'sam':
            break
    return count

def caculate_square_root(n):
    '''
    better = (approx + n/approx)/2
    '''
    approx = n/2.0

    while True:
        better = (approx + n/approx)/2.0
        print 'This time better is ', better
        if abs(better - approx) < 0.001:
            break
        approx = better

    return better

def is_prime(n):
    for i in range(2, n//2 + 1):
        if n % i == 0:
            return False
    return True

def print_triangular_numbers(n):
    total = 0
    for i in range(1,n+1):
        total += i
    print n, '\t', total

def test_suite():
    '''
    Run the suite of tests for code in this module
    '''
    test(count_odd([1,2,3,4,5,6,7]) == 4)
    test(count_odd([2,4,6,8,10]) == 0)

    test(sum_even([1,2,3,4,5,6,7]) == 12)
    test(sum_even([1,3,5,7]) == 0)

    test(sum_neg([-2,-4,-5,-6]) == -17)
    test(sum_neg([1,2,3,4,5]) == 0)

    test(sum_word5(['list','word','world','apple']) == 2)
    test(sum_word5(['no','yes','live','home']) == 0)

    test(sum_until_even([2,3,4,5,6,7]) == 0)
    test(sum_until_even([1,3,5,7,9,11]) == 36)

    test(count_befor_sam(['sam','he','she']) == 1)
    test(count_befor_sam(['he','sam','she']) == 2)
    test(count_befor_sam(['he','she','they']) == 3)

    caculate_square_root(25)

    print_triangular_numbers(1)
    print_triangular_numbers(2)
    print_triangular_numbers(3)
    print_triangular_numbers(4)
    print_triangular_numbers(5)

    test(is_prime(2))
    test(is_prime(3))
    test(is_prime(11))
    test(not is_prime(35))
    test(is_prime(19911121))

def main():
    test_suite()

if __name__ == '__main__':
    main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
《如何像计算机科学家一样思考-用Python 3学习》是一本通过学习Python编程语言来培养计算机科学思维的书籍。这本书的目的是帮助读者通过编程的方式培养解决问题和抽象思维的能力,并通过实际的编程练习来加深对计算机科学概念的理解。 首先,这本书强调了计算机科学的核心思维方式,即问题解决和抽象思维。通过分析和解决一系列问题,读者将逐渐习得理解问题本质和设计解决方案的能力。此外,书中提出了抽象思维的重要性,即将复杂问题简化成更易于理解和处理的部分。 其次,这本书主要使用Python编程语言作为学习工具。Python是一种易于学习和使用的语言,它具有清晰的语法和广泛的应用领域。通过使用Python编写各种类型的程序,读者将学会如何使用计算机语言表示和解决问题。 在学习过程中,这本书强调了实践的重要性。读者将通过大量的编程练习来巩固所学知识,并在每章末尾的习题中应用所掌握的概念。这种通过实际操作来加深对计算机科学概念的理解,有助于读者熟练掌握编程技能。 最后,这本书还鼓励读者发展计算机科学的准则和最佳实践。它介绍了一些编程经验和原则,例如代码可读性、模块化设计和文档编写等,帮助读者在编程过程中培养规范和良好的习惯。 总而言之,《如何像计算机科学家一样思考-用Python 3学习》是一本通过学习Python编程语言来培养计算机科学思维的书籍。它强调问题解决和抽象思维,教授编程技能,并鼓励读者养成良好的编程习惯和思维方式。通过阅读和实践,读者将能够更好地理解和应用计算机科学的概念。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值