Stripe CodingFree previewFree
Bitmap Font Renderer
Last asked
Problem
Take a bitmap-style ASCII glyph alphabet and render a string. Part 2 enforces a line-wrap width; later parts add a decoder/encoder that converts an arbitrary input through the bitmap glyphs.
You are building a text rendering system that displays characters using bitmap fonts. A bitmap font stores each character as a grid of pixels where a pixel is either on (filled) or off (empty). A character grid is a list of equal-length strings containing only 0 (off) and 1 (on); a font is a dict mapping each character name to its grid, e.g. {"face": "Simple Font", "chars": {"H": ["10001", ...], ...}}.
Part 1 — render one character
Convert a 0/1 grid into a picture, mapping 0 -> . (empty) and 1 -> # (filled). Returns a list of strings. Must work for any grid size.
Part 2 — render a word
Join the per-character grids side-by-side (no inter-letter spacing) into a single multi-row grid, then convert to symbols. All characters in a font share one height but may have different widths.
Part 3 — compressed (RLE) fonts
Decode Run-Length-Encoded rows back into full 0/1 strings, then render as in Part 1. Each row stores alternating run lengths starting from off: digits 0-9 mean lengths 0-9, letters a-z mean lengths 10-35 (a=10). Candidates rarely finish all three parts.
Canonical signatures
def render_character(grid: list[str]) -> list[str]:
# Map each cell: "0" -> ".", "1" -> "#". Any other char is invalid.
# Empty grid -> [], empty row -> "".
...
def render_word(text: str, font: dict) -> list[str]:
# Empty text -> []. Height = len of the first char's grid.
# Concatenate the binary rows of every glyph FIRST (no spacing),
# THEN convert to "."/"#" via render_character. Glyphs may differ in width.
...
def char_to_length(c: str) -> int:
# "0"-"9" -> 0..9 ; "a"-"z" -> 10..35 (ord(c) - ord('a') + 10).
...
def decode_rle(encoded_rows: list[str]) -> list[str]:
# Each row: runs alternate off->on->off..., ALWAYS starting off (0).
# Emit char_to_length(c) copies of the current pixel, then flip.
# A run of length 0 emits nothing but STILL flips the next pixel state.
# All decoded rows end up the same width.
...
def render_word_rle(text: str, font: dict) -> list[str]:
# decode_rle each glyph, concatenate row-by-row, then render_character.
...Clarifying questions worth asking
- Is there any spacing between letters? (Canonical: no; glyphs are flush.)
- Are all glyph heights equal? (Canonical: yes.)
- What if a letter is missing from the font, or the text contains newlines?
Examples
Example 1
Input
render_character(["0001000", "0101000"])
Output
["...#...", ".#.#..."]
Explanation
Part 1 example.
Example 2
Input
render_word("HI", font) with H = ["10001","10001","11111","10001","10001"] and I = ["111","010","010","010","111"]
Output
the two glyphs flush side-by-side ("#...####", "#...#.#.", ...)
Explanation
Part 2 example — glyphs concatenated with no inter-letter spacing.
Example 3
Input
decode_rle(["532", "19"])
Output
["0000011100", "0111111111"]
Explanation
Part 3 example: row "532" = 5 off, 3 on, 2 off; row "19" = 1 off, 9 on.
Follow-ups
Bonus - dispatch by font type: a single entry point that inspects the font and routes raw-binary fonts to
render_wordvs RLE fonts torender_word_rle, raising on unsupported fonts. The canonical dispatcher branches on the font's face name against a known set (raw-binary faces vs the RLE face) and raisesValueErroron anything else; checking an encoding key instead is an equivalent shape; clarify which the font schema exposes.
Loading the solution…
