Create a function which takes two arguments: x
and p
, where p
defaults to 2. It should return $x\,^p$. Your function must be named power
to receive credit. For this problem, do not consider invalid use of your function (i.e., what happens if x
is a string).
In [19]:
#The points awarded this cell corresopnd to partial credit and/or documentation
### BEGIN SOLUTION
def power(x, p=2):
'''Computes x^p
Args:
x: input number
p: input power, defaults to 2
returns: x^p as a floating point
'''
return x**p
### END SOLUTION
In [26]:
'''Check if your function returns the correct values'''
from numpy import testing as t
t.assert_almost_equal( power(3,2), 9 )
### BEGIN HIDDEN TESTS
import numpy as np
test_x = np.array([-2, -1.5, 0, 4])
test_p = np.array([-3, 2, 4, 1])
t.assert_almost_equal( power(test_x, test_p), test_x ** test_p)
t.assert_almost_equal( power(3), 3**2)
### END HIDDEN TESTS
Create a function which sums the numbers between its first (inclusive) and second argument (exclusive). For example, if you pass in 3
and 6
, it should return 12
(3+4+5
). Your function should return None
if the given arguments are not integers. Your function must be called int_sum
to receive credit.
In [23]:
#The points awarded this cell corresopnd to partial credit and/or documentation
### BEGIN SOLUTION
def int_sum(x, y):
'''Computes sum from x to y (excluding y)
Args:
x: start of sum
p: end of sum
returns: the sum as an integer
'''
if type(x) != type(1) or type(y) != type(1):
return None
s = 0
for i in range(x, y):
s += i
return s
### END SOLUTION
In [24]:
'''check that it returns correct answer'''
from numpy import testing as t
t.assert_equal( int_sum(3,6), 12)
### BEGIN HIDDEN TESTS
t.assert_equal( int_sum(-2, 7), sum(range(-2, 7)))
t.assert_equal( int_sum(0, 5), sum(range(0, 5)))
### END HIDDEN TESTS
In [25]:
'''check that it deals with invalid input correctly'''
from numpy import testing as t
t.assert_equal( int_sum(4.4, 4.6), None)
### BEGIN HIDDEN TESTS
t.assert_equal( int_sum('test', 4), None)
t.assert_equal( int_sum(3, 'test'), None)
t.assert_array_equal( int_sum(5,4), 0)
### END HIDDEN TESTS
Create a function which takes in two arguments: a floating point number and an integer representing precision. It should return a string that prints the number to the given precision or None
if any of the arguments are invalid. Your function must be called pprint
to receive credit.
In [43]:
#The points awarded this cell corresopnd to partial credit and/or documentation
### BEGIN SOLUTION
def pprint(x, i):
'''Prints x to the given precision indicated by i
Args:
x: the number to print
i: the integer precision
returns: a string
'''
if( not (type(x) == float or type(x) == int)):
return None
if(type(i) != int or i <= 0):
return None
return '{:.{}}'.format(x, i)
### END SOLUTION
In [45]:
'''check answer is correct'''
from numpy import testing as t
t.assert_equal( pprint(4.3212, 2), '4.3')
### BEGIN HIDDEN TESTS
t.assert_equal( pprint(-4.3212, 2), '-4.3')
t.assert_equal( pprint(5.45676, 3), '5.46')
t.assert_equal( pprint(11.2, 1), '1e+01')
### END HIDDEN TESTS
In [46]:
'''check that your function correctly deals with invalid input'''
from numpy import testing as t
t.assert_equal( pprint('not a number', 4), None)
### BEGIN HIDDEN TESTS
t.assert_equal( pprint(-4.3212, -2), None)
t.assert_equal( pprint(5.45676, 'b'), None)
t.assert_equal( pprint(55, 4.12), None)
t.assert_equal( pprint(55, 0), None)
### END HIDDEN TESTS