Fix the slugify helper for article URL slugs
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
AtoZbecome lowercase. - Only the characters
atozand0to9are kept. Every other character is a separator: spaces, tabs, newlines, punctuation, hyphens, underscores, and letters outsideatozsuch 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) -> strExample 1
Input
slugify("Hello, World!!")Output
"hello-world"Example 2
Input
slugify(" Leading and trailing ")Output
"leading-and-trailing"Example 3
Input
slugify("Café & Crème")Output
"caf-cr-me"