Ex
Exerpad
🔥0
0 XP
Lv.1 Beginner

Gradient Rectangle

Read a number N and a hex color (like #ff0000) from input.

Create an N x N grid with a "white" border (1 pixel thick). Fill the inside with a gradient that goes from "#000000" (black) on the left to the given color on the right.

To compute each gradient color, use the formula for each channel (red, green, blue):

value = int(target_value * t) where t goes from 0.0 (left) to 1.0 (right).

Use int(hex_string, 16) to convert hex to a number, and f"#{r:02x}{g:02x}{b:02x}" to format it back.

Example for N=8 with color #ff0000:

Don't change the print statements at the bottom.

python
n = int(input())
color = input()
# Parse the hex color into r, g, b components
# r2 = int(color[1:3], 16) etc.

grid = []
# Create N x N grid
# Border (row 0, row n-1, col 0, col n-1): "white"
# Inside: gradient from "#000000" to the given color
# t = (col - 1) / (n - 3) for inner columns


__grid__ = grid

# Don't change below this line
print(len(grid))
print(grid[0][0])
print(grid[1][1])
print(grid[1][n - 2])
Exerpad — Learn to Code with Fun!