-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathnumericpattern134.py
More file actions
43 lines (34 loc) · 1.04 KB
/
numericpattern134.py
File metadata and controls
43 lines (34 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
def generate_pattern(rows=5):
"""
Generate a pattern dynamically where:
- Odd rows (1st, 3rd, 5th...): Increment normally
- Even rows (2nd, 4th...): Fill in reverse order at the end
Example for 5x5:
Row 1: 1-5 (block 0)
Row 2: 11-15 (block 2)
Row 3: 21-25 (block 4)
Row 4: 16-20 (block 3)
Row 5: 6-10 (block 1)
"""
pattern = []
odd_rows = (rows + 1) // 2
even_rows = rows // 2# Number of even-positioned rows
block_order = []
for i in range(odd_rows):
block_order.append(i * 2)
for i in range(even_rows - 1, -1, -1):
block_order.append(i * 2 + 1)
for block in block_order:
row = []
start = block * rows + 1
for j in range(rows):
row.append(start + j)
pattern.append(row)
return pattern
def print_pattern(pattern):
"""Print the pattern in a formatted grid"""
for row in pattern:
print(' '.join(f'{num:3d}' for num in row))
n = int(input())
pattern = generate_pattern(n)
print_pattern(pattern)