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.
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.
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
arraysize12are valid but
If underscores are present, they must appear at the beginning of the name, like in
_VERSIONbut unlike in
For case sensitive names, variables like
HeLlObOySaren't counted. A name must be either all uppercase (
HELLOBOYS) or all lowercase (
There must be at least one alphabetic character. This means
_________a9999999999is valid but
Regexs are, of course, allowed;
Blame Tom's meticulousness.
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
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
2is correct. Valid names are either
<character><character>. Without case sensitivity, we have
26 + 26 * 10 + 26 * 26 = 962combinations. With case sensitivity, we have
52 + 2 * 26 * 26 + 52 * 10 = 1924combinations. The difference is
962, but you state
926. Typo? \$\endgroup\$
%wmatches alphanumerics, so your pattern allows for e.g.
%afor letters. \$\endgroup\$