呈现“数字时钟样式”数字


28

编写最短的程序,该程序将使用一串数字(至少 20个长度)作为输入,并使用标准的数字时钟样式数字显示输出。例如对于输入81,具有ascii输出的解决方案将给出:

 _
|_|  |
|_|  |

如果有帮助,图形输出也是可以接受的。


“将数字字符串作为输入”的上限吗?
2011年

嗯,假设在80列屏幕(20)上一行已经足够了
mootinator '02


Answers:


6

APL(Dyalog)(45)

{3 3⍴' _|'[1+⍵⊤⍨9⍴3]}¨⎕UCS'ા8धगɯે૙ࣃଏ૽'[1+⍎¨⍞]

字符串,ા8धगɯે૙ࣃଏ૽是unicode字符2750 56 2343 2327 623 2759 2777 2243 2831 2813(但是,您应该只能复制并粘贴它)。他们编码数字。程序从键盘读取一行。

说明:

  • 1+⍎¨⍞:从键盘上读取一行,将每个字符解析为一个数字,然后将每个数字加1(默认情况下,APL数组基于1)。
  • ⎕UCS'ા8धगɯે૙ࣃଏ૽'[... ]:选择属于您输入数字位数的字符,然后查找Unicode值。
  • {... :对于每个值,请执行以下操作:
  • 1+⍵⊤⍨9⍴3:从以base-3表示的值中获取前9个以3为基数的数字,然后加1(因为数组基于1)。
  • ' _|'[... ]:根据这些数字选择空格,水平线或垂直线
  • 3 3⍴:格式化为3 x 3框。

36

功能

并不是一种真正适合打高尔夫球的语言……但是无论如何,我都试图将代码的大小保持得尽可能小,这与“普通”语言的挑战截然不同。这是1555个字符3110个字节(如果编码为UTF-16; UTF-8较大)。

这是程序运行的屏幕截图。真的行 :)

由于额外的行距,在StackExchange中这看起来很丑陋,因此请考虑在浏览器的JavaScript控制台中运行以下代码来解决此问题: $('pre').css('line-height',1)

     ╓─╖ ╔╗┌─╖   ┌─╖
     ║ʜ║ ║╟┤↔╟┐ ┌┤‼╟┐
     ╙┬╜ ╚╝╘═╝│ │╘╤╝│
╔═╗  ┌┴────┐  │┌┴╖ ┌┴╖
║0║ ┌┴─┐  ┌┴─┐└┤ʜ╟─┤·╟┐
╚╤╝┌┴╖┌┴╖┌┴╖┌┴╖╘╤╝ ╘╤╝│
┌┘┌┤·╟┤ɦ╟┤·╟┤?╟ │  ┌┴╖│
│ │╘╤╝╘╤╝╘╤╝╘╤╝ └──┤‼╟┘
│┌┴╖│ ┌┴╖┌┘╔═╧╗    ╘═╝
└┤?╟┘┌┤?╟┘┌╢10║    ┌─────────┐
 ╘╤╝ │╘╤╝┌┘╚══╝ ┌─┬┘╔══╗┌──╖┌┴╖ ╓─╖
  ┘  └───┘╔════╗│┌┴┐║21╟┤>>╟┤·╟┐║ɦ║
╔════════╗║1005╟┘└┬┘╚══╝╘═╤╝╘╤╝│╙┬╜        ┌─┐
║14073768║╚════╝ ┌┴╖ ┌─╖ ┌┴╖┌┴╖└─┴─────────┘┌┴╖
║7584800 ╟───────┤?╟─┤‼╟─┤ʜ╟┤·╟─────────────┤·╟┐╔═══════╗╔══╗
╚════════╝       ╘╤╝ ╘╤╝ ╘╤╝╘╤╝    ╔══╗┌─╖┌┐╘╤╝│║2097151║║21╟┐
 ╔═══════════════╗│   ┘   │  │     ║48╟┤−╟┤├─┤┌┘╚══╤════╝╚══╝│
 ║140737555464224╟┘  ┌────┘┌┬┘     ╚══╝╘╤╝└┘┌┘│╓─╖ │┌┐┌─╖┌─╖┌┴─╖
 ╚═══════════════╝   │ ┌───┘└─────────┐┌┴─╖ │┌┘║↔║ ├┤├┤‼╟┤↔╟┤>>║
┌────────────┐┌────┐┌┴╖│┌────────────┐├┤<<║ ││ ╙┬╜┌┘└┘╘╤╝╘═╝╘╤═╝
│   ╔══╗╔═══╗├┘╓─╖ └┤·╟┘│   ╔══╗╔═══╗├┘╘╤═╝ │└─┐└─┤╔═╗┌┴╖ ┌──┘
│   ║95║║892║│┌╢‡╟┐ ╘╤╝ │   ║95║║877║│ ┌┘╔══╧═╗│  │║0╟┤?╟┬┘
│   ╚═╤╝╚═╤═╝││╙─╜│  │  │   ╚═╤╝╚═╤═╝│╔╧╗║2097║│  │╚═╝╘╤╝│
│╔══╗┌┴╖┌┐│  ││┌─╖│ ┌┴╖ │╔══╗┌┴╖┌┐│  │║1║║151 ║│  └──────┘
│║32╟┤?╟┤├┤  │└┤‼╟┘┌┤‡║ │║32╟┤?╟┤├┤  │╚═╝╚════╝│
│╚══╝╘╤╝└┘└──┴┐╘╤╝ │╘╤╝ │╚╤═╝╘╤╝└┘└──┴┐      ┌─┘
│    ┌┴╖     ┌┴╖┌─╖│ │ ┌┴╖│  ┌┴╖     ┌┴╖ ┌─╖┌┴╖
│    │‼╟─────┤·╟┤‼╟┘ │┌┤·╟┘  │‼╟─────┤·╟─┤‼╟┤‡║
└┐┌┐ ╘╤╝     ╘╤╝╘╤╝  ││╘╤╝┌┐ ╘╤╝     ╘╤╝ ╘╤╝╘╤╝
 ├┤├┐┌┴╖╔══╗  └──┐┌┐ │└┐├─┤├┐┌┴╖╔══╗  ├──┐└  │
 │└┘└┤?╟╢32║╔═══╗├┤│┌┴╖││ └┘└┤?╟╢32║╔═╧═╗│┌┐┌┴╖╔══╗
╔╧══╗╘╤╝╚══╝║881╟┘│├┤?╟┘│    ╘╤╝╚══╝║325║└┤├┤?╟╢32║
║927║╔╧══╗  ╚═══╝ └┘╘╤╝╔╧═══╗╔╧══╗  ╚═══╝ └┘╘╤╝╚══╝
╚═══╝║124╟───────────┘ ║1019║║124╟───────────┘
     ╚═══╝             ╚════╝╚═══╝

如果我没有记错的话,它可能会更小,因为输出会倒退到前面。我通过添加一个额外的功能来反转输入来解决了这一问题。否则,我可能必须重写所有内容。

我还犯了另一个错误(在两次调用中交换操作数),这有必要声明额外的函数,但是这个错误很小,适合放入主函数中,因此不添加任何字符!


8
哇,真酷。不仅解决方案,语言也是如此:-)
Joey

1
那真是太棒了
Knerd 2014年

9

wxpython,很多字符

import wx, wx.gizmos as g

class T(wx.Frame):
 def __init__(_):
  wx.Frame.__init__(_, None, size = (800, 60))
  l = g.LEDNumberCtrl(_, -1)
  l.Value = raw_input()

class M(wx.App):
 def OnInit(_):
  T().Show()
  return 1

M().MainLoop()

测试

echo -n 81 | python codegolf-997-wx.py

在此处输入图片说明

ps:不是很认真的输入,但是看起来图形输出也是可以接受的,所以我只是尝试了一下:-)


+1很高兴看到这个:)。
mootinator

1
你们太多了。:P

8

Golfscript-66个字符

"placeholder text for userscript which counts chars             ";

xxd:(xxd -r用于还原)

0000000: 332c 7b3a 533b 2e7b 3438 2d22 5e70 285d  3,{:S;.{48-"^p(]
0000010: 7025 d3c4 4ab1 7d4a b8dc 4469 ce41 2222  p%..J.}J..Di.A""
0000020: f303 227b 6261 7365 7d2f 3330 2f53 3d33  .."{base}/30/S=3
0000030: 2f3d 7b22 5f20 7c22 3d7d 257d 256e 407d  /={"_ |"=}%}%n@}
0000040: 2f3b                                     /;

这遵循了大多数其他答案,因为数字之间没有空格,尾部的空格也保持在其中。可以容易地在数字之间添加1+before {"_ |"=}%。打包为以3为底的数字,然后作为以243为底的字符串。


很公平。我更新了问题,以免打扰数字之间的不必要间隔。
mootinator

6
我认为“占位符...”有点令人困惑。
Eelvex

8

Ĵ,9078 68个字符

[ 更新:使用unicode(1字节)编码:

,./(10 3 3$((90$3)#:256#.24x-~3&u:'%ė¨ÔW/~º»sy¡ăì<t÷²'){' _|'){~"./.Y
NB. utf characters are: 37 279 168 212 87 47 126 186 187 115 121 161 259 236 60 116 247 178

像以前一样工作:]

,./(10 3 3$((90$3)#:1219424106940570763878862820444729939648410x){' _|'){~"./. '58321'
 _  _  _  _    
|_ |_| _| _|  |
 _||_| _||_   |

关键在于将数字编码为以3为基数的整数。零,例如:

:
 _ 
| |
|_|

' _ | ||_|',变成010202212 3 = 2750。


如果J具有高精度的base-36数字,我可以保存10个字符。有任何建议吗?
Eelvex

我现在正在使用二进制文件。我想知道三位一体是否能为我省些中风?
luser droog

2
祝贺您在codegolf.SE上发布了第1000个帖子!(http://codegolf.stackexchange.com/q/1000
门把手

晚了2年以上,但谢谢您:)
Eelvex

5

数学205 209 198 179

i = IntegerDigits; t = Thread; r = Rule;
z@n_ := Row@i@n /. t[r[Range[0, 9], Grid[Partition[ReplacePart[Characters@" _ |_||_|", 
t[r[#, ""]]], 3], Spacings -> 0] & 
/@ (i /@ {5, 24578, 49, 47, 278, 67, 6, 4578, , 78})]]

用法

z@1234567890

数字


5

的JavaScript(145)

148 145

由于JavaScript实际上没有标准的输入/输出,因此将其编写为接受字符串并以字符串形式返回输出的函数。

function r(n){for(i=o="",b=" |_\n|",L=n.length;i<3*L;)o+=b[(c="ǪĠòƲĸƚǚĢǺƺ".charCodeAt(n[i%L])>>(i++/L|0)*3)&1]+b[c&2]+b[c&4]+b[i%L?0:3];return o}

隔开:

function r(n)
{
    for (i = o = "", b = " |_\n|", L = n.length;   i < 3*L;   )
        o += b [ (c = "ǪĠòƲĸƚǚĢǺƺ".charCodeAt(n[i%L]) >> (i++/L|0)*3) & 1 ] +
             b [ c&2 ] +
             b [ c&4 ] +
             b [ i%L ? 0 : 3 ];  // space or newline
    return o
}

运作方式如下:

  • 每个数字形状均以包含9位的Unicode字符编码。
  • 前三位用于第一行,依此类推。

  • 在每一组中的三个比特,所述第一指定的第一个字符是否是|或空间,所述第二无论是_或空间,而第三再次|或空间。

  • 这三个位作为检索c&1c&2c&4,然后将其用作索引到字符串b

  • 在每次迭代中,i%L都是“ x坐标”,即输入中的数字n

  • 在每次迭代中,i/L都是“ y坐标”,即行,但是我们需要|0将其设为整数

  • 最后,数字之间的空格和行之间的换行符也可以通过索引到b,重新使用空格字符该字符串中未使用的位置3来检索!:)


Unicode!呸!......好吧,这比我短。+1
luser droog


3

Golfscript-97个字符

:*{32' _':$@'14'{?~!=}:&~32}%n*{:x' |':|\'1237'&$x'017'&|x'56'&}%n*{:x|\'134579'&$x'147'&|x'2'&}%

使用反引号为所有数字字符串保存一个字符(我如何以代码格式插入此字符?)
2011年

@Nabb:不能— StackExchange就是这样的废话……
Timwi

3

Windows PowerShell,127

$i="$input"[0..99]
'☺ ☺☺ ☺☺☺☺☺','♠☻♥♥♦♣♣☻♦♦','♦☻♣♥☻♥♦☻♦♥'|%{$c=$_
""+($i|%{('···0·_·0··|0·_|0|_|0|_·0|·|'-split0)[$c[$_-48]]})}

由于字符串包含一些不愉快的字符,为方便起见,使用了十六进制转储:

000: 24 69 3D 22 24 69 6E 70 │ 75 74 22 5B 30 2E 2E 39  $i="$input"[0..9
010: 39 5D 0A 27 01 00 01 01 │ 00 01 01 01 01 01 27 2C  9]◙'☺ ☺☺ ☺☺☺☺☺',
020: 27 06 02 03 03 04 05 05 │ 02 04 04 27 2C 27 04 02  '♠☻♥♥♦♣♣☻♦♦','♦☻
030: 05 03 02 03 04 02 04 03 │ 27 7C 25 7B 24 63 3D 24  ♣♥☻♥♦☻♦♥'|%{$c=$
040: 5F 0A 22 22 2B 28 24 69 │ 7C 25 7B 28 27 20 20 20  _◙""+($i|%{('
050: 30 20 5F 20 30 20 20 7C │ 30 20 5F 7C 30 7C 5F 7C  0 _ 0  |0 _|0|_|
060: 30 7C 5F 20 30 7C 20 7C │ 27 2D 73 70 6C 69 74 30  0|_ 0| |'-split0
070: 29 5B 24 63 5B 24 5F 2D │ 34 38 5D 5D 7D 29 7D     )[$c[$_-48]]})}

5
看看所有的笑脸..
王鼎伟

3

gForth,186 175个字符

新版本:

: s query parse-word bounds s" D@DD@DDDDDb`ddfFF`fff`Fd`df`f`" bounds do cr 2dup do i c@ '0 - j + c@ 3 0 do dup 3 and s"  _|" drop + 1 type 4 / loop drop loop 10 +loop bye ; s

实际上,这也困扰着退出(+3个字符):)。这是更具可读性的版本,它进行了一些位打包以将LUT的大小减小1/3,但是生成的代码更加复杂,因此节省的钱不多:

: 7s query parse-word bounds 
    s" D@DD@DDDDDb`ddfFF`fff`Fd`df`f`"
    bounds do 
        cr
        2dup do
            i c@ '0 - j + c@
            3 0 do
                dup 3 and
                s"  _|" drop + 1 type
                4 / \ shorter than an rshift
            loop
            drop
        loop
    10 +loop bye ;
7s

旧版:

: s query parse-word bounds s"  _     _  _     _  _  _  _  _ | |  | _| _||_||_ |_   ||_||_||_|  ||_  _|  | _||_|  ||_|  |" bounds do cr 2dup do i c@ '0 - 3 * j + 3 type loop 30 +loop ; s

这使堆栈不平衡,并且不会费心退出解释器。这是一个更清晰易读的版本

: 7s query parse-word bounds 
    s"  _     _  _     _  _  _  _  _ | |  | _| _||_||_ |_   ||_||_||_|  ||_  _|  | _||_|  ||_|  |"
    bounds do 
        cr
        2dup do
            i c@ '0 - 3 * j + 3 type
        loop
    30 +loop 2drop bye ;
7s

2

C#369个字符

static void Main(string[] a){var b = new[] {123,72,61,109,78,103,119,73,127,111};var g = new[]{" _  ","|","_","| ","|","_","| "};a[0].ToCharArray().SelectMany((x,w)=>g.Select((y,i)=>new{s=((b[x-48]>>i&1)==1)?y:new String(' ',y.Length),j=i,v=w})).GroupBy(z=>(z.j+2)/3).ToList().ForEach(q=>Console.WriteLine(String.Join("", q.OrderBy(l=>l.v).Select(k=>k.s).ToArray())));}

我可以轻松切出几个字符。重点是滥用LINQ :)

更多空白版本:

static void Main(string[] a)
{
    var b = new[] {123, 72, 61, 109, 78, 103, 119, 73, 127, 111};
    var g = new[] { " _  ", "|", "_", "| ", "|", "_", "| " };
    a[0].ToCharArray().SelectMany(
        (x,w)=>g.Select(
           (y,i)=>new{s=((b[x-48]>>i&1)==1)?y:new String(' ',y.Length),j=i,v=w}))
            .GroupBy(z=>(z.j+2)/3).ToList().ForEach(
            q=>Console.WriteLine(
                String.Join("", q.OrderBy(l=>l.v).Select(k=>k.s).ToArray())));
}

首先,您需要一个类声明和usings(409)。然后,有一些不必要的空白可以删除(402)。该任务指出,数字之间的空格应省略(399)。
乔伊,

嗯,谢谢。我编辑了任务,因为没有人遵循原始的空格规则。如果您不满意,请随时编辑答案,因为我不在乎。
mootinator 2011年

g只能使用一次,因此可以通过内联保存7个字符。
Timwi

1
实际上b也只使用一次,因此您也可以内联它。另外,如果将整数数组更改为,则可以节省很多字符;如果"{H=mNgwI\x7fo"更改\x7f为实际的字符#127(不可打印,但允许),则可以节省很多字符。该>>会仍然工作,因为是从隐式转换charint
Timwi

此外,.ToCharArray().ToArray()都是多余的,您可以将它们删除:)
Timwi

2

Java解决方案:585570个字符

我认为我不会再尝试用Java打高尔夫球了...

import java.util.*;
public class CG997{public static void main(String[]args){
short[][]lets=new short[][]{{0,1,3,2,0,4,2,1,4},{0,0,3,0,0,4,0,0,4},{0,1,3,0,1,
4,2,1,3},{0,1,3,0,1,4,0,1,4},{0,0,3,2,1,4,0,0,4},{0,1,3,2,1,3,0,1,4},{0,1,3,2,1
,3,2,1,4},{0,1,3,0,0,4,0,0,4},{0,1,3,2,1,4,2,1,4},{0,1,3,2,1,4,0,0,4}};
String[]syms=new String[]{" ","_","|","  ","| "};
String s=new Scanner(System.in).nextLine();
for(int o=0;o<3;o++){for(char c:s.toCharArray()){for(int i =0;i<3;i++)
System.out.print(syms[lets[Short.parseShort(c+"")][i+o*3]]);
}System.out.println();}}}

您的Java版本比我的版本更好/更短:) btw!...数字时钟表示形式中的6是否在顶部有上限(_)?你输出后我很困惑!
2011年

在检查了我身后的咖啡机后,6号瓶应该有一个盖子。我将更新解决方案。
米奇

2

蟒蛇, 218 180 176

b=map(int,raw_input());a=map(int,bin(914290166014670372457936330)[2:]);c=' |_';p=lambda k:''.join(c[a[9*n+k]]+c[2*a[9*n+1+k]]+c[a[9*n+2+k]]for n in b)+'\n';print p(6)+p(0)+p(3)

带换行符:

b=map(int,raw_input())
a=map(int,bin(914290166014670372457936330)[2:])
p=lambda k:''.join(' |'[a[9*n+k]]+' _'[a[9*n+1+k]]+' |'[a[9*n+2+k]]for n in b)+'\n'
print p(6)+p(0)+p(3)

2

重击,11个字符

toilet "$i"

是的,我知道,我在作弊。

您需要安装厕所。


1
并将默认字体设置为带有7个段显示数字的字体。figlet也可以。
罗布

2

爪哇,2,095

public class DigitalNumber {
    public static void main(String args[]){
        char[][] panel = new char[3][120]; //A 20 digit panel!
        int digXIndex = 0;int digYIndex = 0;
        for (int i=0;i<args[0].length(); i++){
            int dig=Integer.parseInt(""+args[0].charAt(i));
            panel[digXIndex][digYIndex]=32;   
            digYIndex++;
            if (dig!=1 && dig!=4)
                panel[digXIndex][digYIndex]='_';  
            else
                panel[digXIndex][digYIndex]=32;
            digYIndex++;
                panel[digXIndex][digYIndex]=32; 
            digYIndex=3*i;
            digXIndex++;
            if (dig!=1 && dig!=2 && dig!=3 && dig!=7)
                panel[digXIndex][digYIndex]='|';    
            else
                panel[digXIndex][digYIndex]=32;
            digYIndex++;
            if (dig!=1 && dig!=0 && dig!=7)
                panel[digXIndex][digYIndex]='_';    
            else
                panel[digXIndex][digYIndex]=32;
            digYIndex++;
            if (dig!=6 && dig!=5)
                panel[digXIndex][digYIndex]='|';  
            else
                panel[digXIndex][digYIndex]=32;
            digYIndex=3*i;
            digXIndex++;
            if (dig!=6 && dig!=8 && dig!=2 && dig!=0)
                panel[digXIndex][digYIndex]=32;  
            else
                panel[digXIndex][digYIndex]='|';
            digYIndex++;
            if (dig!=7 && dig!=4 && dig!=1)
                panel[digXIndex][digYIndex]='_';  
            else
                panel[digXIndex][digYIndex]=32;
            digYIndex++;
            if (dig!=2)
                panel[digXIndex][digYIndex]='|';  
            else
                panel[digXIndex][digYIndex]=32;
            digXIndex=0;
            digYIndex+=(i*3)+1;
        }       
        for (int i=0; i<3; i++){
            for (int j=0; j<120; j++)
                if (panel[i][j]!=0)
                System.out.print((char)(panel[i][j]));
                else
                    System.out.print("");
            System.out.println();
        }   
    }
}

样品I / O

java DigitalNumber 98765432109876543210
     _  _  _  _  _     _  _     _  _  _  _  _  _     _  _     _ 
    |_||_|  ||_ |_ |_| _| _|  || ||_||_|  ||_ |_ |_| _| _|  || |
     _||_|  ||_| _|  | _||_   ||_| _||_|  ||_| _|  | _||_   ||_|

5
我应该开始讨厌Java :)
Aman ZeeK Verma 2011年

1
Java在很多方面都很棒,但是简洁的代码不是其中之一。
乔纳森·M·戴维斯

2
嗯,这个代码甚至都没有。它可能会更短。
乔伊,

我打了一些你的密码;)
Knerd 2014年

1

D:295个字符

import std.stdio;void main(string[]a){string[3]o;foreach(c;a[1]){int n=cast(int)(c)-48;auto e=" ";o[0]~=n!=1&&n!=4?" _ ":"   ";o[1]~=!n||n>3&&n!=7?"|":e;o[1]~=n>1&&n!=7?"_":e;o[1]~=n<5||n>6?"|":e;o[2]~=!(n&1)&&n!=4?"|":e;o[2]~=!n||n>1&&n!=4&&n!=7?"_":e;o[2]~=n!=2?"|":e;}foreach(l;o)writeln(l);}

更清晰:

import std.stdio;

void main(string[] a)
{
    string[3] o;

    foreach(c; a[1])
    {
        int n = cast(int)(c) - 48;
        auto e = " ";

        o[0] ~= n != 1 && n != 4 ? " _ " : "   ";

        o[1] ~= !n || n > 3 && n != 7 ? "|" : e;
        o[1] ~= n > 1 && n != 7 ? "_" : e;
        o[1] ~= n < 5 || n > 6 ? "|" : e;

        o[2] ~= !(n&1) && n != 4 ? "|" : e;
        o[2] ~= !n || n > 1 && n != 4 && n != 7 ? "_" : e;
        o[2] ~= n != 2 ? "|" : e;
    }

    foreach(l; o)
        writeln(l);
}

1

奥卡姆268

let t=function|'1'|'4'->"   "|_->" _ "let m=function|'0'->"| |"|'1'|'7'->"  |"|'2'|'3'->" _|"|_->"|_|"|'5'|'6'->"|_ "let b=function|'0'|'8'->"|_|"|'1'|'4'|'7'->"  |"|'2'->"|_ "|_->" _|"let f s=let g h=String.iter(fun c->print_string(h c))s;print_newline()ing t;g m;g b

可读版本

let t = function
  | '1'
  | '4' -> "   "
  | _ -> " _ "
let m = function
  | '0' -> "| |"
  | '1'
  | '7' -> "  |"
  | '2'
  | '3' -> " _|"
  | _ -> "|_|"
  | '5'
  | '6' -> "|_ "
let b = function
  | '0'
  | '8' -> "|_|"
  | '1'
  | '4'
  | '7' -> "  |"
  | '2' -> "|_ "
  | _ -> " _|"
let f s =
  let g h =
    String.iter (fun c -> print_string (h c)) s;
    print_newline () in
  g t;
  g m;
  g b

1

Perl(182 180)

#!perl -l
$_=<<7;
 _     _  _     _  _  _  _  _ 
| |  | _| _||_||_ |_   ||_||_|
|_|  ||_  _|  | _||_|  ||_| _|
7
@b=map{[/(...)/g]}split/\n/;@d=split//,<>;for$p(@b){print map$p->[$_],@d}

从STDIN读取。

$ perl 7segment.pl 
1234567890987654321
    _  _     _  _  _  _  _  _  _  _  _  _  _     _  _     _ 
  | _| _||_||_ |_   ||_||_|| ||_||_|  ||_ |_ |_| _| _|  || |
  ||_  _|  | _||_|  ||_| _||_| _||_|  ||_| _|  | _||_   ||_|

1

鬼脚本 (270) (248) (214)

编辑:更多替换。删除数字之间的空格。

编辑:更多的替代。现在,主循环看起来像它在做什么!

/F{forall}def[48<~HUp;::1ncBInp~>{1 index 1 add}F
pop/*{dup
2 idiv exch
2 mod
1 eq}/P{print}/#{( )P}/?{ifelse
P}/O{{( )}?}/|{*{(|)}O}/_{*{(_)}O}>>begin[[[[ARGUMENTS{{load
# _ #}F()=]2{{| _ |}F()=]}repeat]pop[[[[}F

使用ghostscript的参数处理功能:通过调用gs -dNODISPLAY -- digit.ps 012 345 6789


1

德尔福|| 453(带格式的568)

距离胜利还差得远,但是做^。^很有趣

const asc: array[0..9] of array[0..2] of string = ((' _ ','| |','|_|'),('   ','  |','  |'),(' _ ',' _|','|_ '),(' _ ',' _|',' _|'),('   ','|_|','  |'),(' _ ','|_ ',' _|'),(' _ ','|_ ','|_|'),(' _ ','  |','  |'),(' _ ','|_|','|_|'),(' _ ','|_|',' _|'));var s,l:string;x,i:integer;begin Readln(s);s:=StringReplace(s,' ','',[rfReplaceAll]);for I := 0 to 2 do begin l:='';for x := 1 to length(s) do l := l + asc[StrToInt(s[x])][i];writeln(l);end;readln;end.  

带格式

const
asc: array[0..9] of array[0..2] of string = (
  (' _ ','| |','|_|'),
  ('   ','  |','  |'),
  (' _ ',' _|','|_ '),
  (' _ ',' _|',' _|'),
  ('   ','|_|','  |'),
  (' _ ','|_ ',' _|'),
  (' _ ','|_ ','|_|'),
  (' _ ','  |','  |'),
  (' _ ','|_|','|_|'),
  (' _ ','|_|',' _|'));
var
s,l:string;
x,i:integer;
begin
    Readln(s);
    s:=StringReplace(s,' ','',[rfReplaceAll]);
    for I := 0 to 2 do
    begin
      l:='';
      for x := 1 to length(s) do
        l := l + asc[StrToInt(s[x])][i];
      writeln(l);
    end;
    readln

结束。


1

PHP,140个 136 133 131 129 128字节

使用扩展的ascii,我可以再节省5 7:一个用于换"| _"行,三个用于~"z/]{4lno~|"(按位取反会将所有内容转换为扩展的ascii字符=没有特殊字符,并且PHP不需要引号),两个用于-1(它是仅在此处将地图保留为标准ascii)。但是出于可读性和兼容性,我仍然使用标准ascii。

for(;""<$c=$argv[1][$i++];)for($n=753754680;$n>>=3;)$r[$p++%3].="| _"[ord(~"z/]{4lno~|"[$c])-1>>$n%8&1?:$n&2];echo join("
",$r);

位图

  • 采取LEDS _|_||_|称为比特-6-024135(比特数和2为0,垂直LED)的
  • 为数字0..9创建位图: [123,48,94,124,53,109,111,112,127,125]
  • 减少1,使它们全部为可打印的ASCII码-> "z/]{4lno~|"
  • 取反-> ~"z/]{4lno~|"(允许在字符选择中使用三元速记)

模板

  • 使用7的空间- > 767024135
  • 由列而不是行重新组合- > 701623745(呈现$p=0作废)
  • 反向-> 547326107(从右到左读取地图;允许算术循环)
  • 追加零-> 5473261070(允许将移位与循环头中的测试结合使用)
  • 读取八进制,转换为十进制-> 753754680(短两个字节:一位数字和前缀)

分解

for(;""<$c=$argv[1][$i++];) // loop through input characters
    for($n=753754680;$n>>=3;)   // loop through template
        $r[$p++%3].="| _"[          // append character to row $p%3:
            ord(~"z/]{4lno~|"[$c])-1// decode bitmap
                >>$n%8&1            // test bit $n%8 (always 1 for bit 7)
            ?                       // if set: 1 (space)
            :$n&2                   // else: 2 (underscore) for bits 2,3,6; 0 (pipe) else
        ];
echo join("\n",$r);         // print result

+16个字节(十六进制):ord(~"z/]{4lno~|v.J=NF"[hexdec($c)])-1
Titus

1

Java 8,280字节

interface M{static void main(String[]a){String x="",y=x,z=x;for(int c:a[0].getBytes()){c-=48;x+=" "+(c==4|c==1?" ":"_")+" ";y+=(c==7|c>0&c<4?" ":"|")+(c==7|c<2?" ":"_")+(c>4&c<7?" ":"|");z+=(c%2<1&c!=4?"|":" ")+(c%3==1?" ":"_")+(c==2?" ":"|");}System.out.print(x+"\n"+y+"\n"+z);}}

说明:

在这里尝试。

interface M{                     // Class
  static void main(String[]a){   //  Mandatory main-method
    String x="",                 //   String for row 1, starting empty
           y=x,                  //   String for row 2, starting empty
           z=x;                  //   String for row 3, starting empty
    for(int c:a[0].getBytes()){  //   Loop over the bytes of the input
      c-=48;                     //    Convert the byte to integer
      x+=                        //    Append to row 1:
         " "                     //     a space
         +(c==4|c==1?            //     +If the digit is a 1 or 4:
            " "                  //       Append a space
           :                     //      Else:
            "_")                 //       Append an underscore
         +" ";                   //     + another space
      y+=                        //    Append to row 2:
         (c==7|c>0&c<4?          //      If the digit is 1, 2, 3, or 7:
           " "                   //       Append a space
          :                      //      Else:
           "|")                  //       Append a pipe
         +(c==7|c<2?             //     +If the digit is 0, 1, or 7:
            " "                  //       Append a space
           :                     //      Else:
            "_")                 //       Append an underscore
         +(c>4&c<7?              //     +If the digit is 5 or 6:
            " "                  //       Append a space
           :                     //      Else:
            "|");                //       Append a pipe
      z+=                        //    Append to row 3:
         (c%2<1&c!=4?            //      If the digit is 0, 2, 6 or 8:
           "|"                   //       Append a pipe
          :                      //      Else:
           " ")                  //       Append a space
          +(c%3==1?              //     +If the digit is 1, 4, or 7:
             " "                 //       Append a space
            :                    //      Else:
             "_")                //       Append a pipe
          +(c==2?                //     +If the digit is 2:
             " "                 //       Append a space
            :                    //      Else:
             "|");               //       Append a pipe
    }                            //   End of loop
    System.out.print(x+"\n"+y+"\n"+z);
                                 //   Print the three rows
  }                              //  End of main-method
}                                // End of class

作为功​​能,它将是218个字节


0

Python,227个字符

a="   ";b=" _ ";c="|_|";d="| |";e="|  ";f="  |";g="|_ ";h=" _|"
z=[[b,d,c],[a,f,f],[b,h,g],[b,h,h],[a,c,f],[b,g,h],[b,g,c],[b,f,f],[b,c,c],[b,c,h]]
x=map(int,raw_input())
for i in range(3):
 for j in x:
  print z[j][i],
 print

简单明了。


0

Perl,145个字符

$i=<>;for$s(6,3,0){for($i=~/./g){$v=(175,9,158,155,57,179,183,137,191,187)[$_]>>$s;$o.=($v&4?'|':$").($v&2?'_':$").($v&1?'|':$")}$o.="
"}print$o

取消高尔夫:

# Read STDIN
$i = <>;
# Amount to bit shift later
for $s (6,3,0)
{
  # For each character C from STDIN
  for ($i =~ /./g)
  {
    # Get the Cth array index, bit shifted right by $s
    $v = (175, 9, 158, 155, 57, 179, 183, 137, 191, 187)[$_]>>$s;
    # Concatenate each character if the bitwise masked value says it should be there
    $o .= ($v & 4 ? '|' : $") .
          ($v & 2 ? '_' : $") .
          ($v & 1 ? '|' : $");
  }
  # Concatenate a newline
  $o .= "
";
}
# Print the result
print $o;

0

Python 3.4.3- 1514 858字节

解决这个问题的诱惑太大了,所以不要注册并回答= P

我对Python有点陌生,所以我将程序格式化的很好而整洁(或至少我做到了)。改进非常感谢!

import sys;l1,l2,l3,l4,l5="";num=input()
for c in num:
    if c=="1":
        l1+=" oo  ";l2+="  o  ";l3+="  o  ";l4+="  o  ";l5+="oooo "
    if c=="2":
        l1+="oooo ";l2+="   o ";l3+="oooo ";l4+="o    ";l5+="oooo "
    if c=="3":
        l1+="oooo ";l2+="   o ";l3+=" ooo ";l4+="   o ";l5+="oooo "
    if c=="4":
        l1+="o  o ";l2+="o  o ";l3+="oooo ";l4+="   o ";l5+="   o "
    if c=="5":
        l1+="oooo ";l2+="o    ";l3+="oooo ";l4+="   o ";l5+="oooo "
    if c=="6":
        l1+="oooo ";l2+="o    ";l3+="oooo ";l4+="o  o ";l5+="oooo "
    if c=="7":
        l1+="oooo ";l2+="   o ";l3+="   o ";l4+="   o ";l5+="   o "
    if c=="8":
        l1+="oooo ";l2+="o  o ";l3+="oooo ";l4+="o  o ";l5+="oooo "
    if c=="9":
        l1+="oooo ";l2+="o  o ";l3+="oooo ";l4+="   o ";l5+="oooo "
    if c=="0":
        l1+="oooo ";l2+="o  o ";l3+="o  o ";l4+="o  o ";l5+="oooo "
print(l1+"\n"+l2+"\n"+l3+"\n"+l4+"\n"+l5)

编辑:缩短的变量名称,使用; 为了缩短,输出编号的末尾仅一个空格,一次定义了所有变量。

新行是2个字节(CRLF),我使用制表符而不是4个空格。


2
欢迎来到编程难题和代码高尔夫球!由于这是代码挑战赛,因此您需要使代码尽可能短。这些技巧可能对打高尔夫球很有帮助。
Alex A.

“我用制表符代替了4个空格”,我感到困惑。您为什么仍然不使用标签?;)
莫妮卡(Monica)

0

SmileBASIC,216字节

?INPUT N$DIM A[14]COPY A,@A@A
DATA 1,0,0,1,3,1,1,3,0,4,3,4,1,6FOR D=0 TO LEN(N$)-1X=ASC("w$]m.k{%\o"[VAL(N$[D])])FOR I=0TO 6T=I*2IF X<<31THEN GBOX A[T]+D*5,A[T+1],A[T]+D*5+!(I MOD 3),A[T+1]+!!(I MOD 3)
X=X/2NEXT
NEXT

使用图形代替文本,因为它可能更短。

说明:

PRINT 'so the input and output don't overlap
INPUT NUMBER$ 'get number
DIM PTS[7*2] 'locations of segments
COPY PTS,@PTDATA 'copy data into array
@PTDATA
DATA 1,0,0,1,3,1,1,3,0,4,3,4,1,6 'stored as x,y,x,y,...
FOR DIGIT=0 TO LEN(NUMBER$)-1
 NUM=ASC("w$]m.k{%\o"[VAL(NUMBER$[DIGIT])]) 'get digit data. That data string doesn't have any non-ASCII characters, except \ which is 127 in SB.
 FOR I=0 TO 7-1 'draw each segment
  T=I*2 'position of point in array
  IF X AND 1 THEN GLINE PTS[T]+DIGIT*5,PTS[T+1],PTS[T]+DIGIT*5+!(I MOD 3),PTS[T+1]+!!(I MOD 3) 'draw segment. I MOD 3 determines whether it's horizontal or vertical.
  X=X>>1 'shift to next bit
 NEXT
NEXT

0

C ++,230 229 225 223 218个 207 204 198字节

#import<iostream>
#define d for(auto
std::string v[3],t,g="|_| =2$0^262\'032;2$2?272";main(){std::cin>>t;d i:t)d j:{0,1,2})d k:{0,1,2})v[k]+=g[g[i*2-92+!k]>>j+k/2*3&1?j:3];d j:v)std::cout<<j<<'\n';}

从stdin读取并输出到stdout。

说明:

#import<iostream>                // string inside

std::string v[3], t, g="|_| "    // symbol on different horizontal position
      "=2$0^262\'032;2$2?272";   // space(0) or not(1) for each number and position
                                 // binary representation, last 6 bits is used
                                 // even positions are for row 1, 2; odds are for row 0

main() {
    std::cin>>t;                 // input
    for (auto i:t)               // for each character
        for (auto j:{0,1,2})     // for each horizontal position
            for (auto k:{0,1,2}) // for each vertical position
                v[k]+=g[         // use first four chars only
                    g[i*2-92     // i*2-96 is number*2, +4 to skip first four
                        +!k]     // row 0 uses another character
                    >>j+k/2*3    // (k==2?3:0)+j, the expected bit
                    &1           // extract the bit
                    ?j:3         // space or not space
                ];

    for (auto j:v) std::cout<<j<<'\n'; // output
}

0

Powershell,114个字节

param($a)6,3,0|%{$l=$_
-join($a|% t*y|%{('   0 _ 0 _|0|_ 0| |0  |0|_|'-split0)[(+('f-SR5Z^mvr'["$_"])-shr$l)%8]})}

测试脚本:

$f = {

param($a)6,3,0|%{$l=$_
-join($a|% t*y|%{('   0 _ 0 _|0|_ 0| |0  |0|_|'-split0)[(+('f-SR5Z^mvr'["$_"])-shr$l)%8]})}

}

&$f "1234567890"
&$f "81"

输出:

    _  _     _  _  _  _  _  _
  | _| _||_||_ |_   ||_||_|| |
  ||_  _|  | _||_|  ||_| _||_|
 _
|_|  |
|_|  |

大意:

每个标准数字时钟样式号包含3行。此外,第一行仅包含2个选项。共有6个选项。因此,7位足以编码每个数字。

line str=@('   ', ' _ ', ' _|', '|_ ', '| |', '  |', '|_|')

#    line str    binary       dec    ASCII
-    --------    ---------    ---    -----
0 -> 1 
     4 
     6        -> 1 100 110 -> 102 -> 'f'

1 -> 0
     5
     5        -> 0 101 101 ->  45 -> '-'

...

8 -> 1
     6
     6        -> 1 110 110 -> 118 -> 'v'

9 -> 1
     6
     2        -> 1 110 010 -> 114 -> 'r'

因此,该字符串f-SR5Z^mvr对所有标准数字时钟样式编号的所有段进行编码。

注意:的顺序line str是特别选择的,因此所有代码都在间隔中32..126

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.