2D lists

Python lists can contain different types of elements. They can have integers, strings, floating-point numbers etc. They can also contain other lists:
l = [4, 5, 'hi', 3.14, ['how', 'are', 'you', '?'], 64]
print(l[2])       # hi
print(l[2][0])    # h
inner = l[4]
print(inner[1])   # are
print(l[4][1])    # are
This is an example of a list in python that contains another list in it. The operations with the nested list are exactly the same as the ones with the outer one. One can add elements with append, or remove them with pop, etc. Therefore the indexing would work like we are accessing a nested list from within a list.
We can even keep a matrix of numbers this way:
multiplication_table = [
	[1, 2, 3, 4, 5, 6, 7, 8, 9],
	[2, 4, 6, 8, 10, 12, 14, 16, 18],
	[3, 6, 9, 12, 15, 18, 21, 24, 27],
	[4, 8, 12, 16, 20, 24, 28, 32, 36],
	[5, 10, 15, 20, 25, 30, 35, 40, 45],
	[6, 12, 18, 24, 30, 36, 42, 48, 54],
	[7, 14, 21, 28, 35, 42, 49, 56, 63],
	[8, 16, 24, 32, 40, 48, 56, 64, 72],
	[9, 18, 27, 36, 45, 54, 63, 72, 81]
]

print(multiplication_table[2])
# [3, 6, 9, 12, 15, 18, 21, 24, 27]
notion image
 

Challenge

Given a square of size , you are asked to divide the square into 3 parts. The one above its diagonal, the one on the diagonal, and the one below it.
The fields above should be painted with 0, the fields on the diagonal should be painted with 1, and the ones below should be in 2.
The input of the program contains a single integer n - the size of the square.
The output of the program should contain the painted square.
Input
Output
4
1 0 0 0 2 1 0 0 2 2 1 0 2 2 2 1
 

Constraints

Time limit: 2 seconds

Memory limit: 512 MB

Output limit: 1 MB

To check your solution you need to sign in
Sign in to continue