基于python的完全数之TDD版本

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Filename: tdd_perfect_number.py

from math import sqrt, floor

'''
' 创建一个完全数查找程序
'
' 完全数指其真因子相加等于数字本身的数字。
' 例如,6 是一个完全数,因为 6 的因子(不包括 6 本身)是 1、2 和 3,
' 而 1 + 2 + 3 = 6。
' 更规则的完全数定义是因子(不包括该数字本身)之和等于该数字的数字。
' 在我的示例中,计算结果是 1 + 2 + 3 +6 - 6 = 6。
''' 

'''
' 1.所求数字的因子
' 2.确定某个数是不是因子
' 3.(真)因子之和
'''

def is_factor(factor, num):
    return (num % factor) == 0

def factors_for(num):
    return [num,]

def init_factors(num):
    return [1, num]

def cal_factors(num, factors):
    factors += init_factors(num)
    for i in range(2, int(floor(sqrt(num))) + 1):
        if is_factor(i, num):
            add_factor(i, factors, num)

def get_factors(num):
    factors = []
    if num > 0:
        cal_factors(num, factors)
    return factors

def add_factor(factor, factors, num):
    factors.append(factor)
    factors.append(num / factor)
    return factors

def is_perfect(num):
    real_sum = sum(get_factors(num)) - num
    return real_sum == num


 

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Filename: test_tdd_perfect_number.py

import sys
import unittest
from tdd_perfect_number import *

class PerfectNumberTestCase(unittest.TestCase):
    def setUp(self):
        self.PERFECT_NUMS = [6, 28, 496, 8128, 33550336]
        return

    def tearDown(self):
        return

    def test_1_is_factor_of_10(self):
        assert(is_factor(1, 10))

    def test_factors_for_1(self):
        expected = [1]
        self.assertEqual(factors_for(1), expected)

    def test_factors_for_6(self):
        expected = set([1, 2, 3, 6])
        self.assertEqual(set(get_factors(6)), expected)

    def test_add_factors(self):
        expected = set([1, 2, 3, 6])
        factors = []
        add_factor(1, factors, 6)
        add_factor(2, factors, 6)
        self.assertEqual(set(factors), expected)
    
    def test_factors_for_100(self):
        expected = set([1, 100, 2, 50, 4, 25, 5, 20, 10])
        self.assertEqual(set(get_factors(100)), expected)
    
    def test_factors_for_negative(self):
        expected = set([])
        self.assertEqual(set(get_factors(-20)), expected)

    '''    
    def test_factors_for_max_int(self):
        expected = set([1, 2147483647])
        self.assertEqual(set(get_factors(sys.maxint)), expected)  
    '''
    def test_perfection(self):
        for num in self.PERFECT_NUMS:
            self.assertTrue(is_perfect(num))

    def test_init_factors(self):
        self.assertEqual(init_factors(6), [1, 6])

    def test_cal_factors(self):
        
    def test_a_bunch_of_numbers(self):
        for i in range(2, 10000):
            if self.PERFECT_NUMS.count(i) > 0:
                self.assertTrue(is_perfect(i))
            else:
                self.assertFalse(is_perfect(i))

if __name__ == "__main__":
    unittest.main()


测试结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值