Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Exercise 13 - numpy practice

5 total points

Directions

  • Due: 12/7/2025 @ 11:59 p.m.

  • Change the name of your notebook to EX12_FirstLast.ipynb, where First is your first name and Last is your last name.

  • For each of the following prompts, write or modify Python code that fulfills the requirements.

All problems are worth 0.5 points unless otherwise specified.

Problem 1

Turn the list into a numpy array and set it equal to ‘a_n’

import numpy as np

def problem_1():

    a = [1, 2, 3, 4, 5]

    a_n = None # replace None and turn a into a numpy array here

    return a_n

print(problem_1())
None

Problem 2

Turn the list into a numpy array and set it equal to ‘a_n’ and multiply the numpy array by 10

def problem_2():

    a = [.1, .2, .3, .4, .5]

    a_n = None # replace None and turn a into a numpy array here

    a_n = None # replace None and multiply the numpy array by 10 here

    return a_n

print(problem_2())
None

Problem 3

Turn the list into a numpy array and set it equal to ‘a_n’ and divide the numpy array by 10

def problem_3():

    a = [10, 20, 30, 40, 50]

    a_n = None # replace None and turn a into a numpy array here

    a_n = None # replace None and divide the numpy array by 10 here

    return a_n

print(problem_3())
None

Problem 4

Turn the list into a numpy array and set it equal to ‘a_n’ and subtract 10 from the numpy array

def problem_4():

    a = [11, 12, 13, 14, 15]

    a_n = None # replace None and turn a into a numpy array here

    a_n = None # replace None and subtract 10 from the numpy array here

    return a_n

print(problem_4())
None

Problem 5

Turn the list into a numpy array and set it equal to ‘a_n’ and add 10 to the numpy array

def problem_5():

    a = [-9, -8, -7, -6, -5]

    a_n = None # replace None and turn a into a numpy array here

    a_n = None # replace None and add 10 to the numpy array here

    return a_n

print(problem_5())
None

Problem 6

Turn the list of rainfall in mm into a numpy array and convert it to inches

1 mm = 0.04 inches

‘a_mm’ must be an ndarray and ‘a_in’ must be correct.

def problem_6():

    a = [[10, 11, 12, 13, 14],
         [11, 12, 13, 14, 15],
         [12, 13, 14, 15, 16],
         [13, 14, 15, 16, 17],
         [14, 15, 16, 17, 18]]
    
    a_mm = None # replace None and turn a into a numpy array here

    a_in = None # replace None here and convert a_mm to inches

    return a_mm, a_in

print(problem_6())
(None, None)

Problem 7

turn the list of rainfall in mm into a numpy array

next, turn the list of 0s and 1s into into a numpy array

finally, find the sum of a_mm only where b is 1

‘a_n’ must be an ndarray

‘b_n’ must be an ndarray

‘c’ must be the result where positions in ‘a_n’ that correspond to a 0 in ‘b_n’ are also zero

‘d’ must be the correct sum

def problem_7():

    a = [[10, 11, 12, 13, 14],
         [11, 12, 13, 14, 15],
         [12, 13, 14, 15, 16],
         [13, 14, 15, 16, 17],
         [14, 15, 16, 17, 18]]
    
    a_n = None # replace None and turn a into a numpy array here

    b = [[1, 0, 0, 0, 1],
         [0, 1, 0, 1, 0],
         [0, 0, 1, 0, 0],
         [0, 1, 0, 1, 0],
         [1, 0, 0, 0, 1]]

    b_n = None # replace None and turn b into a numpy array here

    c = None # find a way to make a values 0 for locations where b is also zero

    c_sum = None # find the sum of c

    return a_n, b_n, c, c_sum

print(problem_7())
(None, None, None, None)

Problem 8

Use slice indexing to replace every 0 in row 3 with 1

‘a’ must be the original unmodified list

‘a_n’ must be the modified list as an ndarray and it must use values from a and not a different list

def problem_8():

    a = [[0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0]]
    
    a_n = None # replace None and turn a into a numpy array here

    a_n = None # use slice indexing to meet the requirements.

    return a, a_n

print(problem_8())
([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]], None)

Problem 9

Use slice indexing to replace every 0 in column 3 with 1

‘a’ must be the original unmodified list

‘a_n’ must be the modified list as an ndarray and it must use values from a and not a different list

def problem_9():

    a = [[0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0]]
    
    a_n = None # replace None and turn a into a numpy array here

    a_n = None # use slice indexing to meet the requirements.

    return a, a_n

print(problem_9())
([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]], None)

Problem 10

Use slice indexing to replace every 0 in column 4 and row 2 with 1

‘a’ must be the original unmodified list

‘a_n’ must be the modified list as an ndarray and it must use values from a and not a different list

def problem_10():

    a = [[0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0]]
    
    a_n = None # replace None and turn a into a numpy array here

    a_n = None # use slice indexing to meet the first requirement.

    a_n = None # use slice indexing to meet the second requirement.

    return a, a_n

print(problem_10())
([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]], None)

Grader

Do not modify the code below. Your functions above must work here or you will not get credit.

import numpy as np

def test_problem_1():

    result = problem_1()

    assert isinstance(result, np.ndarray)

def test_problem_2():

    result = problem_2()

    assert isinstance(result, np.ndarray)

    np.testing.assert_almost_equal(result, np.array([1, 2, 3, 4, 5]))

def test_problem_3():

    result = problem_3()

    assert isinstance(result, np.ndarray)

    np.testing.assert_almost_equal(result, np.array([1, 2, 3, 4, 5]))

def test_problem_4():

    result = problem_4()

    assert isinstance(result, np.ndarray)

    np.testing.assert_almost_equal(result, np.array([1, 2, 3, 4, 5]))


def test_problem_5():

    result = problem_5()

    assert isinstance(result, np.ndarray)

    np.testing.assert_almost_equal(result, np.array([1, 2, 3, 4, 5]))
    
def test_problem_6():

    result_1, result_2 = problem_6()

    assert isinstance(result_1, np.ndarray)

    assert isinstance(result_2, np.ndarray)

    test_1 = np.array([[10, 11, 12, 13, 14],
                       [11, 12, 13, 14, 15],
                       [12, 13, 14, 15, 16],
                       [13, 14, 15, 16, 17],
                       [14, 15, 16, 17, 18]])
    
    test_2 = np.array([[0.4,  0.44, 0.48, 0.52, 0.56],
                       [0.44, 0.48, 0.52, 0.56, 0.6 ],
                       [0.48, 0.52, 0.56, 0.6,  0.64],
                       [0.52, 0.56, 0.6,  0.64, 0.68],
                       [0.56, 0.6,  0.64, 0.68, 0.72]])
    
    np.testing.assert_array_almost_equal(test_1, result_1)


    np.testing.assert_array_almost_equal(test_2, result_2)
    
def test_problem_7():

    res_1, res_2, res_3, res_4 = problem_7()

    assert isinstance(res_1, np.ndarray)
    assert isinstance(res_2, np.ndarray)
    assert isinstance(res_3, np.ndarray)

    test_1 = np.array([[10, 11, 12, 13, 14],
                       [11, 12, 13, 14, 15],
                       [12, 13, 14, 15, 16],
                       [13, 14, 15, 16, 17],
                       [14, 15, 16, 17, 18]])
    
    test_2 = np.array([[1, 0, 0, 0, 1],
                       [0, 1, 0, 1, 0],
                       [0, 0, 1, 0, 0],
                       [0, 1, 0, 1, 0],
                       [1, 0, 0, 0, 1]])
    
    test_3 = np.array([[10, 0, 0, 0, 14],
                       [0, 12, 0, 14, 0],
                       [0, 0, 14, 0, 0],
                       [0, 14, 0, 16, 0],
                       [14, 0, 0, 0, 18]])
    
    np.testing.assert_array_almost_equal(test_1, res_1)
    np.testing.assert_array_almost_equal(test_2, res_2)
    np.testing.assert_array_almost_equal(test_3, res_3)
    assert res_4 == 126
    
def test_problem_8():

    res1, res2 = problem_8()
    
    assert np.sum(res1) == 0

    test_2 = np.array([[0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0],
                       [1, 1, 1, 1, 1],
                       [0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0]])
    
    np.testing.assert_almost_equal(res2, test_2)
    
    
def test_problem_9():

    res1, res2 = problem_9()
    
    assert np.sum(res1) == 0

    test_2 = np.array([[0, 0, 1, 0, 0],
                       [0, 0, 1, 0, 0],
                       [0, 0, 1, 0, 0],
                       [0, 0, 1, 0, 0],
                       [0, 0, 1, 0, 0]])
    
    np.testing.assert_almost_equal(res2, test_2)
    
def test_problem_10():

    res1, res2 = problem_10()
    
    assert np.sum(res1) == 0

    test_2 = np.array([[0, 0, 0, 1, 0],
                       [1, 1, 1, 1, 1],
                       [0, 0, 0, 1, 0],
                       [0, 0, 0, 1, 0],
                       [0, 0, 0, 1, 0]])
    
    np.testing.assert_almost_equal(res2, test_2)
    
point_total = 0

for num, problem in enumerate([test_problem_1, test_problem_2, test_problem_3, 
                               test_problem_4, test_problem_5, test_problem_6, 
                               test_problem_7, test_problem_8, test_problem_9, 
                               test_problem_10]):
    try:
        problem()
        point_total += 0.5
        print(f"Problem {num+1} is correct")
    except Exception as e:
        print(f"Problem {num+1} is not correct. Check the directions.")
        print(e)
        
print(f"Your score is {point_total}/5")
Problem 1 is not correct. Check the directions.

Problem 2 is not correct. Check the directions.

Problem 3 is not correct. Check the directions.

Problem 4 is not correct. Check the directions.

Problem 5 is not correct. Check the directions.

Problem 6 is not correct. Check the directions.

Problem 7 is not correct. Check the directions.

Problem 8 is not correct. Check the directions.
unsupported operand type(s) for -: 'NoneType' and 'float'
Problem 9 is not correct. Check the directions.
unsupported operand type(s) for -: 'NoneType' and 'float'
Problem 10 is not correct. Check the directions.
unsupported operand type(s) for -: 'NoneType' and 'float'
Your score is 0/5