E
I have another idea of building this matrix, just sharing my idea:def exibir_matriz(n):
Alfabeto = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
camadas = []
camada_temp = []
n_alfa = 2 * n + 1
for i in range(n):
camada_temp.insert(i, Alfabeto[i])
camada_temp.insert(-i, Alfabeto[i])
c = camada_temp.copy()
while len(c) < n_alfa:
c.insert(i, Alfabeto[i])
camadas.append(c)
meio = camadas[n-1].copy()
meio.pop(n)
meio.insert(n, '*')
for camada in camadas:
print(camada)
print(meio)
camadas.reverse()
for camada in camadas:
print(camada)
The idea is that when the Matrix has 3 layers it has 4 types of layers:1 A A A A A A A tipo1
2 A B B B B B A tipo2
3 A B C C C B A tipo3
4 A B C * C B A tipo4
5 A B C C C B A tipo3
6 A B B B B B A tipo2
7 A A A A A A A tipo1
We have some patterns:Most items are used 2 times.The middle layer is only used 1 time.Number of Alphabets is equal number of layers, For example: 3 layers
has A, B, CNumber of letters per layer is equal 2 * camadas + 1First and last layer will always be only AIf we list each letter: 1-A, 2-B, 3-C. The letter number equals the number of the indíce where will start the letter of the layer of the same number: The layer is equal to the previous layer only that replacing the medium, and leaving to tips with 1 of each existing letter: With this patterns we can create the 4 types of layers, starting with the layers that duplicate (type1, type2, type3First is to start listing the Alphabets, Set number of letters per layer and one list of all kinds of layers:n_alfa = 2 * n + 1
Alfabeto =['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
camadas = []
Then Alfabeto[0] = A, Alfabeto[1] = B and so on.The idea of assembling all layers is to use a template list.To create this template, we can note that we insert the letter in the same number of the letter number:#1- A
A _ _ _ _ _ A
1 2 3 4 5 6 7
#2- B
A B _ _ _ B A
1 2 3 4 5 6 7
To enter the end we only use negative number of list:#1- A
A _ _ _ _ _ A
7 6 5 4 3 2 1 (index negativo)
#2- B
A B _ _ _ B A
7 6 5 4 3 2 1 (index negativo)
And fill it with the letter according to the layer number:#Camada 1
camada_temp = ['A','A']
#A _ _ _ _ _ A
#Preencher o meio com letra 'A' (Como disse antes 1-A)
#Camada 2
camada_temp = ['A', 'B', 'B', 'A']
#A B _ _ _ B A
Preencher o meio com letra 'B' (2-B)
And finally we add this kind of layer in list of layers.camada_temp = []
for i in range(n):
camada_temp.insert(i, Alfabeto[i]) #Inserindo Alfabeto no index positivo
camada_temp.insert(-i, Alfabeto[i]) #Inserindo Alfabeto no index negativo
c = camada_temp.copy() #Duplicando a camada
while len(c) < n_alfa: #Preenchendo o meio
c.insert(i, Alfabeto[i])
camadas.append(c) #Adicionando a 'list' de camadas
Now the middle layer is equal to last layer type only that replacing the middle. The middle is after the number of layers.A B C C C B A
A B C * C B A
1 2 3 4 5 6 7
meio = camadas[n-1].copy()
meio.pop(n)
meio.insert(n, '*')
Now we just need to assemble the matrix, as we create from layer 1 to 3 we can print neatly. Then the middle and then we reversed it. The order is:1 2 3 meio 3 2 1for camada in camadas: #Imprimindo Ordenadamente
print(camada)
print(meio) #Meio
camadas.reverse() #Revertendo a lista
for camada in camadas: #Imprimindo a lista revertida
print(camada)
Putting input:n = input('Numero de camadas...')
exibir_matriz(n)