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

143 lines
3.1 KiB
Markdown

---
id: 6994cff2290543b3aec9f50f
title: "Challenge 210: HSL Validator"
challengeType: 29
dashedName: challenge-210
---
# --description--
Given a string, determine whether it is a valid CSS `hsl()` color value.
- A valid HSL value must be in the format `"hsl(h, s%, l%)"`, where:
- `h` (hue) must be a number between 0 and 360 (inclusive).
- `s` (saturation) must be a percentage between 0% and 100%.
- `l` (lightness) must be a percentage between 0% and 100%.
- Spaces are only allowed:
- After the opening parenthesis
- Before and/or after the commas
- Before and/or after closing parenthesis
- Optionally, the value can end with a semi-colon (`";"`).
For example, `"hsl(240, 50%, 50%)"` is a valid HSL value.
# --hints--
`is_valid_hsl("hsl(240, 50%, 50%)")` should return `True`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_hsl("hsl(240, 50%, 50%)"), True)`)
}})
```
`is_valid_hsl("hsl( 200 , 50% , 75% )")` should return `True`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_hsl("hsl( 200 , 50% , 75% )"), True)`)
}})
```
`is_valid_hsl("hsl(99,60%,80%);")` should return `True`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_hsl("hsl(99,60%,80%);"), True)`)
}})
```
`is_valid_hsl("hsl(0, 0%, 0%) ;")` should return `True`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_hsl("hsl(0, 0%, 0%) ;"), True)`)
}})
```
`is_valid_hsl("hsl( 10 , 20% , 30% ) ;")` should return `True`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_hsl("hsl( 10 , 20% , 30% ) ;"), True)`)
}})
```
`is_valid_hsl("hsl(361, 50%, 80%)")` should return `False`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_hsl("hsl(361, 50%, 80%)"), False)`)
}})
```
`is_valid_hsl("hsl(300, 101%, 70%)")` should return `False`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_hsl("hsl(300, 101%, 70%)"), False)`)
}})
```
`is_valid_hsl("hsl(200, 55%, 75)")` should return `False`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_hsl("hsl(200, 55%, 75)"), False)`)
}})
```
`is_valid_hsl("hsl (80, 20%, 10%)")` should return `False`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_hsl("hsl (80, 20%, 10%)"), False)`)
}})
```
# --seed--
## --seed-contents--
```py
def is_valid_hsl(hsl):
return hsl
```
# --solutions--
```py
import re
def is_valid_hsl(hsl):
pattern = r"^hsl\(\s*(\d{1,3})\s*,\s*(\d{1,3})%\s*,\s*(\d{1,3})%\s*\)\s*;?$"
match = re.match(pattern, hsl)
if not match:
return False
hue = int(match.group(1))
sat = int(match.group(2))
light = int(match.group(3))
if not (0 <= hue <= 360):
return False
if not (0 <= sat <= 100):
return False
if not (0 <= light <= 100):
return False
return True
```