r/pythonhelp • u/AliiiB2512 • Jan 21 '22
Unknown use of Function
Hi, i've been working on recognising algorithms and what they do, but im stuck on this one
i understand that this is a square calculator, and the last for loop is the comparitive to identify if too values multplied are square, but its the first part thats confusing me
thanks in advance
def newfunc(Ai)
s = np.shape(Ai)
Bi = np.zeros((s[0],s[0]))
BDiag = []
for i in range(s[0]):
for j in range(s[0]):
Bi[i,j] = j*Ai[i]
for i in range(s[0]):
if Bi[i,j] < i**2:
BDiag.append('less')
elif Bi[i,j] > i**2:
BDiag.append('more')
elif Bi[i,j] == i**2:
BDiag.append('square')
return BDiag
2
Upvotes
1
u/MT1961 Jan 22 '22
What is it you are having trouble with? This is using NumPy, a pretty well known statistical package for Python. The first two lines are creating an matrix for use in the rest of the function. It then sets each element of the matrix to the column multiplied by the value in the input matrix (Ai). After that, it checks the diagonals.