Exercises for Session 1

Exercise (4)

In [1]:
import math
In [2]:
favnums = [1, 3.1415, math.sqrt(7), 6, 7.5]
In [3]:
for i in favnums:
    y = math.cosh(i)**2 - math.sinh(i)**2
    test = abs(y - 1) < 1e-6
    print('{:7.5f}  {:7.5f}  {}'.format(i, y, test))
1.00000  1.00000  True
3.14150  1.00000  True
2.64575  1.00000  True
6.00000  1.00000  True
7.50000  1.00000  True
In [4]:
print([(math.cosh(i)**2 - math.sinh(i)**2 - 1) < 1e-6 for i in favnums])
[True, True, True, True, True]

Exercises (5) and (6)

In [5]:
def is_divisible(x, n):
    """ Find the indices of x where the element is exactly divisible by n.

    Arguments:
    x - list of numbers to test
    n - single divisor

    Returns a list of the indices of x for which the value of the element is
    divisible by n (to a precision of 1e-6 in the case of floats).

    Example:
    >>> favnums = [1, 3.1415, math.sqrt(7), 6, 7.5]
    >>> divisor = 3
    >>> idx = is_divisible(favnums, divisor)
    >>> for i in idx:
    ...     print('{}, {}'.format(i, favnums[i]))
    3, 6

    """
    
    r = []
    small = 1e-6
    for i, m in enumerate(x):
        if m % n < small:
            r.append(i)
    return r
In [6]:
help(is_divisible)
Help on function is_divisible in module __main__:

is_divisible(x, n)
    Find the indices of x where the element is exactly divisible by n.
    
    Arguments:
    x - list of numbers to test
    n - single divisor
    
    Returns a list of the indices of x for which the value of the element is
    divisible by n (to a precision of 1e-6 in the case of floats).
    
    Example:
    >>> favnums = [1, 3.1415, math.sqrt(7), 6, 7.5]
    >>> divisor = 3
    >>> idx = is_divisible(favnums, divisor)
    >>> for i in idx:
    ...     print('{}, {}'.format(i, favnums[i]))
    3, 6

In [7]:
divisor = 3
idx = is_divisible(favnums, divisor)
for i in idx:
    print('Value {} (at index {}) is divisible by {}'.format(favnums[i], i, divisor))
Value 6 (at index 3) is divisible by 3

Exercise (7)

In [8]:
# produce example file
file('ex1_7.txt', 'w').write(
"""1 2 3 4 5 6 7 8 9 10
1 4 9 16 25 36 49 64 81 100
1 8 27 64 125 216 343 512 729 1000
1 16 81 256 625 1296 2401 4096 6561 10000
""")
In [9]:
def is_divisible_from_file(fname='ex1_7.txt', n=8):
    """ Find values in a file which are exactly divisible by n.

    Arguments:
    x - filename: file should contain rows of numbers
    n - single divisor

    Returns a list containing, for each line in the specified file,
    lists of the zero-indexed column numbers for which the value is
    divisible by n (to a precision of 1e-6).

    Example:
    >>> is_divisible_from_file('ex1_7.txt', 8)
    [[7], [3, 7], [1, 3, 5, 7, 9], [1, 3, 5, 7, 9]]

    """

    r = []
    f = file(fname)
    for l in f:
        x = [float(j) for j in l.split()]
        y = []
        small = 1e-6
        for i, m in enumerate(x):
            if m % n < small:
                y.append(i)
        r.append(y)
    return r
In [10]:
divisor = 8
idxlist = is_divisible_from_file('ex1_7.txt', divisor)
print('The values at the following indices are divisible by {}:'.format(divisor))
for lineno, idx in enumerate(idxlist):
    print('Line {}: {}'.format(lineno, idx))
The values at the following indices are divisible by 8:
Line 0: [7]
Line 1: [3, 7]
Line 2: [1, 3, 5, 7, 9]
Line 3: [1, 3, 5, 7, 9]

Exercise (8)

In [11]:
class divtest:
    """ Create an object for testing whether the elements of a list are exactly divisible by n.

    Example:
        >>> favnums = [1, 3.1415, math.sqrt(7), 6, 7.5]
        >>> div3tester = divtest(3)
        >>> idx = div3tester.test(favnums)
        >>> for i in idx:
        ...     print i, favnums[i]
        3, 6
    
    """
    
    def __init__(self, n):
        self.n = n

    def test(self, x):
        r = []
        small = 1e-6
        for i, m in enumerate(x):
            if m % self.n < small:
                r.append(i)
        return r
In [12]:
help(divtest)
Help on class divtest in module __main__:

class divtest
 |  Create an object for testing whether the elements of a list are exactly divisible by n.
 |  
 |  Example:
 |      >>> favnums = [1, 3.1415, math.sqrt(7), 6, 7.5]
 |      >>> div3tester = divtest(3)
 |      >>> idx = div3tester.test(favnums)
 |      >>> for i in idx:
 |      ...     print i, favnums[i]
 |      3, 6
 |  
 |  Methods defined here:
 |  
 |  __init__(self, n)
 |  
 |  test(self, x)

In [13]:
divisor = 3
div3tester = divtest(divisor)
idx = div3tester.test(favnums)
for i in idx:
    print('Value {} (at index {}) is divisible by {}'.format(favnums[i], i, divisor))
Value 6 (at index 3) is divisible by 3