3.1 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 6994cff2290543b3aec9f50f | Challenge 210: HSL Validator | 29 | 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.
({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.
({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.
({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.
({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.
({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.
({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.
({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.
({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.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_hsl("hsl (80, 20%, 10%)"), False)`)
}})
--seed--
--seed-contents--
def is_valid_hsl(hsl):
return hsl
--solutions--
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