编程谜语:如何将 Excel 列名称转换为数字?
最近,我在一次工作面试中被要求解决一个编程难题,我认为分享这个难题会很有趣。 这是关于将 Excel 列字母转换为实际数字,如果您还记得的话,Excel 用从 A 到 Z 的字母命名其列,然后顺序为 AA、AB、AC...AZ、BA、BB 等。
您必须编写接受字符串作为参数(如“AABCCE”)并返回实际列号的函数。
该解决方案可以采用任何语言。
I was recently asked in a job interview to resolve a programming puzzle that I thought it would be interesting to share. It's about translating Excel column letters to actual numbers, if you recall, Excel names its columns with letters from A to Z, and then the sequence goes AA, AB, AC... AZ, BA, BB, etc.
You have to write a function that accepts a string as a parameter (like "AABCCE") and returns the actual column number.
The solution can be in any language.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(28)
对我来说听起来像是一个标准的简化:
Python:
C#:
Sounds like a standard reduce to me:
Python:
C#:
我很久以前为一些 Python 脚本写过这个:
I wrote this ages ago for some Python script:
从 STDIN 读取列名并打印出其相应的编号:
注意事项:采用 ASCII。
编辑:将
"
替换为'
,这样您的 shell 就不会在字符串中插入$x
。Read a column name from STDIN and print out its corresponding number:
Caveats: Assumes ASCII.
EDIT: Replaced
"
with'
so that your shell won't interpolate$x
in the string.巧合的是我用javascript解决了同样的问题
http://jsfiddle.net/M7Xty/1/
Coincidentally I've solved the same problem using javascript
http://jsfiddle.net/M7Xty/1/
哈 - 已经在我们的代码库中写入了它 - 大约 3 次不同的时间:(
谜语是它实际上不是数字的 Base26 表示(我们在这里的函数名称中欺骗了自己),因为其中没有 0 :
顺序是
A、B、C ... Z、AA、AB、AC
而非:
A、B、C ...Z、BA、BB、BC
(语言是 Erlang,mais oui)。
Hah - written it already in our code base - about 3 different times :(
The riddle is that it isn't actually a Base26 representation of a number (we are lying to ourselves in our function name here) because there is no 0 in it.
The sequence is:
A, B, C ... Z, AA, AB, AC
and not:
A, B, C ...Z, BA, BB, BC
(the language is Erlang, mais oui).
您可以在 C 中执行此操作,如下所示:
无错误检查,仅适用于大写字符串,字符串必须以 null 结尾。
You can do this in C like this:
No error checking, only works for upper case strings, string must be null terminated.
从 Java 名称中获取列号
:
Get the column number from its name
Java:
假设 A 列 = 1
Assuming column A = 1
从 Java 中的 int 获取列名(在此处阅读更多信息):
Get a column name from an int in Java(read more here):
警告:这两个版本都仅假定大写字母 A 到 Z。任何其他情况都会导致计算错误。 添加一些错误检查和/或大写来改进它们并不难。
斯卡拉·
哈斯克尔
Caveat: both of these versions assume only uppercase letters A to Z. Anything else causes a miscalculation. It wouldn't be hard to add a bit of error checking and/or uppercasing to improve them.
Scala
Haskell
另一个德尔福的:
Another Delphi one:
另一个Java:
Another Java:
简单的Java解决方案-->
Easy Java solution -->
将字符串视为以 26 为基数、用 A、B、... Z 表示的数字的列号的反转是否有帮助?
Does it help to think of the string as the reverse of the column number in base 26 with digits represented by A, B, ... Z?
这基本上是一个以 26 为基数的数字,不同之处在于该数字不使用 0-9 和字母,而是仅字母。
This is basically a number in base 26, with the difference that the number doesn't use 0-9 and then letters but only letters.
这是一个 CFML:
因为我的心情很奇怪,所以这里有一个 CFScript 版本:
Here's a CFML one:
And because I'm in an odd mood, here's a CFScript version:
另一个[更神秘的] erlang 示例:
和反函数:
another [more cryptic] erlang example:
and inverse function:
德尔福:
-阿尔。
Delphi:
-Al.
稍微相关的是,更好的挑战是相反的:给定列号,找到字符串形式的列标签。
我为 KOffice 实现的 Qt 版本:
Slightly related, the better challenge is the other way around: given the column number, find the column label as string.
Qt version as what I implemented for KOffice:
Common Lisp:
编辑:逆运算:
Common Lisp:
edit: the inverse operation:
该版本纯粹是功能性的,并且允许替代的“代码”序列,例如,如果您只想使用字母“A”到“C”。 在 Scala 中,根据 dcsobral 的建议。
This version is purely functional and permits alternative 'code' sequences, for example if you wanted to only uses the letters 'A' to 'C'. In Scala, with a suggestion from dcsobral.
ps 我的第一个 Python 脚本!
p.s. My first Python script!
在数学中:
In Mathematica:
使用Mr. Wizard 令人敬畏的Mathematica 代码,但摆脱了神秘的纯函数!
Using Mr. Wizard's awesome Mathematica code, but getting rid of the cryptic pure function!
维基百科有很好的解释和算法
http://en.wikipedia.org/wiki/Hexavigesimal
Wikipedia has good explanations and algos
http://en.wikipedia.org/wiki/Hexavigesimal
...只需要一个 PHP 解决方案。 这就是我想到的:
当然,只需将一些内容组合到 foreach 循环中的一行代码中,就可以稍微缩短脚本:
…just needed a solution for PHP. This is what I came up with:
Of course the script can be shortened a little by just combining some stuff into one line of code within the foreach loop:
在Python中,不使用reduce:
In Python, without reduce:
下面是此代码的 Python 版本:
Here is another version of this code in Python: