Question: Python 1a) Write a function (by completing the template below) that computes the represented number corresponding to a specific set of parameter values. For this
Python


1a) Write a function (by completing the template below) that computes the represented number corresponding to a specific set of parameter values. For this part of the question, assume that all input values are of the appropriate type (i.e., digits is a list of integers and the other arguments are integers). In [ ]: from nose.tools import assert_equal, assert_raises In (): def list_to_num(sign_bit, digits, exponent, base=2): compute the numerical value corresponding to floating point parameter values Args : sign_bit: for positive, 1 for negative digits: list of integer digits in range [0, base] exponent: integer exponent value Named args: base: default value 2 Returns: float value of represented number # YOUR CODE HERE raise Not ImplementedError() return value Test your function definition by entering (list_to_num(0, [1,1,1), 3) in a code cell and evaluating. This should produce the largest number the system can represent. What should this largest value be? Check that your function produces the appropriate value. The cell below performs additional tests in an automated fashion. Each assert_equal statement takes 2 arguments: a function call and an output value. If the function call produces the specified value, then the assertion is valid and no output is produced. Messages will be generated to alert you if there are cases for which the function call does NOT produce the specified value. In [ ]: assert_equal(list_to_num(0, [1,0,0], 0), 1) assert equal(list_to_num( 0, [1,0, 1], 1), 2.5) assert_equal(list_to_num(1, [1,0, 1], 1), -2.5) assert_equal(list_to_num(0, (1,0,1), 2), 5.0) 1b) Write a similar function that produces the numerical value represented by the arguments, but this time assume that digits is a numpy ndarray instead of a python list. In : import numpy as np def array_to_num( sign_bit, digits, exponent, base=2): compute the numerical value corresponding to floating point parameter values Args: sign_bit: 0 for positive, 1 for negative digits: 1D numpy array of integer digits in range [0, base] exponent: integer exponent value Named args: base: default value 2 Returns: float value of represented number # YOUR CODE HERE raise Not ImplementedError() return value In [ ]: assert_equal(array_to_num(0, np.array([1,0,0]), %), 1) assert_equal(array_to_num(O, np.array([1,0,1]], 1), 2.5) assert equal(array_to_num(1, np.array([1,0,1)), 1), -2.5) assert_equal(array_to_num(O, np.array([1,0,1)), 2), 5.0) assert equal(array_to_num( 0, np.array([1,0,1]), 2, base=10), 101) 1c) You should expect some of the assertions might fail initially. Can you figure out why and fix the problem? HINT: Check the data type of your digits argument. Is your code doing integer or floating point arithmetic In [ ]: def array_to_num2 (sign_bit, digits, exponent, base=2): compute the numerical value corresponding to floating point parameter values Args: sign_bit: for positive, 1 for negative digits: 1D numpy array of integer digits in range [0, base] exponent: integer exponent value Named args: base: default value 2 Returns: float value of represented number # YOUR CODE HERE raise Not ImplementedError() return value In [ ]: assert_equal(array_to_num2(0, np.array([1,0,0]), o), 1) assert_equal(array_to_num2 (0, np.array([1,0,1]], 1), 2.5). assert equal(array_to_num2(1, np.array( (1,0,1]], 1), -2.5) assert equal(array_to_num2(0, np.array([1,0,1]), 2), 5.0)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
