read the elements of an input matrix

Asked

Viewed 470 times

0

I have a code that represents a checkerboard (8x8 matrix):

print(f'ATENÇÃO: ')
print(f'Insira 0 nas posições vazias do tabuleiro;')
print(f'Insira 1 nas posições das peças pretas e 11 para as damas pretas;')
print(f'Insira 2 nas posições de peças vermelhas e 22 para damas vermelhas.')
print(" ")

matriz=[]
x1=x2=x3=x4=0


def gerarmatriz(l, c, matriz):
    for i in range (l):
        linha = []
        for j in range (c):
            y = input("peça da posição {} {}: ".format(chr(65+i),j+1))
            linha.append(int(y))
        matriz.append(linha)

gerarmatriz(8, 8, matriz)

print ('---' *20)

print(matriz)

and I wanted to know a way to read the amount of times that the numbers 1, 11, 2 and 22 are inserted by the 'user' because they represent the checkers game pieces.

2 answers

0

TL;DR

Just read it? The code below creates a list of dictionaries and stores the number 1, 11, 2 and 22 in the lists in each position of the matrix:

dicts = []
for n_lst in range(len(matriz)):
    lst = []
    for n in [1, 11, 2, 22]:
       key = 'matriz['+str(n_lst)+']'
       lst.append((n , matriz[n_lst].count(n)))
    dicts.append({key: lst})

With this you will have a list with the number of dictionaries equivalent to the size of the original "matrix", in case 8, each one presenting with tuples, the numbers 1, 11, 2 and 22 of each matrix list, the output will be something like this?

[{'matriz[0]': [(1, 0), (11, 2), (2, 1), (22, 1)]},
 {'matriz[1]': [(1, 0), (11, 2), (2, 1), (22, 0)]},
 {'matriz[2]': [(1, 0), (11, 2), (2, 0), (22, 2)]},
 {'matriz[3]': [(1, 0), (11, 2), (2, 0), (22, 1)]},

 ...]

Indicating that in matriz[0] there were 0 1’s, 2 11’s, 1 2’s and 1 22’s, and so on.

See working on repl it.

0


friend I believe that is your answer, but can give an improved conditions putting in a function!

print('ATENÇÃO: ')
print('Insira 0 nas posições vazias do tabuleiro;')
print('Insira 1 nas posições das peças pretas e 11 para as damas pretas;')
print('Insira 2 nas posições de peças vermelhas e 22 para damas vermelhas.')
print(" ")

matriz=[]
x1=x2=x3=x4=0


def gerarmatriz(l, c, matriz):
  global um, onze, dois, vintedois
  for i in range (l):
    linha = []
    for j in range (c):
        y = input(f"peça da posição {chr(65+i)} {j+1}: ")
        if int(y) == 1:
            um += 1
        if int(y) == 11:
            onze += 1
        if int(y) == 2:
            dois += 1
        if int(y) == 22:
            vintedois += 1
        linha.append(int(y))
    matriz.append(linha)


um = onze = dois = vintedois = 0
gerarmatriz(8, 8, matriz)

print ('---' *20)

print(matriz)
print(f"Você digitou '1' {um} vezes, '11' {onze} vezes, '2' {dois} vezes e '22' {vintedois} vezes") 
  • and I also gave an improved input

  • The problem of its approach would be the need for it to be done with several matrices to be "processed" in a next step. Aside from the fact that using global variables itself is a bad idea.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.