Click here to Skip to main content
15,436,916 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
Print the following pattern in python for the given number of rows.
Pattern for N = 5
1 2 3 4 5
11 12 13 14 15
21 22 23 24 25
16 17 18 19 20
6 7 8 9 10

What I have tried:

Python
n = int(input())
upper = int(n/2)+1
lower = n - upper
start_i_lower = 0

for i in range(0, upper):
  if i != 0:
    i = i + i
  start = n*i + 1
  end = start + n

  for j in range(start, end):
    print(j, end =" ")
  start_i_lower = i
  print()

start_i_lower -= 1

for i in range(0, lower):
  if(n%2 == 0):
    start_i_lower -= 1

  start = n*(start_i_lower-1) + 1
  end = start + n

  for j in range(1, n+1):
        print(j+n, end =" ")

  print()
Posted
Updated 24-Jun-22 1:08am
v2
Comments
Richard MacCutchan 30-Sep-20 11:52am    
What is the question?
OriginalGriff 30-Sep-20 12:28pm     CRLF
And? What does it do that you didn't expect, or not do that you did? What have you tried to do to find out why? Are there any error messages, and if so, where and when? What did you do to make them happen? This is not a good question - we cannot work out from that little what you are trying to do. Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with. Use the "Improve question" widget to edit your question and provide better information.

n = int(input( ))
P = 1
for i in range(1, n + 1):
    for j in range(P, P + n):
        print(j, end=" ")
    print()
    if i == ((n + 1)//2):
        if (n % 2) != 0:
            P = n*(n - 2) + 1
        else:
            P = n * (n - 1) + 1
    elif i > ((n + 1) // 2):
        P = P - 2 * n
    else:
        P = P + 2 * n


It's a pretty simple and straightforward question. Sure a little tricky one
 
Share this answer
 
v2
Comments
CHill60 24-Jun-22 7:33am    
A word to the wise - answering "old" questions that already have solutions is often perceived as "rep point hunting" and in the past has led to accounts being suspended. My advice is to stick to posting solutions to more recent posts, and if there is already a solution posted, make sure you add commentary to explain why yours is better/different/an alternative approach.
The more optimized version will be:
Python
n = 5
h = 0
d = int(n/2)+1
for r in range(0, n):
	s=''
	if r<d:
	    h = r*2
	elif r==d:
		h = d
	else:
	    h = int(d/3)
	for c in range(1, n+1):
		s+=str(h*n+c) + ' '
	print(s)
 
Share this answer
 
v2
Try this:

Python
n = 5
h = 0
d = int(n/2)+1
for r in range(0, n):
	s=''
	for c in range(1, n+1):
		if r<d:
			h = r*2
		elif r==d:
			h = d
		else:
			h = int(d/3)
		s+=str(h*n+c) + ' '
	print(s)


Result:
1 2 3 4 5 
11 12 13 14 15 
21 22 23 24 25 
16 17 18 19 20 
6 7 8 9 10 
 
Share this answer
 
Comments
CPallini 1-Oct-20 3:31am    
5.
Maciej Los 1-Oct-20 3:51am    
Thank you, Carlo. I'm exploring new programming area and i'm truly fascinated ;)
CPallini 1-Oct-20 3:58am    
:thumbsup:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900