Programming Riddle: How might you translate an Excel column name to a number?

前端 未结 28 1434
[愿得一人]
[愿得一人] 2020-11-29 22:26

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 actu

28条回答
  •  广开言路
    2020-11-29 22:51

    Sounds like a standard reduce to me:

    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 );
    }
    

提交回复
热议问题