Fix the bio preview for a profile card
Your task
Your profile card shows a short preview of each user's bio. The first version was Go, where len(bio) counts bytes and bio[:20] cuts bytes. It cut emoji in half and shortened bios that were already short. The new version is meant to count characters, not bytes.
An assistant wrote preview_by_runes(bio, max_runes). It is in the editor. It has a bug. Find it and fix it.
Input and output:
bio: the user's bio.max_runes: the most characters the preview may keep.- Return the preview.
Rules the tests check:
- A character here is one Unicode code point (a rune in Go). An emoji like 😀 is one code point, and so are
éand世. - A limit below 0 counts as 0.
- If the bio has at most
max_runescode points, return it unchanged, with no ellipsis. - Otherwise return its first
max_runescode points followed by…, the single ellipsis character U+2026. - So with a limit of 0, a bio that is not empty becomes
…, and an empty bio stays empty.
Examples
preview_by_runes(bio: str, max_runes: int) -> strExample 1
Input
preview_by_runes("hello", 20)Output
"hello"Example 2
Input
preview_by_runes("abcdefghij", 5)Output
"abcde…"Example 3
Input
preview_by_runes("😀😀😀😀😀😀", 3)Output
"😀😀😀…"