Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

1.9 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
68b06e589bf2273243814775 Challenge 38: Slug Generator 29 challenge-38

--description--

Given a string, return a URL-friendly version of the string using the following constraints:

  • All letters should be lowercase.
  • All characters that are not letters, numbers, or spaces should be removed.
  • All spaces should be replaced with the URL-encoded space code %20.
  • Consecutive spaces should be replaced with a single %20.
  • The returned string should not have leading or trailing %20.

--hints--

generate_slug("helloWorld") should return "helloworld".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(generate_slug("helloWorld"), "helloworld")`)
}})

generate_slug("hello world!") should return "hello%20world".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(generate_slug("hello world!"), "hello%20world")`)
}})

generate_slug(" hello-world ") should return "helloworld".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(generate_slug(" hello-world "), "helloworld")`)
}})

generate_slug("hello world") should return "hello%20world".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(generate_slug("hello  world"), "hello%20world")`)
}})

generate_slug(" ?H^3-1*1]0! W[0%R#1]D ") should return "h3110%20w0r1d".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(generate_slug("  ?H^3-1*1]0! W[0%R#1]D  "), "h3110%20w0r1d")`)
}})

--seed--

--seed-contents--

def generate_slug(str):

    return str

--solutions--

import re
def generate_slug(s):
    cleaned = re.sub(r'[^a-zA-Z0-9 ]+', '', s)
    cleaned = re.sub(r'\s+', ' ', cleaned).strip()
    return cleaned.lower().replace(' ', '%20')