Write a program or function that
- checks if a variable name is valid and
- output
1
orTrue
if it is valid, 0.5
if it is valid but starts with an underscore (_
),- and
0
orFalse
if it is not valid.
Rules
- A variable name in most languages is valid if it begins with an underscore or letter (a – z, A – Z, _) and the rest of the characters are either underscores, letters, or numbers. (a – z, A – Z, 0 – 9, _)
- Output
1
orTrue
if the variable name is valid and0
orFalse
if not valid. - However, it is not good practice to start a variable with an underscore, so return
0.5
if it starts with an underscore and the name is valid. - Standard loopholes apply.
Test Cases
input | output | reason |
---|---|---|
abcdefghijklmnop |
1 |
|
_test_ |
0.5 |
starts with an underscore |
123abc |
0 |
starts with a number |
A_b1C_23 |
1 |
|
_! |
0 |
not 0.5 because it’s not valid |
magical pony1 |
0 |
no spaces |
Scoring
This is code-golf, so shortest code wins.
Bonus: −10% if your program/function outputs 0
for an empty string(""
).
__init__
; also, no, classes do not need an__init__
but typically have one \$\endgroup\$