Click here to Skip to main content
15,867,568 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 13-May-23 8:23am
v2
Comments
Richard MacCutchan 30-Sep-20 11:52am    
What is the question?
OriginalGriff 30-Sep-20 12:28pm    
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.

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:
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
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.
n = int(input())
if n % 2 == 0:
    n1 = int(n/2)
    n2 = n-n1
else:
    n1 = int(n/2)+1
    n2 = n-n1

x = 1
i = 1
i1 = 1

#Upper pattern
while i <= n1:
    for j in range(1, n+1):
        print(x, end=" ")
        x = x+1
    print()
    if i == n1:
        break
    x = x+n
    i = i+1


#Lowe Pattern
if n % 2 == 0:
    while i1 <= n2:
        for k in range(1, n+1):
            print(x, end=" ")
            x = x+1
        x=x-(2*n)
        print()
        i1 = i1+1
        x = x-n
        y=y+1

else:
    while i1 <= n2:
        x = x-(2*n)
        for k in range(1, n+1):
            print(x, end=" ")
            x = x+1
        x=x-n
        print()
        i1 = i1+1
 
Share this answer
 
v2
n = int(input())

if n%2 == 0:
m = n//2
else:
m = n//2+1

for i in range(1, m+1, 1):
for j in range(1, n+1, 1):
print(2*n*(i-1)+j, end ='')
print(' ',end = '')
print()

r = n - m
for i in range(r, 0, -1):
for j in range(1, n+1, 1):
print((2*i-1)*n + j, end ='')
print(' ', end ='')
print()
 
Share this answer
 

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