In: Computer Science
can you please create the code program in PYTHON for me.
i want to create array matrix Nx1 (N is multiple of 4 and start from 16), and matrix has the value of elements like this:
if N = 16, matrix is
[ 4 4 4 4 -4 -4 -4 -4 4 4 4 4 -4 -4 -4 -4]
if N = 64, matrix is
[8 8 8 8 8 8 8 8 -8 -8 -8 -8 -8 -8 -8 -8 8 8 8 8 8 8 8 8 -8 -8 -8
-8 -8 -8 -8 -8 8 8 8 8 8 8 8 8 -8 -8 -8 -8 -8 -8 -8 -8 8 8 8 8 8 8
8 8 -8 -8 -8 -8 -8 -8 -8 -8]
if N = 256, matrix is
[16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 -16 -16 -16 -16
-16 -16 -16 -16 -16 -16 -6 -16 -16 -16 -16 -16 16 16 16 16 16 16 16
16 16 16 16 16 16 16 16 16 -16 -16 -16 -16 -16 -16 -16 -16 -16 -16
-6 -16 -16 -16 -16 -16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16
16 -16 -16 -16 -16 -16 -16 -16 -16 -16 -16 -6 -16 -16 -16 -16 -16
16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 -16 -16 -16 -16 -16
-16 -16 -16 -16 -16 -6 -16 -16 -16 -16 -16 16 16 16 16 16 16 16 16
16 16 16 16 16 16 16 16 -16 -16 -16 -16 -16 -16 -16 -16 -16 -16 -6
-16 -16 -16 -16 -16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16
-16 -16 -16 -16 -16 -16 -16 -16 -16 -16 -6 -16 -16 -16 -16 -16 16
16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 -16 -16 -16 -16 -16
-16 -16 -16 -16 -16 -6 -16 -16 -16 -16 -16 16 16 16 16 16 16 16 16
16 16 16 16 16 16 16 16 -16 -16 -16 -16 -16 -16 -16 -16 -16 -16 -6
-16 -16 -16 -16 -16]
and so on for N = 1024, 4096, etc
please paste the code.
# PLEASE Read all the comments
import math # For sqrt of input number
def printResult(num): # This is function to print the
result
num = int(math.sqrt(num)) ## Get the sqrt of number
numPositive = str(num) # String for positive char like 4
numNegative = str(-num) # String for negative char like -4
## Set the positive string and negative string
for i in range(0,num-1):
numPositive = numPositive + ' ' + str(num) # StringList for
positive char like 4 4 4 4
numNegative = numNegative + ' -' + str(num) # StringList for
negative char like -4 -4 -4 -4
numList = numPositive + ' ' + numNegative + ' ' # for a pair of
output like 4 4 4 4 -4 -4 -4 -4
ansList = '' # for the list for answer
for i in range(0,int(num/2)): # Print result for num/2 times
pair
ansList = ansList + numList # append the pair in ansList
print('[' + ansList +']') # print the result
print('Enter value') # Show input line
n = int(input()) # Get the input
printResult(n) # Print the result with given by calling
function
# See the output