I would like to generate a sequence of letters i.e. \"A\", \"DE\" \"GJE\", etc. that correspond to a number. The first 26 are pretty easy so 3 returns \"C\", 26 returns \"Z\
Using the base conversion method found here. I also changed it for the lack of "0" that we have in this numbering system. End cases have been addressed.
def baseAZ(num)
# temp variable for converting base
temp = num
# the base 26 (az) number
az = ''
while temp > 0
# get the remainder and convert to a letter
num26 = temp % 26
temp /= 26
# offset for lack of "0"
temp -= 1 if num26 == 0
az = (num26).to_s(26).tr('0-9a-p', 'ZA-Y') + az
end
return az
end
irb I/O:
>> baseAZ(1)
=> "A"
>> baseAZ(26^2 + 1)
=> "Y"
>> baseAZ(26*26 + 1)
=> "ZA"
>> baseAZ(26*26*26 + 1)
=> "YZA"
>> baseAZ(26*26*26 + 26*26 + 1)
=> "ZZA"