-1
Good evening guys, I have this matrix:
I need the matrix numbers summed all 15
.
Both vertical and horizontal..
Code:
import numpy as np
import random
from tabulate import tabulate
a = [1,2,3,4,5,6,7,8,9]
random.shuffle(a)
data = np.reshape(a, (3, 3))
soma_horizontal = []
for lista in data:
soma_horizontal.append(sum(lista))
soma_vertical = []
for x in range(len(data)):
soma = 0
for lista in data:
soma += lista[x]
soma_vertical.append(soma)
indice = 0
for lista in data:
lista.extend(['\u2192', soma_horizontal[indice]])
indice += 1
setas_cima = ['\u2191'] * 3
data = [soma_vertical] + [setas_cima] + data
print(tabulate(data, tablefmt="grid", stralign='center', numalign='center'))
The program running on repl:
Start by correcting the indentation of the question, which is not correct nor equal to the code you have in repl. Then you’re basically looking for a magic square. You can find a solution using combinations
– Isac
Good evening @Isac I changed the name of the question.. I wonder if you could help me??
– William
I copied the code that’s in the rpli-it, but don’t do this anymore: Python identation is not optional, what you had pasted there was a command soup, not a Python script. Use the button
{ }
to format your code instead of trying to manually ident in editing here.– jsbueno