Printing pattern without importing modules

Viewed 109

Please help me print the pattern below as it is, if the input entered is 7:

1 1 1 1 1 1 1
1 2 2 2 2 2 1
1 2 3 3 3 2 1
1 2 3 4 3 2 1
1 2 3 3 3 2 1
1 2 2 2 2 2 1
1 1 1 1 1 1 1

I figured out to find the middle element of the pattern with any input:

rows=int(input("Enter the number of rows:"))
l=[]
for x in range(1,rows+1):
    if x%2!=0:
        l.append(x)
mid_value=len(l)

Please help me complete the above pattern...... Thanks in advance!

4 Answers

If you use a list-of-lists to store the values, the value for any specific cell can be determined by doing some basic math involving the cell indexes and the number of rows.

An illustration:

def cell_value(i, j, n_rows):
    # The value of any cell is the minimum distance
    # from its own coordinates (i, j) to the "outside" (ie,
    # an i or j less than 0 or equal to n_rows). Imagine an
    # ant in the grid. How many steps would it have to take
    # to escape the grid, using the shortest route?
    return min(
        abs(i - -1),
        abs(i - n_rows),
        abs(j - -1),
        abs(j - n_rows),
    )

N_ROWS = 7

rows = [
    [
        cell_value(i, j, N_ROWS)
        for j in range(N_ROWS)
    ]
    for i in range(N_ROWS)
]

for r in rows:
    print(*r)

Output:

1 1 1 1 1 1 1
1 2 2 2 2 2 1
1 2 3 3 3 2 1
1 2 3 4 3 2 1
1 2 3 3 3 2 1
1 2 2 2 2 2 1
1 1 1 1 1 1 1

This looks like a homework question, so I'm going to try and explain how to approach it rather than just provide code.

A few things worth noting to start:
- The pattern's symmetrical in both directions, so we can save some effort and logic by only solving the top-left quarter, and copying it to the rest.
- Each row is similar the the one before, with one added at the point where the row and column indices (i and j) are equal - rather than recalculate every row from scratch, we can take the one before as a base.

So, for the first row, make a list of 1s the length of your input (7, in this case).
Copy this for the seventh row (note: row6 = row0 won't create a copy; you'll need row6 = list(row0) )
For the second and sixth rows, take a copy of the first row. If i is equal to or greater than j and is in the first half of the row, add 1 to it. You'll need to copy that in reverse for the back half of the row. (Alternative - set the value to j+1 rather than just adding 1)

Repeat until the fourth row, and you should be done.

EDIT: code included, because it was an interesting problem

numberOfRows = int(input("Enter the number of rows:"))
listOut = [[1]*numberOfRows] * numberOfRows #grid of 1s of appropriate size
for j in range(int((numberOfRows+1)/2)): #symmetrical, so only look to the middle
    if j > 0:
        listOut[j] = list(listOut[j-1]) #copy previous row
    for i in range(int((numberOfRows+1)/2)):   
        if i>=j:
            listOut[j][i] = j+1
            listOut[j][numberOfRows-(i+1)] = j+1
    #copy current row to appropriate distance from the end
    listOut[numberOfRows-(j+1)] = list(listOut[j])
for row in listOut:
    # * for sequence unpacking, printing lists as strings w/o commas
    print(*row)

It might not be the most elegant solution but something like this should work:

n = int(input('Enter the number of rows:'))

table = [[1 for _ in range(n)] for _ in range(n)]
start = 0
end = n

while start < end:
    start += 1
    end -= 1

    for i in range(start, end):
        for j in range(start, end):
            table[i][j] += 1

for row in table:
    print(' '.join(str(ele) for ele in row))

Simple implementation without using any list

n = int(input())
x = n
for i in range((n // 2 + 1) if n % 2 and n > 1 else n //2):
    for l in range(1, i + 1):
        print(l, end=' ')

    print((str(i + 1) + ' ') * x, end='') 

    for r in range(i, 0, -1):
        print(r, end=' ')
    print()
    x -= 2

y = 1
for j in range(n // 2, 0, -1):
    for l in range(1, j):
        print(l, end=' ')

    print((str(j) + ' ') * (2 * y + 1 if n % 2 else 2 * y), end='') 

    for r in range(j-1, 0, -1):
        print(r, end=' ')
    print()
    y += 1
Related