I am working on a project in which I want to find a combination of letters from a number.
For example:
1 = A
2 = B
26 = Z
27 = A A
52 = A Z
53 = B A
It would be greatly appreciated if anyone knows how to make something like this work.
I am working on a project in which I want to find a combination of letters from a number.
For example:
1 = A
2 = B
26 = Z
27 = A A
52 = A Z
53 = B A
It would be greatly appreciated if anyone knows how to make something like this work.
Your question is not clear (at least for me). I don’t understand exactly what you want, but i think you have a number and you want to access to a letter combination? You can have a table that look like this:
local Comb = {
[1] = "A",
[2] = "B",
[27] = "A B",
}
local number = 2
print(Comb[number]) --prints B since number 2 represent "B" in the table.
I should have just explained it like this:
Basically I want a system like in google sheets where the letters keep going
So you want to generate ALL of the combinations possible from the alphabet?
Only the combination I tell it to generate from a number like 29 which would be AC
This is pretty simple. I can sketch it up tomorrow if you wish, but it looks like base 26 numbering system. Conversion shouldn’t be hard and you can likely find examples.
If I haven’t marked this thread “solved” by tomorrow then it would be appreciated.
First you need a table with all of the letters from alphabet in the order, you would need to make it yourself since i think there is no way for system to know alphabet.
local Comb = {
[1] = "A",
[2] = "B",
[3] = "C",
[4] = "D",
-- so on
}
Then you can use this function i made in like 8 minutes to generate a letter by number. (see example below)
local Comb = {
[1] = "A",
[2] = "B",
[3] = "C",
[4] = "D",
-- so on
}
local function GetCombFromNumber(num)
local stringToReturn = ""
if Comb[num] then
stringToReturn = Comb[num]
else
local First = math.floor(num/26)
local second = num - (26 * First)
First = Comb[First]
second = Comb[second]
stringToReturn = First.." "..second
end
return stringToReturn
end
--Example
local Combination = GetCombFromNumber(27)
print(Combination) --prints A A
Let me know if this work since i can’t test it right now but it looks like it should work.
The only issue with this script is that it only support all combinations up to “Z Z” and not further. I could try to improve it, if needed.
Please fix any typos if there any. I wrote everything on the phone and there might be mistakes
This will work and will go above “ZZ”, but keep ShkatulkaGames as the solution as his works and does what you needed.
local lettercombo = {}
local letters = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}
local times = 0
local wantedtimes = 26 * 5
repeat
times += 1
local letter = ""
if times >= 1 and times <= 26 then
letter = letters[times]
else
local divided = math.floor(times / 26)
local starttimes = divided
if times / 26 == divided then
starttimes -= 1
end
repeat
letter ..= "A"
starttimes -= 1
until starttimes == 0
local letternum = times - (26 * divided)
if letternum > 0 then
letter ..= letters[letternum]
else
if times / 26 == divided then
letter ..= "Z"
else
letter ..= "A"
end
end
end
lettercombo[times] = letter
until times == wantedtimes
--do whatever with it.
@Cheese127 If this is still useful to you at all, here is a proper to and from base converter. I’ve added comments to try to explain what I’m doing. These guys did extremely well, but it doesn’t actually require quite so many lines for any of the individual functions. I’ve tested it and verified that it works as intended. I know it’s a lot more than you need, but for what it’s worth it’s able to scale up until Lua finds the numbers invoved are too large to handle.
Edit: I’m not smart. A true base converted needs to start at 0, and since this is a true base converter it will not progress like you want. The progression here will be
A-Z, BA-ZZ, BAA-ZZZ
I assume you wanted this
A-Z, AA-ZZ, AAA-ZZZ
Sorry about that. I’m not going to try and fix it since it’s already solved, but here’s the script anyways for reference.
local toTable do -- Create a new scope to hide the ugly. Just for organization
toTable = {}
for i = 65, 90 do toTable[i-65] = string.char(i) end -- The same as writing {"A", "B", "C", etc
end
local fromTable do -- Hide the ugly. I always do this when the creation of a variable takes up multiple lines. Just preference.
fromTable = {}
for i, v in pairs(toTable) do fromTable[v] = i end -- The same as writing {A = 1, B = 2, C = 3, etc
end
print(toTable, fromTable)
local function from26(n) -- With any base of numbering, you can resolve it into another base by multiplying each digit by the base to the power of the number of digit it is. In base 26, the right-most letter is worth 1 time the letter, the second is worth 26 time the letter, and the third is worth 26*26 times the letter.
local output = 0
local e = 0 -- Exponent
for i = #n, 1, -1 do -- Starting from the right end since our exponent is zero.
output += fromTable[n:sub(i,i)]*26^e
e+=1 -- Increase the exponent for the next digit.
end
return output
end
local function to26(n)
local output = ""
while n > 0 do
output ..= toTable[n%26] -- The % is modular division. It's basically the remainder after dividing by 26. If I divide the number by 26 and the remainder is 3, then this digit is a C.
n = math.floor(n/26) -- Do the actual division. We round down because we've already used the decimal for the remainder bit. Repeat until 0.
end
return output:reverse() -- This function puts the output in reverse, so correct it.
end