Draw a pixel grid as text

An image is a grid of numbers. This program draws a small grayscale image with characters.

What to build

A program that prints a grid of brightness values as text and the mean brightness.

Requirements

  • Use the ten shades .:-=+*#%@, darkest first. The shade of a value v is the character at position v * 10 // 256.
  • Print each row between two | characters.
  • Then print mean M, the average of all values, to one decimal place.

Input

Line 1: h w. Then h lines of w integers from 0 to 255.

Output

h rows, then the mean line.

Constraints

1 ≤ h, w ≤ 20.

Example

Input:

2 3
0 128 255
64 192 32

Output:

| +@|
|:#.|
mean 111.8

Why it works

Dividing by 256 splits the range 0 to 255 into ten equal bands, one per shade, and the mean adds up every pixel and divides by how many there are.

Examples

Example 1 · Input
2 3
0 128 255
64 192 32
Expected output
| +@|
|:#.|
mean 111.8
Example 2 · Input
1 1
255
Expected output
|@|
mean 255.0

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

Turn a brightness from 0 to 255 into a position in the shade string with `value * 10 // 256`.

Hint 2

The shades from dark to bright are in the string ` .:-=+*#%@` (it starts with a space).

Solution

Show one correct solution (try it yourself first)
SHADES = " .:-=+*#%@"
h, w = map(int, input().split())
total = 0
for _ in range(h):
    row = list(map(int, input().split()))
    total += sum(row)
    print("|" + "".join(SHADES[v * 10 // 256] for v in row) + "|")
print(f"mean {total / (h * w):.1f}")

Lesson: Image generation →