Exercise 1: Matrix functions


In [69]:
def matrix_id(d):
    '''
    This function takes an integer d and
    returns the d*d identity matrix
    
    matrix_id(3)
    [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
    
    '''
    assert type(d) == int, \
        'd should be an integer it is an '+type(d)
    assert d >= 0 , \
        'd should be a positive integer it is '+str(d)
        
    return [i*[0]+[1]+[0]*(d-i-1)
            for i in range(d)]

def matrix_transpose(m):
    '''
    This function takes a matrix m and
    returns the transposed matrix
    
    >>> matrix_transpose([[1,2,3,4],[5,6,7,8]])
    [[1, 5], [2, 6], [3, 7], [4, 8]]
    
    >>> matrix_transpose([[1,2],[3,4]])
    [[1, 3], [2, 4]]
    
    >>> matrix_transpose([[2]])
    [[2]]
    
    '''
    assert type(m) == list, \
        'm should be a list of lists'
    assert type(m[0]) == list, \
        'm should be a list of lists'
    
    return [[m[j][i] for j in range(len(m))]
            for i in range(len(m[0]))]

def matrix_scalar_mul(m,c):
    '''
    This function take a matrix m and a scalar c
    and returns the scalar product of both
    
    >>> matrix_scalar_mul([[1,2],[3,4]], 3)
    [[3, 6], [9, 12]]
    
    >>> matrix_scalar_mul([[1,2],[3,4]], 2.34)
    [[2.34, 4.68], [7.02, 9.36]]
    
    '''
    assert type(m) == list, \
        'm should be a list of lists'
    assert type(m[0]) == list, \
        'm should be a list of lists'
    assert type(c) == int or type(c) == float, \
        'c should be of type int or float'
    
    return [[m[i][j]*c for j in range(len(m[0]))]
            for i in range(len(m))]

if __name__ == '__main__':
    import doctest
    doctest.testmod()

Exercise 2: Iterative factorial and divide and conquer


In [124]:
def fact2(n):
    '''
    This function takes a positive integer n
    and returns its factorial
    
    >>> fact2(0)
    1
    
    >>> fact2(1)
    1
    
    >>> fact2(8)
    40320
    
    '''
    
    assert type(n) == int, \
        'd should be an integer it is an '+type(n)
    assert n >= 0 , \
        'd should be a positive integer it is '+str(n)
    
    if n == 0: 
        return 1
    result = 1
    for x in range(1,n+1):
        result *= x
        
    return result


def prod(l):
    '''
    This function takes a list of integers and 
    returns the product of all elements
    
    >>> prod([1,2,3])
    6
    
    >>> prod([-1,2,-3])
    6
    
    >>> prod([3,0,2])
    0
    
    >>> prod([-3,5.5,1,7])
    -115.5
    
    '''

    assert type(l) in [list, range], \
        'l should be a list or range of numbers'
    
    if len(l) == 1:
        return l[0]
    if len(l) == 2:
        return l[0]*l[1]
    else:
        mid = len(l)//2
        return prod(l[:mid]) * prod(l[mid:])

    
def fact3(n):
    '''
    This function takes a positive integer n
    and returns its factorial
    
    >>> fact3(0)
    1
    
    >>> fact3(1)
    1
    
    >>> fact3(8)
    40320
    
    '''

    assert type(n) == int, \
        'd should be an integer it is an '+type(n)
    assert n >= 0 , \
        'd should be a positive integer it is '+str(n)

    if n == 0 or n == 1: 
        return 1    

    return prod(range(1,n+1))

        
if __name__ == '__main__':
    import doctest
    doctest.testmod()

Exercise 3: Base conversion


In [161]:
def base_convert(n,b):
    '''
    >>> base_convert(123, 16)
    '7b'
    >>> base_convert(123, 2)
    '1111011'
    >>> base_convert(123, 36)
    '3f'
    
    '''
    assert type(n) == int, \
        'n should be an integer it is an '+type(n)
    assert type(b) == int, \
        'b should be an integer it is an '+type(b)
    assert 1 < b and b <= 36 , \
        'b should be between 2 and 36'
    
    import string
    result = ''
    while n > 0:
        values = string.digits+string.ascii_lowercase
        result += values[n%b]
        n //=b
        
    return result[::-1]
        
if __name__ == '__main__':
    import doctest
    doctest.testmod()

In [164]:
number = 123

for i in range(6,37,5):
    print('{} in base {:>2}:  {}'.format(number, i,
                                         base_convert(number,i)))


123 in base  6:  323
123 in base 11:  102
123 in base 16:  7b
123 in base 21:  5i
123 in base 26:  4j
123 in base 31:  3u
123 in base 36:  3f