PYTHON formatting

Asked

Viewed 129 times

0

can give me a hint on how to print the following problem in the correct way?

while True:
    m = int(input())
    mlen = m
    sm = 1
    aux = 1
    matriz = []

    if m == 0:
        print()
        break

    for i in range(m):
        linha = []
        for j in range(m):
            linha.append(sm)
        matriz.append(linha)

    while m - 2 > 0:
        for i in range(aux, m - 1):
            for j in range(aux, m - 1):
                matriz[i][j] = sm + 1
        sm += 1
        aux += 1
        m -= 1
    for i in matriz:
        for j in i:
            print('{:4}'.format(j), end='')
        print('')

I need the printed matrix to have 2 spaces on the left side, 3 spaces between the values and no space at the end of each line. It is an exercise of the URI Online Judge - 1435.

    Accepted Output             Your Output
1   ··1···1···1···1         1   ···1···1···1···1
2   ··1···2···2···1         2   ···1···2···2···1
3   ··1···2···2···1         3   ···1···2···2···1
4   ··1···1···1···1         4   ···1···1···1···1
6   ··1···1···1···1···1     6   ···1···1···1···1···1
7   ··1···2···2···2···1     7   ···1···2···2···2···1
8   ··1···2···3···2···1     8   ···1···2···3···2···1
9   ··1···2···2···2···1     9   ···1···2···2···2···1
10  ··1···1···1···1···1     10  ···1···1···1···1···1

Thanks for your help!

  • 2

    Edit your question and add the full statement of the exercise, too, as it will facilitate in understanding the issue.

  • Note that the way you said what you need is not right. Values are right justified in size 3 and separated by space. So if you have the number 11, will have 1 space before due to justification and another to separate from the other elements.

2 answers

0

while True:
m = int(input())
mlen = m
sm = 1
aux = 1
matriz = []

if m == 0:
    print()
    break

for i in range(m):
    linha = []
    for j in range(m):
        linha.append(sm)
    matriz.append(linha)

while m - 2 > 0:
    for i in range(aux, m - 1):
        for j in range(aux, m - 1):
            matriz[i][j] = sm + 1
    sm += 1
    aux += 1
    m -= 1
for i in matriz:
    for pos, j in enumerate(i): # pos vai ser o endereço do valor j
        if pos == 0: # se for a primeira coluna pos = 0
            print('{:3}'.format(j), end='')
        else:
            print('{:4}'.format(j), end='')
    print('')

0

This question refers to the number problem "1435", whose title is "Matriz Quadrada I", made available by "Uri Online Judge" (Online Programming Marathon).

See here the entirety of the statement.

To solve this issue you have to know how to assemble a square matrix. Moreover, it must take into account the constraints that the problem makes us.

To solve this question, you can use the following algorithm...

# Problema: 1435
# Título: Matriz Quadrada I
# Categoria: Iniciante
# Disponibilizado por: https://www.urionlinejudge.com.br/judge/pt/problems/view/1435

while True:
    N = int(input())

    if N == 0:
        break

    resultado = []

    for i in range(N):
        tmp = []
        for j in range(N):
            tmp.append(1)
        resultado.append(tmp)

    valor = 1
    cima = 0
    esq = 0
    baixo = N - 1
    direita = N - 1

    if N % 2 == 0:
        meio = N / 2

    else:
        meio = (N + 1) / 2


    while valor <= meio:
        i = esq
        while i <= direita:
            resultado[cima][i] = valor
            resultado[baixo][i] = valor
            i += 1

        i = (cima + 1)
        while i < baixo:
            resultado[i][esq] = valor
            resultado[i][direita] = valor
            i += 1

        valor += 1
        cima += 1
        baixo -= 1
        esq += 1
        direita -= 1

    for i in range(N):
        tx = ''
        for j in range(N):
            tx += " %3d" % resultado[i][j]
        print(tx[1:])
    print("")

See here the functioning of the algorithm.

This algorithm has already been testado, submetido and properly aprovado on the website Uri, up programming language Python 3.

Browser other questions tagged

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