Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Write the shortest program which will take a string of numbers (of up to at least 20 length) as input, and display the output using the standard digital clock style numbers. For instance for input 81, a solution with ascii output would give:

 _
|_|  |
|_|  |

Graphical output is also acceptable, if it helps.

share|improve this question
    
any upper limit on "take a string of numbers as input" ? –  Aman ZeeK Verma Feb 17 '11 at 19:29
    
Hmm, let's say one row on an 80 column screen (20) is good enough. –  mootinator Feb 17 '11 at 20:06
1  
1  
Also related: stackoverflow.com/q/3324301/code-golf-digital-clock –  dmckee Feb 17 '11 at 22:15

24 Answers 24

up vote 3 down vote accepted

APL (Dyalog) (45)

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

The string, ા8धगɯે૙ࣃଏ૽, are the unicode characters 2750 56 2343 2327 623 2759 2777 2243 2831 2813 (however, you should be able to just copy and paste it). They encode the numbers. The program reads a line from the keyboard.

Explanation:

  • 1+⍎¨⍞: read a line from the keyboard, parse each character as a digit, then add 1 to each number (APL arrays are 1-based by default).
  • ⎕UCS'ા8धगɯે૙ࣃଏ૽'[...]: Select the characters belonging to the digits of the numbers you entered, and look up the Unicode values.
  • {...: for each of these values, do:
  • 1+⍵⊤⍨9⍴3: get the first 9 base-3 digits from the value as expressed in base-3, and add 1 (because the arrays ar 1-based).
  • ' _|'[...]: select a space, horizontal line, or vertical line depending on these digits
  • 3 3⍴: format as a 3-by-3 box.
share|improve this answer

Funciton

Not really a language suitable for golfing... but regardless, I tried to keep the code size as small as I could — quite a different challenge than in “usual” languages. This is 1555 characters or 3110 bytes (if encoded as UTF-16; UTF-8 is larger).

Here’s a screenshot of the program running. It really works :)

Since this looks ugly in StackExchange due to the extra line spacing, consider running the following code in your browser’s JavaScript console to fix that: $('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╟───────────┘
     ╚═══╝             ╚════╝╚═══╝

It could probably be smaller if I hadn’t made a mistake due to which the output was back to front; I fixed that by adding an extra function to reverse the input. Otherwise I would probably have to rewrite all of it.

I also made another mistake (swapping the operands in two calls to ) which made it necessary to declare the extra function, but this one is so small it fits inside the main function and thus doesn’t add any characters!

share|improve this answer
6  
Whoa, that looks cool. Not only the solution, the language too :-) –  Joey May 7 '11 at 9:58
    
@Joey: Why, thanks! :) –  Timwi May 7 '11 at 14:56
    
That is just awesome –  Knerd Dec 10 at 10:31

wxpython, many characters

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()

Test

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

enter image description here

ps: not a serious entry, but looks like graphical output is also acceptable, so I just gave it a try :-)

share|improve this answer
    
+1 Glad to see this :). –  mootinator Feb 18 '11 at 4:27

Golfscript - 66 chars

"placeholder text for userscript which counts chars             ";

xxd: (use xxd -r to revert)

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                                     /;

This follows most of the other answers in that there are no spaces between numbers and trailing spaces are kept in. A space between numbers can easily be added with 1+ before {"_ |"=}%. Packed into a base 3 number, and then as base 243 into a string.

share|improve this answer
    
Fair enough. I updated the question to not bother with the unnecessary space between numbers. –  mootinator Feb 18 '11 at 5:12
4  
I think the "placeholder..." thing is a bit confusing. –  Eelvex Feb 18 '11 at 13:35

J, 90, 78 68 chars

[ update: using unicode (1 byte) encoding:

,./(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

works as before: ]

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

The key is in the encoding of digits as base-3 integers. Zero, for example is:

:
 _ 
| |
|_|

or ' _ | ||_|', which becomes 0102022123 = 2750.

share|improve this answer
    
I could save 10 characters if J had high precision base-36 numbers. Any suggestions anyone? –  Eelvex Feb 17 '11 at 23:09
    
I'm using binary now. I wonder if trinary would save me some strokes? –  luser droog Dec 3 '12 at 4:59
    
congratulations for having the 1000th post on codegolf.SE! (http://codegolf.stackexchange.com/q/1000) –  Doorknob 冰 Sep 23 '13 at 21:54
    
2+ years late but thanks :) –  Eelvex Sep 24 '13 at 1:13

Mathematica 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})]]

Usage

z@1234567890

digits

share|improve this answer

JavaScript (145)

148 145

Since JavaScript doesn’t really have standard input/output, this is written as a function that takes a string and returns the output as a string.

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}

Spaced out:

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
}

Here’s how it works:

  • Every digit shape is encoded in a Unicode character consisting of 9 bits.
  • The first three bits are for the first row, etc.

  • In each group of three bits, the first specifies whether the first character is | or space, the second whether it’s _ or space, and the third again | or space.

  • These three bits are retrieved as c&1, c&2 and c&4, which are then used as indexes into the string b.

  • At each iteration, i%L is the “x-coordinate”, i.e. the digit within the input n

  • At each iteration, i/L is the “y-coordinate”, i.e. the row, but we need |0 to make it an integer

  • Finally, the spaces between the digits and the newlines between the lines are also retrieved by indexing into b, re-using the space character and the otherwise unused position 3 in that string! :)

share|improve this answer
    
Unicode! BAH! ... well, it is shorter than mine. +1 –  luser droog Dec 3 '12 at 4:54

Ruby, 142

' _     _  _     _  _  _  _  _ 
| |  | _| _||_||_ |_   ||_||_|
|_|  ||_  _|  | _||_|  ||_| _|'.lines{|l|puts x.chars.map{|i|l[i.to_i*3,3]}*''}

expects input in the variable x. examples:

x = '321'
#  _  _    
#  _| _|  |
#  _||_   |

x = '42'
#      _ 
#  |_| _|
#    ||_ 
share|improve this answer
    
ooooo. pretty! ... –  luser droog Dec 3 '12 at 4:49

Golfscript - 97 chars

:*{32' _':$@'14'{?~!=}:&~32}%n*{:x' |':|\'1237'&$x'017'&|x'56'&}%n*{:x|\'134579'&$x'147'&|x'2'&}%
share|improve this answer
    
Use the backtick to save a char for all the numeric strings (how do I insert this character in the code format?) –  Nabb Feb 18 '11 at 5:00
    
@Nabb: You can’t — StackExchange is crap like that... –  Timwi May 6 '11 at 13:29

Windows PowerShell, 127

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

Since the strings contain some unpleasant-to-write characters, a hexdump for your convenience:

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]]})}
share|improve this answer
3  
Look at all the smileys.. –  Wang Dingwei Feb 18 '11 at 4:58

C# 369 Characters

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())));}

I could easily cut a few characters out. The point was more to abuse LINQ :)

More whitespace version:

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())));
}
share|improve this answer
    
First of all, you need a class declaration and usings (409). Then there is a some unnecessary whitespace that can be removed (402). The task states that whitespace between the digits shall be omitted (399). –  Joey Apr 16 '11 at 12:26
    
Uh, thanks. I edited the task because nobody was following the original whitespace rule. Feel free to edit the answer if it offends you, because I just don't care. –  mootinator Apr 16 '11 at 17:42
    
g is used only once, so you can save 7 characters by inlining it. –  Timwi May 6 '11 at 1:10
1  
Actually b is also used only once, so you can inline it too. Also, you can save a lot of characters if you change the integer array to "{H=mNgwI\x7fo", or even shorter if you change \x7f to the actual character #127 (which is unprintable, but permissible). The >> will still work because there is an implicit conversion from char to int. –  Timwi May 6 '11 at 1:14
    
Also, the .ToCharArray() and the .ToArray() are both redundant, you can just remove them :) –  Timwi May 6 '11 at 1:16

Java Solution: 585 570 Chars

I don't think I'll be attempting any more golfing in 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();}}}
share|improve this answer
    
Your java version is way better/shorter than mine :) btw!... does 6 in digital clock representation has a cap (_) at top or not? I got confused after you output! –  Aman ZeeK Verma Feb 18 '11 at 22:02
    
Upon checking the coffee maker behind me, the 6's should have a cap. I'll update my solution. –  Mitch Feb 18 '11 at 22:30

Python, 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)

With line breaks:

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)
share|improve this answer

Bash, 11 characters

toilet "$i"

Yes I know, I'm cheating.

You need to have toilet installed.

share|improve this answer
1  
And have the default font set to one with 7 segment display numbers. figlet would also work. –  Rob Nov 14 '12 at 18:36

Java, 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();
        }   
    }
}

SAMPLE I/O

java DigitalNumber 98765432109876543210
     _  _  _  _  _     _  _     _  _  _  _  _  _     _  _     _ 
    |_||_|  ||_ |_ |_| _| _|  || ||_||_|  ||_ |_ |_| _| _|  || |
     _||_|  ||_| _|  | _||_   ||_| _||_|  ||_| _|  | _||_   ||_|
share|improve this answer
4  
I should start hating java :) –  Aman ZeeK Verma Feb 17 '11 at 21:06
1  
Java is great for many things, but terse code is not one of them. –  Jonathan M Davis Feb 18 '11 at 11:32
    
Well, this code isn't even golfed. It could be way shorter. –  Joey Apr 16 '11 at 9:20
    
I golfed your code a bit ;) –  Knerd Dec 10 at 10:46

gForth, 186 175 chars

New version:

: 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

This actually bothers to exit (+3 chars) as well :). Here is the more readable version, it does some bit-packing to reduce the LUT size by 1/3, but the resulting code is more complex so it's not much of a savings:

: 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

Old version:

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

This leaves the stack unbalanced and doesn't bother to exit the interpreter. Here is a cleaner more readable version

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

D: 295 Characters

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);}

More Legibly:

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);
}
share|improve this answer

Ocaml, 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

Readable version

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
share|improve this answer

Perl (182 180)

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

reads from STDIN.

$ perl 7segment.pl 
1234567890987654321
    _  _     _  _  _  _  _  _  _  _  _  _  _     _  _     _ 
  | _| _||_||_ |_   ||_||_|| ||_||_|  ||_ |_ |_| _| _|  || |
  ||_  _|  | _||_|  ||_| _||_| _||_|  ||_| _|  | _||_   ||_|
share|improve this answer

Ghostscript (270) (248) (214)

Edit: More substitutions. Removed space between digits.

Edit: Even more substitutions. Main loop now looks like what it does!

/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

Uses ghostscript's argument-processing feature: invoke with gs -dNODISPLAY -- digit.ps 012 345 6789.

share|improve this answer

Delphi || 453 (568 With format)

Not even close enough to win but it was fun to do ^.^

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.  

With format

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.

share|improve this answer

C - 12,359 chars (just for fun)

#include <stdio.h>
#include <string.h>

int main(int argc, char **argv){
  char *str = argv[1];
  int i,
      j,
      h = 5,
      ch = 5,
      w = strlen(str) * ch;
  char display[h * w];
  for(i = 0; i < h * w; i ++){
   display[i] = 0;
  }
  void putd(c,x,y){
    display[x + y * w] = c;
  }
  char getd(x,y){
    return display[x + y * w];
  }
  for(i = 0; i < h; i++){
    for(j = 0; j < strlen(str); j++){
      switch((int)str[j]){
        case '0':
          switch(i){
            case 0:
              putd(' ',(j*ch)+0,0);
              putd('-',(j*ch)+1,0);
              putd('-',(j*ch)+2,0);
              putd('-',(j*ch)+3,0);
              putd(' ',(j*ch)+4,0);
              break;
            case 1:
              putd('|',(j*ch)+0,1);
              putd(' ',(j*ch)+1,1);
              putd(' ',(j*ch)+2,1);
              putd(' ',(j*ch)+3,1);
              putd('|',(j*ch)+4,1);
              break;
            case 2:
              putd('|',(j*ch)+0,2);
              putd(' ',(j*ch)+1,2);
              putd(' ',(j*ch)+2,2);
              putd(' ',(j*ch)+3,2);
              putd('|',(j*ch)+4,2);
              break;
            case 3:
              putd('|',(j*ch)+0,3);
              putd(' ',(j*ch)+1,3);
              putd(' ',(j*ch)+2,3);
              putd(' ',(j*ch)+3,3);
              putd('|',(j*ch)+4,3);
              break;
            case 4:
              putd(' ',(j*ch)+0,4);
              putd('-',(j*ch)+1,4);
              putd('-',(j*ch)+2,4);
              putd('-',(j*ch)+3,4);
              putd(' ',(j*ch)+4,4);
              break;
          }
          break;
        case '1':
          switch(i){
            case 0:
              putd(' ',(j*ch)+0,0);
              putd(' ',(j*ch)+1,0);
              putd(' ',(j*ch)+2,0);
              putd(' ',(j*ch)+3,0);
              putd(' ',(j*ch)+4,0);
              break;
            case 1:
              putd(' ',(j*ch)+0,1);
              putd(' ',(j*ch)+1,1);
              putd(' ',(j*ch)+2,1);
              putd('|',(j*ch)+3,1);
              putd(' ',(j*ch)+4,1);
              break;
            case 2:
              putd(' ',(j*ch)+0,2);
              putd(' ',(j*ch)+1,2);
              putd(' ',(j*ch)+2,2);
              putd('|',(j*ch)+3,2);
              putd(' ',(j*ch)+4,2);
              break;
            case 3:
              putd(' ',(j*ch)+0,3);
              putd(' ',(j*ch)+1,3);
              putd(' ',(j*ch)+2,3);
              putd('|',(j*ch)+3,3);
              putd(' ',(j*ch)+4,3);
              break;
            case 4:
              putd(' ',(j*ch)+0,4);
              putd(' ',(j*ch)+1,4);
              putd(' ',(j*ch)+2,4);
              putd(' ',(j*ch)+3,4);
              putd(' ',(j*ch)+4,4);
              break;
          }
          break;
        case '2':
          switch(i){
            case 0:
              putd(' ',(j*ch)+0,0);
              putd('-',(j*ch)+1,0);
              putd('-',(j*ch)+2,0);
              putd('-',(j*ch)+3,0);
              putd(' ',(j*ch)+4,0);
              break;
            case 1:
              putd(' ',(j*ch)+0,1);
              putd(' ',(j*ch)+1,1);
              putd(' ',(j*ch)+2,1);
              putd(' ',(j*ch)+3,1);
              putd('|',(j*ch)+4,1);
              break;
            case 2:
              putd(' ',(j*ch)+0,2);
              putd('-',(j*ch)+1,2);
              putd('-',(j*ch)+2,2);
              putd('-',(j*ch)+3,2);
              putd(' ',(j*ch)+4,2);
              break;
            case 3:
              putd('|',(j*ch)+0,3);
              putd(' ',(j*ch)+1,3);
              putd(' ',(j*ch)+2,3);
              putd(' ',(j*ch)+3,3);
              putd(' ',(j*ch)+4,3);
              break;
            case 4:
              putd(' ',(j*ch)+0,4);
              putd('-',(j*ch)+1,4);
              putd('-',(j*ch)+2,4);
              putd('-',(j*ch)+3,4);
              putd(' ',(j*ch)+4,4);
              break;
          }
          break;
        case '3':
          switch(i){
            case 0:
              putd(' ',(j*ch)+0,0);
              putd('-',(j*ch)+1,0);
              putd('-',(j*ch)+2,0);
              putd('-',(j*ch)+3,0);
              putd(' ',(j*ch)+4,0);
              break;
            case 1:
              putd(' ',(j*ch)+0,1);
              putd(' ',(j*ch)+1,1);
              putd(' ',(j*ch)+2,1);
              putd(' ',(j*ch)+3,1);
              putd('|',(j*ch)+4,1);
              break;
            case 2:
              putd(' ',(j*ch)+0,2);
              putd('-',(j*ch)+1,2);
              putd('-',(j*ch)+2,2);
              putd('-',(j*ch)+3,2);
              putd(' ',(j*ch)+4,2);
              break;
            case 3:
              putd(' ',(j*ch)+0,3);
              putd(' ',(j*ch)+1,3);
              putd(' ',(j*ch)+2,3);
              putd(' ',(j*ch)+3,3);
              putd('|',(j*ch)+4,3);
              break;
            case 4:
              putd(' ',(j*ch)+0,4);
              putd('-',(j*ch)+1,4);
              putd('-',(j*ch)+2,4);
              putd('-',(j*ch)+3,4);
              putd(' ',(j*ch)+4,4);
              break;
          }
          break;
        case '4':
          switch(i){
            case 0:
              putd(' ',(j*ch)+0,0);
              putd(' ',(j*ch)+1,0);
              putd(' ',(j*ch)+2,0);
              putd(' ',(j*ch)+3,0);
              putd(' ',(j*ch)+4,0);
              break;
            case 1:
              putd('|',(j*ch)+0,1);
              putd(' ',(j*ch)+1,1);
              putd(' ',(j*ch)+2,1);
              putd(' ',(j*ch)+3,1);
              putd('|',(j*ch)+4,1);
              break;
            case 2:
              putd(' ',(j*ch)+0,2);
              putd('-',(j*ch)+1,2);
              putd('-',(j*ch)+2,2);
              putd('-',(j*ch)+3,2);
              putd(' ',(j*ch)+4,2);
              break;
            case 3:
              putd(' ',(j*ch)+0,3);
              putd(' ',(j*ch)+1,3);
              putd(' ',(j*ch)+2,3);
              putd(' ',(j*ch)+3,3);
              putd('|',(j*ch)+4,3);
              break;
            case 4:
              putd(' ',(j*ch)+0,4);
              putd(' ',(j*ch)+1,4);
              putd(' ',(j*ch)+2,4);
              putd(' ',(j*ch)+3,4);
              putd(' ',(j*ch)+4,4);
              break;
          }
          break;
        case '5':
          switch(i){
            case 0:
              putd(' ',(j*ch)+0,0);
              putd('-',(j*ch)+1,0);
              putd('-',(j*ch)+2,0);
              putd('-',(j*ch)+3,0);
              putd(' ',(j*ch)+4,0);
              break;
            case 1:
              putd('|',(j*ch)+0,1);
              putd(' ',(j*ch)+1,1);
              putd(' ',(j*ch)+2,1);
              putd(' ',(j*ch)+3,1);
              putd(' ',(j*ch)+4,1);
              break;
            case 2:
              putd(' ',(j*ch)+0,2);
              putd('-',(j*ch)+1,2);
              putd('-',(j*ch)+2,2);
              putd('-',(j*ch)+3,2);
              putd(' ',(j*ch)+4,2);
              break;
            case 3:
              putd(' ',(j*ch)+0,3);
              putd(' ',(j*ch)+1,3);
              putd(' ',(j*ch)+2,3);
              putd(' ',(j*ch)+3,3);
              putd('|',(j*ch)+4,3);
              break;
            case 4:
              putd(' ',(j*ch)+0,4);
              putd('-',(j*ch)+1,4);
              putd('-',(j*ch)+2,4);
              putd('-',(j*ch)+3,4);
              putd(' ',(j*ch)+4,4);
              break;
          }
          break;
        case '6':
          switch(i){
            case 0:
              putd(' ',(j*ch)+0,0);
              putd('-',(j*ch)+1,0);
              putd('-',(j*ch)+2,0);
              putd('-',(j*ch)+3,0);
              putd(' ',(j*ch)+4,0);
              break;
            case 1:
              putd('|',(j*ch)+0,1);
              putd(' ',(j*ch)+1,1);
              putd(' ',(j*ch)+2,1);
              putd(' ',(j*ch)+3,1);
              putd(' ',(j*ch)+4,1);
              break;
            case 2:
              putd(' ',(j*ch)+0,2);
              putd('-',(j*ch)+1,2);
              putd('-',(j*ch)+2,2);
              putd('-',(j*ch)+3,2);
              putd(' ',(j*ch)+4,2);
              break;
            case 3:
              putd('|',(j*ch)+0,3);
              putd(' ',(j*ch)+1,3);
              putd(' ',(j*ch)+2,3);
              putd(' ',(j*ch)+3,3);
              putd('|',(j*ch)+4,3);
              break;
            case 4:
              putd(' ',(j*ch)+0,4);
              putd('-',(j*ch)+1,4);
              putd('-',(j*ch)+2,4);
              putd('-',(j*ch)+3,4);
              putd(' ',(j*ch)+4,4);
              break;
          }
          break;
        case '7':
          switch(i){
            case 0:
              putd(' ',(j*ch)+0,0);
              putd('-',(j*ch)+1,0);
              putd('-',(j*ch)+2,0);
              putd('-',(j*ch)+3,0);
              putd(' ',(j*ch)+4,0);
              break;
            case 1:
              putd(' ',(j*ch)+0,1);
              putd(' ',(j*ch)+1,1);
              putd(' ',(j*ch)+2,1);
              putd(' ',(j*ch)+3,1);
              putd('|',(j*ch)+4,1);
              break;
            case 2:
              putd(' ',(j*ch)+0,2);
              putd(' ',(j*ch)+1,2);
              putd(' ',(j*ch)+2,2);
              putd(' ',(j*ch)+3,2);
              putd('|',(j*ch)+4,2);
              break;
            case 3:
              putd(' ',(j*ch)+0,3);
              putd(' ',(j*ch)+1,3);
              putd(' ',(j*ch)+2,3);
              putd(' ',(j*ch)+3,3);
              putd('|',(j*ch)+4,3);
              break;
            case 4:
              putd(' ',(j*ch)+0,4);
              putd(' ',(j*ch)+1,4);
              putd(' ',(j*ch)+2,4);
              putd(' ',(j*ch)+3,4);
              putd(' ',(j*ch)+4,4);
              break;
          }
          break;
        case '8':
          switch(i){
            case 0:
              putd(' ',(j*ch)+0,0);
              putd('-',(j*ch)+1,0);
              putd('-',(j*ch)+2,0);
              putd('-',(j*ch)+3,0);
              putd(' ',(j*ch)+4,0);
              break;
            case 1:
              putd('|',(j*ch)+0,1);
              putd(' ',(j*ch)+1,1);
              putd(' ',(j*ch)+2,1);
              putd(' ',(j*ch)+3,1);
              putd('|',(j*ch)+4,1);
              break;
            case 2:
              putd(' ',(j*ch)+0,2);
              putd('-',(j*ch)+1,2);
              putd('-',(j*ch)+2,2);
              putd('-',(j*ch)+3,2);
              putd(' ',(j*ch)+4,2);
              break;
            case 3:
              putd('|',(j*ch)+0,3);
              putd(' ',(j*ch)+1,3);
              putd(' ',(j*ch)+2,3);
              putd(' ',(j*ch)+3,3);
              putd('|',(j*ch)+4,3);
              break;
            case 4:
              putd(' ',(j*ch)+0,4);
              putd('-',(j*ch)+1,4);
              putd('-',(j*ch)+2,4);
              putd('-',(j*ch)+3,4);
              putd(' ',(j*ch)+4,4);
              break;
          }
          break;
        case '9':
          switch(i){
            case 0:
              putd(' ',(j*ch)+0,0);
              putd('-',(j*ch)+1,0);
              putd('-',(j*ch)+2,0);
              putd('-',(j*ch)+3,0);
              putd(' ',(j*ch)+4,0);
              break;
            case 1:
              putd('|',(j*ch)+0,1);
              putd(' ',(j*ch)+1,1);
              putd(' ',(j*ch)+2,1);
              putd(' ',(j*ch)+3,1);
              putd('|',(j*ch)+4,1);
              break;
            case 2:
              putd(' ',(j*ch)+0,2);
              putd('-',(j*ch)+1,2);
              putd('-',(j*ch)+2,2);
              putd('-',(j*ch)+3,2);
              putd(' ',(j*ch)+4,2);
              break;
            case 3:
              putd(' ',(j*ch)+0,3);
              putd(' ',(j*ch)+1,3);
              putd(' ',(j*ch)+2,3);
              putd(' ',(j*ch)+3,3);
              putd('|',(j*ch)+4,3);
              break;
            case 4:
              putd(' ',(j*ch)+0,4);
              putd('-',(j*ch)+1,4);
              putd('-',(j*ch)+2,4);
              putd('-',(j*ch)+3,4);
              putd(' ',(j*ch)+4,4);
              break;
          }
          break;
      }
    }
  }
  for(i = 0; i < h; i++){
    for(j = 0; j < w; j++){
      putchar(getd(j,i));
    }
    putchar(10);
  }
  return 0;
}

It's a purposely long and over-complicated way of doing it.

What I'm doing is creating a single string (char *display), and treating it as a 2D array. I noticed that arr[y][x] is equivalent to arr[x + y*<width-of-rectangle>], but I seem to get less segfaults doing it the "manual" way.

After writing to the string, I display its contents line-by-line, putting a linefeed (10) after every "line"

Output:

$ ./a.out 425
      ---  --- 
|   |    ||    
 ---  ---  --- 
    ||        |
      ---  --- 
$ ./a.out 36924812
 ---  ---  ---  ---       ---       --- 
    ||    |   |    ||   ||   |   |     |
 ---  ---  ---  ---  ---  ---    |  --- 
    ||   |    ||        ||   |   | |    
 ---  ---  ---  ---       ---       --- 

It looks a little better in an actual terminal.

It's also fun to directly print the string with puts(display);,

$ ./a.out 543
 ---       --- |    |   |    | ---  ---  ---     |    |    | ---       ---

Oh, and because I'm lazy, it assumes you actually entered a string of numbers. Not entering anything will make it try to read the non-existent argv[1], and we all know where that goes...

$./a.out
Segmentation fault
share|improve this answer

Python, 227 chars

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

Simple and straightforward.

share|improve this answer

Perl, 145 characters

$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

Ungolfed:

# 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;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.