Code 102: CGI - Pattern Printing - 2 | 365 Days of Code

Опубликовано: 09 Март 2025
на канале: Code House
154
2

Coding Question: - Write a program that receives a number as input and prints it in the following format as shown below.
Examples:

Input: n = 3
Output:
1*2*3*10*11*12
--4*5*8*9
----6*7

Input: n = 4
Output:
1*2*3*4*17*18*19*20
--5*6*7*14*15*16
----8*9*12*13
------10*11

n = int(input())
temp_number = (n*n)+n
counter = 1
for i in range(n):
temp_list = []
for j in range(n-i):
temp_list.append(counter)
temp_list.append(temp_number-counter+1)
counter += 1
temp_list.sort()
temp_list = [str(each) for each in temp_list]
[print("--", end="") for k in range(i)]
print("*".join(temp_list))

Thanks