Objective
Write a program or function that checks if a variable name is valid and output 1 or True
if it is valid, 0.5 if it is valid but starts with an underscore (_), and 0 or False
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 or
True
if the variable name is valid and 0 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.
Test Cases
Input
abcdefghijklmnop
Output
1
Input
_test_
Output
0.5
(starts with an underscore)
Input
123abc
Output
0
(starts with a number)
Input
A_b1C_23
Output
1
Input
_!
Output
0
(not 0.5 because it's not valid)
Input
magical pony1
Output
0
(no spaces)
Standard loopholes apply.
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\$