Last active
April 11, 2016 10:43
-
-
Save kei-p/1d6a43ac281e09d3ba31815e4da91346 to your computer and use it in GitHub Desktop.
CSV cordinate to index
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'pry' | |
CODES = ('A'..'Z') | |
CODES_SIZE = CODES.to_a.size | |
def index2code(n) | |
num = n | |
chars = [] | |
codes = CODES.to_a | |
begin | |
num -= 1 unless n == num | |
chars << codes[num % CODES_SIZE] | |
num = num / CODES_SIZE | |
end while num > 0 | |
chars.reverse.join | |
end | |
def code2index(code) | |
chars = code.split('') | |
num = 0 | |
chars.each.with_index do |c, i| | |
n = c.ord - CODES.first.ord | |
n += 1 if i < code.length - 1 | |
num = num * CODES_SIZE + n | |
end | |
num | |
end | |
def testc2i(cord) | |
puts "#{cord}:\t#{code2index(cord)}" | |
end | |
def testi2c(num) | |
puts "#{num}:\t#{index2code(num)}" | |
end | |
testi2c 0 | |
testi2c 1 | |
testi2c 2 | |
testi2c 25 | |
testi2c 26 | |
testi2c 27 | |
testi2c 52 | |
testi2c 77 | |
testi2c 700 | |
testi2c 701 | |
testi2c 475253 | |
puts '-' * 12 | |
testc2i 'A' | |
testc2i 'B' | |
testc2i 'Z' | |
testc2i 'AA' | |
testc2i 'AB' | |
testc2i 'AZ' | |
testc2i 'BA' | |
testc2i 'BB' | |
testc2i 'BZ' | |
testc2i 'CA' | |
testc2i 'ZA' | |
testc2i 'ZY' | |
testc2i 'ZZ' | |
testc2i 'Z' * 3 | |
testc2i 'Z' * 4 | |
testc2i 'Z' * 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment