In this question Tom learned that, in general, there are many motivations to choose to include case sensitivity in his programming language, because the possible combinations for a variable name are much more than what's possible with a case insensitive programming language for a name of the same length.
But, as a more meticulous person than him pointed out, even with case sensitivity, many combinations like A_18__cDfG
are wasted anyway because no one will ever use them, so Tom decided to be an even more meticulous person and to redo the calculation of the difference of possibilities of variable names between a case sensitive programming language and a case insensitive programming language only counting the variable names that are likely to be used.
The Challenge
Write a function (or a full program if your language doesn't support them) that takes an input n
and returns (or outputs) the difference between the number of possible, valid combinations for a variable name of length n
with case sensitivity and a variable name of the same length without case sensitivity.
Valid names
You must only count names in which:
The first character is either an alphabet letter or an underscore, but never a digit;
If there are digits, they must appear at the end of the name, so that
x1
andarraysize12
are valid butiam6feettall
isn't;If underscores are present, they must appear at the beginning of the name, like in
__private
and_VERSION
but unlike in__init__
orsome_variable
;For case sensitive names, variables like
HeLlObOyS
aren't counted. A name must be either all uppercase (HELLOBOYS
) or all lowercase (helloboys
);There must be at least one alphabetic character. This means
_________a9999999999
is valid but__
or_4
arent't.
Rules
Regexs are, of course, allowed;
Blame Tom's meticulousness.
Testcases
Input (lenght of the varable name) -> Output (differences in valid combinations)
0 -> 0 (no combinations for both cases: 0 - 0 = 0)
1 -> 26
2 -> 962
3 -> 27898
4 -> 754234
5 -> 19898970
6 -> 520262106
Scoring
It's again code golf. Shortes program in bytes wins.
Reference, non-competing Lua implementation
Since the test cases may be wrong, I've been asked to include it, so here it is:
local case = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_0123456789"
local function isvalid(s) --checks if a string s is valid AND only suitable for case sensitive
if s:upper() == s then --if the string is all uppercase, it's also valid for case unsensitive
return false
end
return (s:match"^_*[%w]+[%d]*$" --checks if it matchs the underscore-character-digit sequence
and s:lower() == s) --checks if it's all lowercase
and true --trasforms to boolean value
end
local function each(s, n) --recursive function called for character at position 2..n
if n == 0 then
if isvalid(s) then
return 1
end
return 0
else
local total = 0
for i = 1, #case do
total = total + each(s..case:sub(i, i), n - 1)
end
return total
end
end
local function diff(n) --the actual function
local total = 0
if n > 0 then
for s = 1, #case - 10 do --loops for character at position 1, which can't be a digit
total = total + each(case:sub(s, s), n - 1)
end
end
print(string.format("%.0f", total)) --prints the result in non-scientific notation
end
2
is correct. Valid names are either_<character>
,<character><digit>
or<character><character>
. Without case sensitivity, we have26 + 26 * 10 + 26 * 26 = 962
combinations. With case sensitivity, we have52 + 2 * 26 * 26 + 52 * 10 = 1924
combinations. The difference is962
, but you state926
. Typo? \$\endgroup\$%w
matches alphanumerics, so your pattern allows for e.g.iam6feettall
. Use%a
for letters. \$\endgroup\$