Skip to content

Instantly share code, notes, and snippets.

@uu59
Created November 3, 2011 05:02
Show Gist options
  • Save uu59/1335810 to your computer and use it in GitHub Desktop.
Save uu59/1335810 to your computer and use it in GitHub Desktop.
# -- coding: utf-8
require "rubygems"
require "rspec"
describe do
def to_num(chars)
table = ("A".."Z").to_a
chars = chars.split(//u)
count = 1
chars.reverse.inject(0){|r, c|
r += (table.index(c)+1) * count
count *= 26
r
}
end
it "A == 1" do
to_num("A").should == 1
end
it "B == 2" do
to_num("B").should == 2
end
it "Z == 26" do
to_num("Z").should == 26
end
it "AA == 27" do
to_num("AA").should == 27
end
it "XFD == 16384" do
to_num("XFD").should == 16384
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment