1. Define a function frame_with_ones(n) that takes an integer as an argument. Your function will create nXn arrays of zeros and then “frame” it with a border of ones. Return the formatted array. The returned array will have the shape: (n+2)x(n+2) HINT: Slicing may be beneficial to solving this.
For example:
frame_with_ones(3) returns:
np.array(
[[ 1. 1. 1. 1. 1. ]
[ 1. 0. 0. 0. 1. ]
[ 1. 0. 0. 0. 1. ]
[ 1. 0. 0. 0. 1. ]
[ 1. 1. 1. 1. 1. ]])
import numpy as np
def frame_with_ones(n):
data=np.ones((n+2,n+2))
data[1:-1,1:-1] = 0
return data
print(frame_with_ones(3))
Get Answers For Free
Most questions answered within 1 hours.