代码高尔夫:数值相当于Excel列名 [英] Code Golf: Numeric equivalent of an Excel column name

查看:103
本文介绍了代码高尔夫:数值相当于Excel列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

挑战



按字符数计算的最短代码将输出Excel列字符串的数值。



例如, A 列为1, B 为2,依此类推。一旦您点击 Z ,下一列将变为 AA ,然后 AB 等等。



测试用例:



  A: 1 
B:2
AD:30
ABC:731
WTF:16074
ROFL:326676

代码计数包括输入/​​输出(即完整程序)。

解决方案

h2> Perl, 34

15 11个字符

  $ _ =()= A .. $ _ 

用法:

  $ echo -n WTF | perl -ple'$ _ =()= A .. $ _'
16074

通过使用echo -n来减少到17,以避免 chop call。



通过使用say而不是



使用-p而不是说,减少到11。



说明:
A 在字符串上下文中进行评估, A .. $ _ 构建一个以A开始的字符串和字符串递增的列表到输入字符串。 Perl在字母上下文中解释 ++ 操作符(因此 .. ),例如 $ _ =AZ; $ _ ++;打印输出 BA



=()= (又名 goatse操作符)强制表达式在列表上下文中进行求值,并返回该表达式返回的元素数量,即 $ scalar =()=< expr> 对应于 @list =< expr> ;; $ scalar = @list


The challenge

The shortest code by character count that will output the numeric equivalent of an Excel column string.

For example, the A column is 1, B is 2, so on and so forth. Once you hit Z, the next column becomes AA, then AB and so on.

Test cases:

A:    1
B:    2
AD:   30
ABC:  731
WTF:  16074
ROFL: 326676

Code count includes input/output (i.e full program).

解决方案

Perl, 36 34 33 31 30 17 15 11 characters

$_=()=A..$_

Usage:

$ echo -n WTF | perl -ple '$_=()=A..$_'
16074

Reduced to 17 by using echo -n to avoid a chop call.

Reduced to 15 by using say instead of print.

Reduced to 11 by using -p instead of say.

Explanation: A is evaluated in string context and A..$_ builds a list starting at "A" and string-incrementing up to the input string. Perl interprets the ++ operator (and thus ..) on strings in an alphabetic context, so for example $_="AZ";$_++;print outputs BA.

=()= (aka "goatse" operator) forces an expression to be evaluated in list context, and returns the number of elements returned by that expression i.e., $scalar = () = <expr> corresponds to @list = <expr>; $scalar = @list.

这篇关于代码高尔夫:数值相当于Excel列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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