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

2.1 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
696655d24b614176d4c9b789 Challenge 166: Hex Validator 29 challenge-166

--description--

Given a string, determine whether it is a valid CSS hex color. A valid CSS hex color must:

  • Start with a #, and
  • be followed by either 3 or 6 hexadecimal characters.

Hexadecimal characters are numbers 0 through 9 and letters a through f (case-insensitive).

--hints--

is_valid_hex("#123") should return True.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_hex("#123"), True)`)
}})

is_valid_hex("#123abc") should return True.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_hex("#123abc"), True)`)
}})

is_valid_hex("#ABCDEF") should return True.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_hex("#ABCDEF"), True)`)
}})

is_valid_hex("#0a1B2c") should return True.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_hex("#0a1B2c"), True)`)
}})

is_valid_hex("#12G") should return False.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_hex("#12G"), False)`)
}})

is_valid_hex("#1234567") should return False.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_hex("#1234567"), False)`)
}})

is_valid_hex("#12 3") should return False.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_hex("#12 3"), False)`)
}})

is_valid_hex("fff") should return False.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_hex("fff"), False)`)
}})

--seed--

--seed-contents--

def is_valid_hex(s):

    return s

--solutions--

def is_valid_hex(s):
    if not s.startswith("#"):
        return False

    hex_part = s[1:]
    if len(hex_part) not in (3, 6):
        return False

    for char in hex_part:
        if not char.lower() in "0123456789abcdef":
            return False

    return True