Squares with unit tests

Write a function that returns a list of numbers, such that xi=i**2, for 1≤i≤n. Make sure it handles the case where n<1 by raising a ValueError.


In [42]:
import unittest

def squares(n):
    if n < 1:
        raise ValueError
    sq_list = [i**2 for i in range(1,n+1)]
    print n, '-->', sq_list
    return sq_list

class SquaresTest(unittest.TestCase):
    def test_neg(self):
        self.failUnlessRaises(ValueError, squares, -4)
    def test_zero(self):
        self.failUnlessRaises(ValueError, squares, 0)
    def test_one(self):
        self.failUnless([1] == squares(1))
    def test_two(self):
        self.failUnless([1,4] == squares(2))
    def test_ten(self):
        self.failUnless([1,4, 9, 16, 25, 36, 49, 64, 81, 100] == squares(10))
    def test_eleven(self):
        self.failUnlessEqual([1,4, 9, 16, 25, 36, 49, 64, 81, 100, 121], squares(11))
        
squares_suite = unittest.TestLoader().loadTestsFromTestCase(SquaresTest)
unittest.TextTestRunner().run(squares_suite)


......
11 --> [1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121]
1 --> [1]
10 --> [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
2 --> [1, 4]
----------------------------------------------------------------------
Ran 6 tests in 0.003s

OK
Out[42]:
<unittest.runner.TextTestResult run=6 errors=0 failures=0>

Sum the squares

Using your squares function, write a function that computes the sum of the squares of the numbers from 1 to n. Your function should call the squares function -- it should NOT reimplement its functionality.


In [43]:
import unittest

def sum_squares(n):
    sum = 0
    for square in squares(n):
        sum += square
    print n,'--> sum: %s' % sum
    return sum

class SumSquaresTest(unittest.TestCase):
    def test_ss_one(self):
        self.failUnlessEqual(1, sum_squares(1))
    def test_ss_two(self):
        self.failUnlessEqual(5, sum_squares(2))
    def test_ss_ten(self):
        self.failUnlessEqual(385, sum_squares(10))
    def test_ss_eleven(self):
        self.failUnlessEqual(506, sum_squares(11))

ss_suite = unittest.TestLoader().loadTestsFromTestCase(SumSquaresTest)
unittest.TextTestRunner().run(ss_suite)


....
11 --> [1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121]
11 --> sum: 506
1 --> [1]
1 --> sum: 1
10 --> [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
10 --> sum: 385
2 --> [1, 4]
2 --> sum: 5
----------------------------------------------------------------------
Ran 4 tests in 0.002s

OK
Out[43]:
<unittest.runner.TextTestResult run=4 errors=0 failures=0>

Fibonacci Recursion


In [48]:
calls = 0
def fib(x, i):
    '''
    Recursively determine the xth fibonacci number.
    i is used only for the indentation which represents the recursive
    depth.
    '''
    global calls
    calls += 1
    i += 1
    if x in [0,1]:
        print "."*4*i, '%d --> %d' % (x, x)
        return x
    f1 = fib(x-1, i)
    f2 = fib(x-2, i)
    print "."*4*i, '%d --> %d' % (x, f1+f2)
    return f1 + f2

answer = fib(8,0)
print 'Answer:', answer
print 'Calls :', calls


................................ 1 --> 1
................................ 0 --> 0
............................ 2 --> 1
............................ 1 --> 1
........................ 3 --> 2
............................ 1 --> 1
............................ 0 --> 0
........................ 2 --> 1
.................... 4 --> 3
............................ 1 --> 1
............................ 0 --> 0
........................ 2 --> 1
........................ 1 --> 1
.................... 3 --> 2
................ 5 --> 5
............................ 1 --> 1
............................ 0 --> 0
........................ 2 --> 1
........................ 1 --> 1
.................... 3 --> 2
........................ 1 --> 1
........................ 0 --> 0
.................... 2 --> 1
................ 4 --> 3
............ 6 --> 8
............................ 1 --> 1
............................ 0 --> 0
........................ 2 --> 1
........................ 1 --> 1
.................... 3 --> 2
........................ 1 --> 1
........................ 0 --> 0
.................... 2 --> 1
................ 4 --> 3
........................ 1 --> 1
........................ 0 --> 0
.................... 2 --> 1
.................... 1 --> 1
................ 3 --> 2
............ 5 --> 5
........ 7 --> 13
............................ 1 --> 1
............................ 0 --> 0
........................ 2 --> 1
........................ 1 --> 1
.................... 3 --> 2
........................ 1 --> 1
........................ 0 --> 0
.................... 2 --> 1
................ 4 --> 3
........................ 1 --> 1
........................ 0 --> 0
.................... 2 --> 1
.................... 1 --> 1
................ 3 --> 2
............ 5 --> 5
........................ 1 --> 1
........................ 0 --> 0
.................... 2 --> 1
.................... 1 --> 1
................ 3 --> 2
.................... 1 --> 1
.................... 0 --> 0
................ 2 --> 1
............ 4 --> 3
........ 6 --> 8
.... 8 --> 21
Answer: 21
Calls : 67