Given a certain square matrix ```A```, of dimension ```n x n```, that has negative and positive values (many of them may be 0).

We need the following values rounded to the closest integer:

- the average of all the positive numbers (more or equal to 0) that are in the principal diagonal and in the columns with odd index, **avg1**

- the absolute value of the average of all the negative numbers in the secondary diagonal and in the columns with even index, **avg2**

Let's see the requirements in an example:
```
A = [[ 1,   3, -4,   5, -2,  5,  1], 
    [  2,   0, -7,   6,  8,  8, 15],
    [  4,   4, -2, -10,  7, -1,  7],
    [ -1,   3,  1,   0, 11,  4, 21],
    [ -7,   6, -4,  10,  5,  7,  6],
    [ -5,   4,  3,  -5,  7,  8, 17],
    [-11,   3,  4,  -8,  6, 16,  4]]
```    
The elements of the principal diagonal are: 
```
[1, 0, -2, 0, 5, 8, 4]
```
The ones that are also in the "odd columns" (we consider the index starting with 0) are: 
```
[0, 0, 8] all positives
```
So,
```
avg1 =  [8 / 3] = 3
```
The elements of the secondary diagonal are:
```
[-11, 4, -4, 0, 7, 8, 1]
```
The ones that are in the even columns are:
```
[-11, -4, 7, 1]
```
The negative ones are:
```
[-11, 4]
```
So,
```
avg2 = [|-15 / 2|] = 8
```
Create a function ```avg_diags()```that may receive a square matrix of uncertain dimensions and may output both averages in an array in the following order: ```[avg1, avg2]```

If one of the diagonals have no elements fulfilling the requirements described above the function should return ```-1```
So, if the function processes the matrix given above, it should output:
```
[3, 8]
```
Features of the random tests:
```
number of tests = 110
5 ≤ matrix_length ≤ 1000
-1000000 ≤ matrix[i, j] ≤ 1000000
```
Enjoy it!
Translations into another languages will be released soon.

Source: codewars | Difficulty: EASY
Tags: Matrices

Fundamentals

Data structures

Mathematics

Starter code: def avg_diags(m):
	

Solutions:
def avg_diags(m):
	(a1, a2, l, l1, l2) = (0, 0, len(m), 0, 0)
	for i in range(0, l):
		if i & 1:
			if m[i][i] >= 0:
				a1 += m[i][i]
				l1 += 1
		elif m[l - i - 1][i] < 0:
			a2 += m[len(m) - i - 1][i]
			l2 += 1
	return [round(a1 / l1) if l1 > 0 else -1, round(abs(a2) / l2) if l2 > 0 else -1]


def avg_diags(m):
	(n, avg) = (len(m), [[0, 0], [0, 0]])
	for i in range(n):
		if i % 2 and m[i][i] >= 0:
			avg[0][0] += m[i][i]
			avg[0][1] += 1
		elif not i % 2 and m[n - i - 1][i] < 0:
			avg[1][0] -= m[n - i - 1][i]
			avg[1][1] += 1
	return [round(lst[0] / lst[1]) if lst[1] != 0 else -1 for lst in avg]


def avg_diags(m):
	avg1 = []
	avg2 = []
	for i in range(len(m)):
		if i % 2 == 1 and m[i][i] >= 0:
			avg1.append(m[i][i])
		if i % 2 == 0 and m[-i - 1][i] < 0:
			avg2.append(m[-i - 1][i])
	avg1 = round(sum(avg1) / len(avg1)) if len(avg1) else -1
	avg2 = round(abs(sum(avg2) / len(avg2))) if len(avg2) else -1
	return [avg1, avg2]


import numpy as np

def avg_diags(m):
	m = np.array(m)
	md = m.diagonal()
	sd = np.flipud(m).diagonal()
	md = [md[i] for i in range(1, len(md), 2) if md[i] >= 0]
	sd = [sd[i] for i in range(0, len(sd), 2) if sd[i] < 0]
	avs = -1
	if sd:
		avs = round(abs(sum(sd)) / len(sd))
	return [round(sum(md) / len(md)), avs]


import numpy as np

def avg_diags(arr):
	arr = np.array(arr)
	d1 = np.diag(arr)
	d2 = np.diag(np.flipud(arr))
	d1 = d1[1::2]
	d1 = d1[d1 >= 0].mean().round()
	d2 = d2[::2]
	d2 = abs(d2[d2 < 0].mean().round())
	d1 = -1 if np.isnan(d1) else d1
	d2 = -1 if np.isnan(d2) else d2
	return [d1, d2]


def avg_diags(m):
	(a, ac, b, bc, n) = (0, 0, 0, 0, len(m))
	for i in range(n):
		if i & 1 == 1 and m[i][i] >= 0:
			a += m[i][i]
			ac += 1
		if n - i - 1 & 1 == 0 and m[i][n - i - 1] < 0:
			b += m[i][n - i - 1]
			bc += 1
	return [-1 if ac == 0 else round(a / ac), -1 if bc == 0 else round(abs(b / bc))]


def avg_diags(m):
	l = len(m)
	a = [m[i][i] for i in range(1, l, 2) if m[i][i] >= 0]
	b = [m[i][l - i - 1] for i in range(l) if m[i][l - i - 1] < 0 and (l - i - 1) % 2 == 0]
	return [int(round(sum(a) / len(a))) if a else -1, int(round(abs(sum(b)) / len(b))) if b else -1]


def avg_diags(m):
	r1 = [m[i][i] for i in range(1, len(m), 2) if m[i][i] >= 0]
	r2 = [m[-i - 1][i] for i in range(0, len(m), 2) if m[-i - 1][i] <= 0]
	return [round(sum(r1) / len(r1)) if r1 else -1, abs(round(sum(r2) / len(r2))) if r2 else -1]


def avg_diags(m):
	prim = []
	sec = []
	for i in range(len(m)):
		prim.append(m[i][i])
		sec.append(m[i][-i - 1])
	sec.reverse()
	prim = [prim[i] for i in range(len(prim)) if i % 2 != 0 and prim[i] >= 0]
	sec = [sec[i] for i in range(len(sec)) if i % 2 == 0 and sec[i] <= 0]
	prim = round(sum(prim) / len(prim))
	if len(sec) == 0:
		sec = -1
		return [prim, sec]
	else:
		sec = round(sum(sec) / len(sec))
	if sec < 0:
		sec = -sec
	return [prim, sec]


import math

def avg_diags(m):
	diagonal_1 = []
	diagonal_2 = []
	ans1 = 0
	ans2 = 0
	for i in range(len(m)):
		if i % 2 != 0 and m[i][i] >= 0:
			diagonal_1.append(m[i][i])
		if i % 2 == 0 and m[len(m) - 1 - i][i] < 0:
			diagonal_2.append(m[len(m) - 1 - i][i])
	if diagonal_1 == []:
		ans1 = -1
	else:
		ans1 = round(sum(diagonal_1) / len(diagonal_1), 0)
	if diagonal_2 == []:
		ans2 = -1
	else:
		ans2 = round(sum(diagonal_2) * -1 / len(diagonal_2), 0)
	return [ans1, ans2]