编程之谜:如何可能你翻译一个Excel列名称为多少? [英] Programming Riddle: How might you translate an Excel column name to a number?

查看:273
本文介绍了编程之谜:如何可能你翻译一个Excel列名称为多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我问面试来解决,我认为这将是有趣的分享编程的难题。这是关于转换的Excel列字母实际的数字,如果你还记得,Excel中的名称与字母列从A到Z,然后序列进行AA,AB,AC ... AZ,BA,BB等。

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.

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

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.

推荐答案

我这个年龄前写了一段Python脚本:

I wrote this ages ago for some Python script:

def index_to_int(index):
    s = 0
    pow = 1
    for letter in index[::-1]:
        d = int(letter,36) - 9
        s += pow * d
        pow *= 26
    # excel starts column numeration from 1
    return s

这篇关于编程之谜:如何可能你翻译一个Excel列名称为多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆