Fix the bio preview for a profile card

Review the AI's codeEasy12 min

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_runes code points, return it unchanged, with no ellipsis.
  • Otherwise return its first max_runes code 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) -> str
Example 1
Inputpreview_by_runes("hello", 20)
Output"hello"
Example 2
Inputpreview_by_runes("abcdefghij", 5)
Output"abcde…"
Example 3
Inputpreview_by_runes("😀😀😀😀😀😀", 3)
Output"😀😀😀…"