algorithm - 编程谜语: How might you translate an Excel column name to a number?

标签 algorithm puzzle

我最近在一次工作面试中被要求解决一个我认为会很有趣的编程难题。它是关于将 Excel 列字母转换为实际数字,如果您还记得的话,Excel 用从 A 到 Z 的字母命名其列,然后序列为 AA、AB、AC...AZ、BA、BB 等。

您必须编写一个接受字符串作为参数(如“AABCCE”)并返回实际列号的函数。

解决方案可以是任何语言。

最佳答案

对我来说这听起来像是一个标准的 reduce:

python :

def excel2num(x): 
    return reduce(lambda s,a:s*26+ord(a)-ord('A')+1, x, 0)

C#:

int ExcelToNumber(string x) {
    return x.Aggregate(0, (s, c) => s * 26 + c - 'A' + 1 );
}

关于algorithm - 编程谜语: How might you translate an Excel column name to a number?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/763691/

相关文章:

c - C将如何解析这句话?

.net - 我应该如何将项目聚集成一个图形化有意义的有序集合?

f# - 如何在 F# 中计算 n 个序列的笛卡尔积?

C - 确定所有偶数位是否都设置为 1

algorithm - 找到平行的中位数

java - 根据可用空间计算所需的列数和行数

algorithm - 找出绳子的最小长度

algorithm - 什么是计算 3 维 i-j-k 网格图中边数的递归算法?

algorithm - 一种生成所有可能的方法来配对数据集中项目的有效方法

algorithm - 散列整数集