Skip to content
This repository was archived by the owner on Oct 7, 2021. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Python/zigzag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
print("Enter the number of rows and columns(m n): ", end="")
m, n = map(int, input().split())
# taking input for number of rows and columns

pattern = [[0]*n for i in range(m)]
# empty list of size m by n

step = 1 # for incrementing or decrementing row counter
row=0

for col in range(n):
pattern[row][col] = col+1
if (row == m-1):
step=-1
# if row reaches the bottom, change step to -1
elif (row == 0):
step=1
# if row reaches the top, change step to -1
row+=step # add step to row counter

max_width = len(str(n))
# maximum characters which can be occupied by a number
# this will be used for correct formatting of pattern

for row in pattern:
for i in row:
if (i==0):
print(" "*max_width, end="")
# if the position is empty, print spaces
else:
print(str(i)+" "*(max_width - len(str(i))), end="")
# printing the number with appropriate spaces at its end
print()