Fix the slugify helper for article URL slugs

Review the AI's codeEasy10 min

Your task

An AI agent wrote a slugify helper that turns an article title into the URL slug your publish flow uses, such as hello-world. It looked fine at a glance. Before it goes in, your team checks it against the slug rules.

An assistant wrote slugify(title). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • title: the article title. It can hold any characters.
  • Return the slug.

Rules the tests check:

  • The letters A to Z become lowercase.
  • Only the characters a to z and 0 to 9 are kept. Every other character is a separator: spaces, tabs, newlines, punctuation, hyphens, underscores, and letters outside a to z such as é.
  • A run of separators between two kept characters becomes a single -.
  • Separators before the first kept character or after the last one produce nothing, so a slug never starts or ends with -.
  • A title with no kept characters gives an empty string.

Examples

slugify(title: str) -> str
Example 1
Inputslugify("Hello, World!!")
Output"hello-world"
Example 2
Inputslugify(" Leading and trailing ")
Output"leading-and-trailing"
Example 3
Inputslugify("Café & Crème")
Output"caf-cr-me"