Square Matrix in Python 3

Asked

Viewed 1,152 times

1

My code is printing spaces at the beginning and end of the line, and is giving presentation error (URI issue 1557) Does anyone have a good tip? "Matrix values should be formatted in a right-justified T-size field and separated by space"

matriz = []

while True:

tamanho = int(input())

valor = 1

if tamanho == 0:

break

for i in range(tamanho):
linha = []
for j in range(tamanho):
  linha.append(valor)
  valor = valor*2
  if len(linha)==tamanho:

    valor = linha[0]

    valor = valor*2

matriz.append(linha)

tamanho_caracter=(str(matriz[-1][-1]))

tamanho_caracter=len(tamanho_caracter)

aux=tamanho_caracter+1

for a in matriz:

for b in a:

  convert1=str(b)

  print("{}".format(convert1).rjust(aux),end="".strip())

print()

print()  

matriz = [] 
  • 2

    Please correct code indentation.

  • 1

    Please enter the full description of the problem.

  • @Masterzub Start by checking if the indentation in the question code matches the one that came from your file, and if not, adjust the indentation here in the question so it looks exactly the same as the one you have. Remember that in Python indentation is vital, and without knowing how you have yours is difficult to help

  • @Pabloalmeida, this is the description of the problem: https://www.urionlinejudge.com.br/judge/pt/problems/view/1557

2 answers

3

The method rjust only exists to precisely insert spaces at the beginning of a line. And you’re calling the "strip", which removes spaces in an empty string, passed to the "end", which does nothing.

Read the documentation of python’s print function, see what "Sep" and "end" are, try it in interactive mode: you’ll understand it better than if I write it here. And keep in mind that in Python, identation is not embellishment: the code above is not only syntactically invalid, as it would not be possible to know what you wanted to do there, if we did not know it is to print an array.

1

This question refers to the number problem 1557, of the site Uri Online Judge of the category Beginners.

See here the entirety of the statement.

As it is possible to notice the first paragraph of the statement says: Write an algorithm that reads an integer N (0 N 15), corresponding to the order of an integer matrix M, and construct the matrix according to the example below.

The example - the question refers to - is formed by matrize quadrada, where each line is formed by increasing geometric rate progressions 2, as shown in the above example.

To solve this question we can use the following algorithm:

n = 9999
while True:
    n = int(input())

    if n == 0:
        break

    m = list()

    # Matriz base com elemento "0"
    for i in range(n):
        m.append([])
        for j in range(n):
            m[i].append(0)

    # Inserindo os elementos pedidos
    m[0][0] = 1
    for i in range(0, n):
        if i >= 1:
            m[i][0] = m[i - 1][0] * 2

        for j in range(1, n):
            m[i][j] = m[i][j - 1] * 2

    # Exibindo a matriz formada
    T = len(str(m[n - 1][n - 1]))
    for i in range(n):
        for j in range(n):
            m[i][j] = str(m[i][j])
            while len(m[i][j]) < T:
                m[i][j] = ' ' + m[i][j]
        M = ' '.join(m[i])
        print(M)
    print()

Note that the primeiro aninhamento block for will be responsible for creating a base matrix with elements "0".

Already the segundo aninhamento block for will be responsible for assembling the matrix with the appropriate values.

The terceiro aninhamento block for is responsible for displaying the matrix in tabular form.

This code has already been tested, submitted and properly approved on the website Uri.

Browser other questions tagged

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