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

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

    another [more cryptic] erlang example:

    col2int(String) -> col2int(0,String).
    col2int(X,[A|L]) when A >= 65, A =< 90 ->
    col2int(26 * X + A - 65 + 1, L);
    col2int(X,[]) -> X.
    

    and inverse function:

    int2col(Y) when Y > 0 -> int2col(Y,[]).
    int2col(0,L) -> L;
    int2col(Y,L) when Y rem 26 == 0 -> 
       int2col(Y div 26 - 1,[(26+65-1)|L]);
    int2col(Y,L) ->
       P = Y rem 26,
       int2col((Y - P) div 26,[P + 65-1|L]).
    

提交回复
热议问题