Challenge - Roux¶
Define a roux array as a 1-D array such that, when it's reversed, it represents the sequence of square numbers 1, 4, 9, 16, ... with 0s interwoven between them.
# roux array of length 5
[9 0 4 0 1]
# roux array of length 8
[ 0 16 0 9 0 4 0 1]
# roux array of length 12
[ 0 36 0 25 0 16 0 9 0 4 0 1]
Implement a function called make_roux(n)
that inputs n, the desired size of the array, and outputs the corresponding roux array. Then test it on the examples above.
In [1]:
%pip install numpy
import numpy as np
Requirement already satisfied: numpy in /home/samrat/Documents/numpy-torch-tutorials/venv/lib/python3.8/site-packages (1.24.4) Note: you may need to restart the kernel to use updated packages.
In [29]:
def make_roux(n):
res = np.zeros(n, dtype='int64')
sequential = np.arange(start=1, stop=((n + 1)//2 + 1), dtype='int64')
sequential_square = np.flip(sequential * sequential)
res[(n%2 + 1)%2::2] = sequential_square
return res
In [36]:
print(make_roux(5))
print(make_roux(8))
print(make_roux(12))
[9 0 4 0 1] [ 0 16 0 9 0 4 0 1] [ 0 36 0 25 0 16 0 9 0 4 0 1]