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

前端 未结 28 1407
[愿得一人]
[愿得一人] 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 23:00

    Read a column name from STDIN and print out its corresponding number:

    perl -le '$x = $x * 26 - 64 + ord for <> =~ /./g; print $x'
    

    Caveats: Assumes ASCII.

    EDIT: Replaced " with ' so that your shell won't interpolate $x in the string.

提交回复
热议问题