高尔夫代码:七个细分 [英] Code Golf: Seven Segments
问题描述
按字符计数的最短代码,用于生成给定十六进制数字的七段显示表示.
The shortest code by character count to generate seven segment display representation of a given hex number.
仅由数字[0-9]和十六进制字符(大写和小写[a-fA-F])组成.无需处理特殊情况.
Input is made out of digits [0-9] and hex characters in both lower and upper case [a-fA-F] only. There is no need to handle special cases.
使用那些 ASCII 面,输出将是输入的七段表示:
Output will be the seven segment representation of the input, using those ASCII faces:
_ _ _ _ _ _ _ _ _ _ _ _
| | | _| _| |_| |_ |_ | |_| |_| |_| |_ | _| |_ |_
|_| | |_ _| | _| |_| | |_| _| | | |_| |_ |_| |_ |
限制
禁止使用以下内容:eval,exec,system,figlet,厕所和外部库.
Restrictions
The use of the following is forbidden: eval, exec, system, figlet, toilet and external libraries.
Input:
deadbeef
Output:
_ _ _ _ _
_||_ |_| _||_ |_ |_ |_
|_||_ | ||_||_||_ |_ |
Input:
4F790D59
Output:
_ _ _ _ _ _
|_||_ ||_|| | _||_ |_|
|| | _||_||_| _| _|
代码计数包括输入/输出(即完整程序).
Code count includes input/output (i.e full program).
推荐答案
Perl,134个字符
所有换行符都可以删除.
Perl, 134 characters
All linebreaks may be removed.
@s=unpack"C*",~"P\xdbLI\xc3a`[\@AB\xe0t\xc8df";
$_=<>;for$s(6,3,0){print map$s[hex$&]&1<<$_+$s?$_%2?"_":"|":" ",
0..2while/./g;print$/}
说明
@s
存储每个段的位.条目的顺序是从0到F(感谢 hex()
),并且位按以下顺序映射到段:
Explanation
@s
stores the bits for each segment. The entries are in order from 0 to F (thanks to hex()
) and the bits map to segments in this order:
6 7 x
3 4 5
0 1 2
,其中 0
是LSB.(未使用位6).值存储在位反转的字符串中,因此有更多可打印的字符.〜
运算符将这些位翻转并解压缩为我提供数字(涉及字符串时,perl的按位运算符更加笨拙).
with 0
being the LSB. (Bit 6 is unused). The values are stored packed in the string bit-inverted so there are a lot more printable characters; the ~
operator flips the bits and unpack gives me numbers (perl's bitwise operators are much clumsier when it comes to strings).
掌握数据后,我读取了输入,并进行了三次遍历.三个循环之间的唯一区别是所需的位掩码.对于输入的每个字符,将打印输出的三个字符.要打印的字符是
With the data in hand I read the input and proceed to loop over it three times; the only difference between the three loops is the bitmask required. For each character of input three characters of output are printed. The character to be printed is
$s[ hex $& ] & (1 << ($_ + $s) )
? ($_ % 2 ? "_" : "|" )
: " "
其中 @s
是查找表, $ s
是根据行而产生的转移,而 $ _
是是否在行中打印第一个,第二个或第三个字符.如果查找表条目中的右位为false,则会打印一个空格;否则,它将显示一个空格.否则,它会打印一个"|"在侧面或中间是"_".
where @s
is the lookup table, $s
is the shift in effect depending on the row, and $_
is whether we're printing the 1st, 2nd, or 3rd character in the row. If the right bit in the lookup table entry is false it prints a space; otherwise it prints a "|" on the sides or a "_" in the middle.
这篇关于高尔夫代码:七个细分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!