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.

Task

The task is to display any of the 128 possible states of a 7-segment display.

Your program should accept a string of 7 characters ("bits") that are either 0 or 1. First bit of input corresponds to segment A of the following illustration, the second to B, etc (ignore dp):

7seg

How you represent the display is up to you -- a single Unicode or ASCII symbol, ASCII art, grafically, or whatever you can come up with. However, each input must have it's own distinct output. If you come up with something fancy I'm sure you can harvest upvotes by showing off some examples.

All 128 possible states of the display are:

states

Rules

  • Codegolf
  • As I said, any kind of output is allowed, but it would be nice if you specified it.
  • Input can be stdin or command line argument.

Examples

Input

1101101

Output

As ASCII/Unicode:
2
Different kinds of ASCII art (I'm not too good at this)
 _   ╺┓  ╒╗   ---
 _|  ┏┛  ╔╝     |
|_   ┗╸  ╚╛   ---
              |
              ---
share|improve this question
    
Closely related to codegolf.stackexchange.com/questions/997/… ? –  David Carraher Dec 20 '12 at 21:49
    
@DavidCarraher: They're related, yes; I even linked to it myself. However, this is slightly harder I would say, as you have 118 more 'numbers' to generate. Most(?) of the answers to the other question would not work here, or would have to be heavily rewritten. Also, here you don't need to encode the different numbers, so other optimizations shuld be possible. –  daniero Dec 20 '12 at 22:02
    
You are correct. Thanks for pointing that out. –  David Carraher Dec 20 '12 at 23:58
    
it is a requirement that we use the encoding you provided? e.g. "1101101" should always represent "2" ? –  ardnew Dec 21 '12 at 17:58
    
@ardnew: yes. The position of the bits in input should map to the alphabetical order of the segments A to G in the first picture. –  daniero Dec 21 '12 at 21:19

25 Answers 25

up vote 4 down vote accepted

J (51)

1 2 1#"1[5 3$' #'{~,|:>0;(88707#:~7#7){>".&.>1!:1[1

output:

1101101
 ## 
   #
 ## 
#   
 ## 
share|improve this answer
1  
This can be shaved down to 38 char with a little effort: 1 2 1#"1' #'{~5 3$,0,.502 A.".&>1!:1]1. –  algorithmshark Apr 10 at 21:56

C, 106

Size is 74 chars if allowed to rename program "W00WG5WW1GW66WG4WW2GW33WG"

main(int i,char**s){
    for(s[0]="W00WG5WW1GW66WG4WW2GW33WG";i=*s[0]++;putchar(s[1][i&7]-49?i==71?10:32:42));
}

running:

./a.out 1101101
 ** 
   *
 ** 
*   
 ** 

notes:

'W' and 'G' (0x47 and 0x57) are chosen such that the value & 7 = 7, i.e they safely index the null character that terminates the input string.

share|improve this answer

Brainfuck - 224

++++++++++[>+>+++<<-]
>>++>,>,>,>,>,>,>,
<<<<<<<.
>[<+.->-]<<.
>>
>>>>>[<<<<<+<+>>>>>>-]<<<<<<.>[<->-]<.>>[<<+.->>-]<<<.>.
>>>>>>>[<<<<<<<+.->>>>>>>-]<<<<<<<<.
>>>>>>[<<<<+<+>>>>>-]<<<<<.>[<->-]<.
>>>[<<<+.->>>-]<<<<.>.
>>>>[<<<<+.>]

Prints using Exlamation points:

 ! 
! !
 !
! !
 !

Not the most readable, but not too horrible either.

Surprised at how close this is to not being last place.

share|improve this answer

Postscript 121 107

1 12 moveto{}5 0{0 -5 rmoveto}0 5 0 5 -5 0 0 -5 0 -5 5 0
n{49 eq{rlineto}{rmoveto}ifelse exec}forall stroke

requires n to be defined as the string to process so invoke like

gs -g7x14 -sn=1101101 lcd.ps

to get

image of number two

the complete set is

complete set of characters

share|improve this answer

APL, 58 55

' _|'[1+3 3⍴0,x[1],0,(x←1 2 2 1 2 2 1×⍞='1')[6 7 2 5 4 3]]

Example input:

1101101

Output:

 _ 
 _|
|_ 

⍞='1' takes the input as a character array and converts it to a numeric array.

1 2 2 1 2 2 1×⍞='1' converts that array to: 0 for blank, 1 for _, 2 for |

(x←1 2 2 1 2 2 1×⍞='1')[6 7 2 5 4 3] assign that array to variable x and reorder to represent segments F, G, B, E, D, C

0,x[1],0,(x←1 2 2 1 2 2 1×⍞='1')[6 7 2 5 4 3] concatenates a blank, segment A and another blank to the front

3 3⍴0,x[1],0,(x←1 2 2 1 2 2 1×⍞='1')[6 7 2 5 4 3] reshape to a 3x3 matrix

1+3 3⍴0,x[1],0,(x←1 2 2 1 2 2 1×⍞='1')[6 7 2 5 4 3] converts to 1-based indexing

Finally uses the string ' _|' to convert indicies into characters


Edit

' _|'[1+3 3⍴(0,1 2 2 1 2 2 1×⍞='1')[1 2 1 7 8 3 6 5 4]]

Shaved off 3 chars by concatenating a 0 to front the array and using duplicate indicies, preventing a variable assignment

share|improve this answer

Mathematica: 135 129 118, image display

Image[MorphologicalComponents@Import@"http://goo.gl/j3elE" /. 
      Thread[Range@7 -> IntegerDigits[#, 2, 7][[{7, 2, 6, 1, 3, 5, 4}]]]] &

spaces added "for clarity"

enter image description here

Edit

For those picky fellow site users: Without using an external mask:

Image[MorphologicalComponents[ColorNegate@Rasterize@8, CornerNeighbors -> False] /. 
    Thread[Range@7 ->IntegerDigits[#, 2, 7][[{7, 2, 6, 1, 3, 5, 4}]]]] &
share|improve this answer
    
The code appears to be incomplete. I don't think you wanted your program to terminate with &. –  David Carraher Dec 22 '12 at 22:11
    
@DavidCarraher It's a function,so it ends with &. –  belisarius Dec 22 '12 at 22:16
    
Yes, but it is not applied to anything. How does one obtain output? –  David Carraher Dec 22 '12 at 23:25
    
@DavidCarraher i = Import@"http://goo.gl/j3elE"; r = Range@7; Image[MorphologicalComponents@i /. Thread[r -> {7, 2, 6, 1, 3, 5, 4}] /. Thread[r -> IntegerDigits[#, 2, 7]]] & /@ Range[0, 11] –  belisarius Dec 22 '12 at 23:29
    
Yes. (Of course!) Thanks –  David Carraher Dec 22 '12 at 23:42

PHP 66 bytes

<?for(;11>$i;)echo++$i&3?$argv[1][md5(¡æyÚ.$i)%8]?$i&1?~ƒ:_:~ß:~õ;

A slight improvement using an md5 magic formula, but requires 3 additional binary bytes:
¡, æ, and Ú are characters 161, 230, and 218 respectively. It should work as is if copied directly, and saved as an ANSI format.


PHP 73 (70) bytes

<?for($s=327638584;3<$s;)echo++$i&3?$argv[1][7&$s/=8]?$i&1?'|':_:' ':'
';

If you'll allow me three binary characters, this can be reduced to 70 bytes:

<?for($s=327638584;3<$s;)echo++$i&3?$argv[1][7&$s/=8]?$i&1?~ƒ:_:~ß:~õ;

where ƒ, ß, and õ are characters 131, 223, and 245 respectively.

Receives input as a command line argument. Sample usage:

$ php seven-seg.php 1101101
 _
 _|
|_

$ php seven-seg.php 0111011

|_|
 _|
share|improve this answer
    
i tried to run your code, but am getting loads of error :( i dont understand your logic but seems interesting. –  D34dman Feb 25 '13 at 16:26
1  
@D34dman The only messages it will produce are notices (undefined variables, etc). These can either be turned off in php.ini (by default they already are), or for testing, you can prepend the following code to the script: <? error_reporting(E_ALL & ~E_NOTICE); ?> –  primo Feb 26 '13 at 5:25
    
awesome! i got it working :D naice! –  D34dman Feb 26 '13 at 8:30

JavaScript + jQuery + HTML + CSS (210 201)

This solution uses CSS sprites and the image provided as an example:

HTML (3)

<a>

CSS (82 71)

Thanks to xem for the "background:url" trick:

a{background:url(//bit.ly/VesRKL);width:13px;height:23px;display:block}

JavaScript (125 after removing newlines added here for readability)

i=prompt();
x=-parseInt(i.substr(0,3),2)*23;
y=-parseInt(i.substr(3),2)*13.75;
$('a').css('background-position',y+'px '+x+'px');

Online test: http://jsfiddle.net/zhqJq/3/

share|improve this answer
    
Hey, but what about the 17.937 bytes of the image?? ;) –  Thomas W. Dec 27 '12 at 17:35
    
@ThomasW. I don't know whether that should be counted as well. Let's leave this to the OP to decide. –  w0lf Dec 27 '12 at 20:33
    
You could use <p> tag and avoid display:block on CSS to save space –  Eagle Apr 10 at 20:42
    
background-image:url(bit.ly/VesRKL); => background:url(//bit.ly/VesRKL); –  xem Apr 10 at 21:28
    
@xem thanks for the tip; I've edited my answer –  w0lf Apr 11 at 13:09

APL 101 86 73 69

m←45⍴' '⋄m[¯40+⎕av⍳(⍎∊3/' ',¨⍕⍞)/')*+16;EJOQRSAFK-27=>?']←'⎕'⋄9 5⍴m

Input is from the screen via ⍞

1101101

Which produces a 9x5 character matrix composed of blanks and ⎕ as follows:

 ⎕⎕⎕
     ⎕
     ⎕
     ⎕
 ⎕⎕⎕
⎕
⎕
⎕
 ⎕⎕⎕

The seven digit number is converted into a partition vector to select the ⎕ co-ordinates.

share|improve this answer
    
My head hurts... –  Rob Quist Apr 10 at 14:52

PostScript: 53 binary, 87 ASCII 52 binary, 86 ASCII

Hexdump of the program using binary tokens:

$ hexdump -C lcd_binary.ps 
00000000  28 34 34 30 34 30 38 30  34 30 34 30 30 30 30 34  |(440408040400004|
00000010  30 34 30 34 34 34 34 34  38 34 38 30 38 29 7b 7d  |0404444484808){}|
00000020  92 49 6e 7b 92 70 31 92  04 92 96 92 6b 92 63 92  |.In{.p1.....k.c.|
00000030  a7 7d 92 49                                       |.}.I|
00000034

Download this file to try it out.

Using ASCII tokens:

(4404080404000040404444484808){}forall
n{not 1 and setgray moveto lineto stroke}forall

First forall loop puts all required coordinates on the stack. The coordinates are stored in a string to minimize required space. The coordinates are in reverse order, i.e. the last 4 chars are for segment A. We draw a line from (0,8) to (4,8) for this segment (actually, we have to add 48 to all coordinates, because forall puts all ASCII codes on the stack).

The second forall loops through all the 0s and 1s in the input string and turns them into a gray value. 0s are drawn white (gray value 1) and 1s are drawn black (gray value 0). Then we use the coordinates that the first forall loop left on the stack to draw the lines.

Invoke the program using Ghostscript, just like Geoff Reedy's:

gs -sn=1101101 lcd.ps

This displays: Sample output

share|improve this answer
    
I think you can replace neg 49 add (which is awesome btw) with 1 and. –  luser droog Dec 27 '12 at 6:25
    
@luserdroog: Brilliant, bitwise operators, haven't thought of them. But it has to be not 1 and, right? Still saves one byte in both ASCII and binary. –  Thomas W. Dec 27 '12 at 7:37
    
I feel there's gotta be a neat way with a 1bit bitmap. but bitshift is such a long word. –  luser droog Dec 27 '12 at 7:45
    
But it's only a two byte binary token –  Thomas W. Dec 27 '12 at 7:48
    
Yeah. But it still feels like a cheat to me somehow. :) I feel I need to do as much as possible with readable source before resorting to binary. –  luser droog Dec 27 '12 at 7:49

Mathematica 112 103 100 96 88, using Graphs

HighlightGraph[z=GridGraph@{3,2},Pick[EdgeList[z][[{3,4,1,2,6,7,5}]],Characters@#,"1"]]&

enter image description here

Using it to show a calculator display

l = {7-> 7, 1-> 6, 0-> 63, 8-> 127, 9-> 111,3 -> 79, 5-> 109,  2-> 91, 4-> 102, 6-> 125}; 
GraphicsRow[
  HighlightGraph[z = GridGraph[{3,2}, EdgeStyle-> {White}, GraphHighlightStyle-> {"Thick"}], 
    EdgeList[z][[{3, 4, 1, 2, 6, 7, 5}]][[IntegerDigits[#, 2, 7] Range@7]]] & /@
            (IntegerDigits[8736302] /. l)]

Mathematica graphics

share|improve this answer
    
What sort of input do you expect? Neither [123] nor ["123"], nor IntegerDigits[123] work for me. I get only z, with no highlighting. –  David Carraher Jan 12 '13 at 22:04
    
@dude Try `HighlightGraph[z = GridGraph@{3, 2}, Pick[EdgeList[z][[{3, 4, 1, 2, 6, 7, 5}]], Characters@#, "1"]] &@"1111111"`` :) –  belisarius Jan 13 '13 at 4:52

R (65 chars):

plot((3^(1i*1.5:-4.5)*(1:7!=7)),col=strsplit(readline(),'')[[1]])

Relies on some loose approximations for some transcendental nuumbers ...

share|improve this answer

Python 2 - 65

s=raw_input()+'0\n'
for i in`0x623C239E38D2EAA1`:print s[int(i)],

Example:

echo 1101101|python2 7seg.py
0 1 0 
0 0 1 
0 1 0 
1 0 0 
0 1 0
share|improve this answer

Python (107)

Definitely more golfable.

a=map(int,raw_input())
for i in(0,6,3):print' .. '*a[i]+('\n'+' .'[a[5-i/6]]+'  '+' .'[a[1+i/6]])*(2*(i!=3))

Output:

1101101
 .. 
   .
   .
 .. 
.   
.   
 .. 

Explanation:

a is a list of booleans extracted from the input.
When you multiply a string with a number, it will return the string repeated (number) times.
If that number happens to be zero, it returns an empty string.
i is iterated through 0 (pos A), 6 (pos G), and 3 (pos D).
' .. ' will print either section A, G, or D, depending on the value of i.
([string here])*(2*(i!=3)) will print [string here] twice only if i!=3.
Breaking down [string here]:
 - '\n' will print a newline for each repetition.
 - '  ' is the null space between horizontal sections.
 - ' .'[(bool)] will return either ' ' if (bool) is 0, and '.' if (bool) is 1.
 - 5-i/6 will return 5 if i=0 and 4 if i=6. a[5] is section F and a[4] is section E.
 - 1+i/6 will return 1 if i=0 and 2 if i=6. a[1] is section B and a[2] is section C.
share|improve this answer

Python 2.7 (93 chars):

print (' {0}\n{5} {1}\n {6}\n{4} {2}\n {3}').format(*map(lambda x:int(x)and'*'or' ',raw_input()))

Explanation:

With the stdin input, use a makeshift ternary operator to give * for true and a space for false. Take those values and plug them into a format statement that's in the format of the 7 digit display. It'd be at most 83 characters if the display worked like this:

 a
b c
 d
e f
 g

but the ordering makes it longer. Anyone have a way around this?

Example:

$ ./7seg.py
111000

 *
  *

  *

$ ./7seg.py


1111111

 *
* *
 *
* *
 *
share|improve this answer
1  
Nice, but that lambda seems unnecessary: print' {0}\n{5} {1}\n {6}\n{4} {2}\n {3}'.format(*[' *'[i=='1']for i in raw_input()]) ;) No need for brackets and space for the print statement either. –  daniero Jan 6 '13 at 20:46
    
Wow! I didn't know you could put booleans in indexers. Thanks :-) –  FakeRainBrigand Jan 6 '13 at 21:30
    
Np. Also, >'0' instead of =='1' and input() (with backticks around, but that won't show up because of the formatting here) instead of raw_input(). –  daniero Jan 6 '13 at 21:52
    
fyi, you can use tripple backticks if you need backticks in the code x = `input()` –  FakeRainBrigand Jan 6 '13 at 22:00
    
Cool. Then I learned something today too :) –  daniero Jan 6 '13 at 23:21

I thought we needed a lispy answer...

Clojure, 159 chars

(print(apply str(flatten(interpose\newline(partition 3(map(fn[x](if(= x\1)\o" "))(str" "(apply str(interpose" "(map(vec(read-line))[0 5 1 6 4 2 3])))" ")))))))

The above will run in the REPL and provide the correct answer. For example:

1111111
 o 
o o
 o 
o o
 o 

Throwing numbers at it with small modifications:

(doseq [i ["1111110" "0110000" "1101101" "1111001" "0110011" "1011011" "1011111" "1110000" "1111111" "1111011"]]
(println (apply str(flatten(interpose\newline(partition 3(map(fn[x](if(= x\1)\o" "))(str" "(apply str(interpose" "(map(vec i)[0 5 1 6 4 2 3])))" "))))))) 
(println))

yields:

 o 
o o

o o
 o 


  o

  o


 o 
  o
 o 
o  
 o 

 o 
  o
 o 
  o
 o 


o o
 o 
  o


 o 
o  
 o 
  o
 o 

 o 
o  
 o 
o o
 o 

 o 
  o

  o


 o 
o o
 o 
o o
 o 

 o 
o o
 o 
  o
 o 

nil

Not easy to read, but they're there!

share|improve this answer

Javascript 123

s=prompt(i=o='');for(m=' 0516423';i++<15;i%3==0?o+='\n':1)o+=' ─│'[~i%2&(A=+s[+m[~~(i/2)]])+(A&+'110'[i%3])];console.log(o)

I can bring the character count lower (101) if we use only one character for the "on" state, but it is less legible:

s=prompt(i=o='');for(m=' 0516423';i++<15;i%3==0?o+='\n':1)o+=' ■'[~i%2&s[+m[~~(i/2)]]];console.log(o)
share|improve this answer
    
+1 This is impressive. I'm still trying to reverse-engineer it. It's taught me a few things already! –  guypursey Mar 7 '13 at 18:28

Postscript 136

Not winner, but a different approach.

15 string exch{1 and 255 mul}forall
[7 3 9 13 11 5 1]{count 1 sub index 3 1 roll exch put}forall
3 5 8[.02 0 0 .05 0 0]{}image showpage

Expects the input string to be on the stack:

$ echo '(1101101)'|cat - 7seg.ps |gs -sDEVICE=png16 -sOutputFile=6c.png -

This one's even worse. 294 to make a "binary" bitmap. I took me a while to remember that each row is padded to an even byte. So a 3x5 bitmap is five bytes with the 3 msb bits significant.

2#1101101
(12345)exch
2 copy 64 and 0 exch put %A
2 copy 2 and 6 bitshift 2 index 32 and or 1 exch put %F B
2 copy 1 and 6 bitshift 2 exch put %G
2 copy 4 and 5 bitshift 2 index 16 and 1 bitshift or 3 exch put %E C
2 copy 8 and 3 bitshift 4 exch put
pop
3 5 1[.02 0 0 .02 0 0]{}image showpage

Output is just as ugly as the other one. :(

Alright here's one that looks good. 190

Edit: It was upside-down and backwards. Fixed now.

(1101101)
{neg 49 add 255 mul
1 string dup 0 4 3 roll put}forall
(\377){@/g/f/e/d/c/b/a}{exch def}forall
@ a a @
b @ @ f
b @ @ f
@ g g @
c @ @ e
c @ @ e
@ d d @
4 7 8[.01 0 0 .01 0 0]{}image showpage
share|improve this answer
    
This shows me some funny patterns. Is there still a bug somewhere? –  Thomas W. Dec 27 '12 at 8:10
    
hmm. yeah. (1111110) does not look like a zero. It's just an enlarged bytemap. I guess boxes is oversimplified. :( –  luser droog Dec 27 '12 at 8:35
    
it's kindof in the nature of this bitmap approach, I think. I'm not sure how to make it not ugly. –  luser droog Dec 27 '12 at 9:13
    
Some light commentary available here. –  luser droog Jan 25 '13 at 7:04

Mathematica 264

Getting the output to look right required many bytes, so no cigar this time. But here's the verbose (264 chars) code anyway.

{a, b, c, d, e, g} = {{-1, 5}, {1, 5}, {1, 3}, {1, 1}, {-1, 1}, {-1, 3}};
f@n_ := Graphics[{Yellow, Thickness[.1], CapForm["Round"],
   Line /@ {{g, c}, {g, a}, {g, e}, {e, d}, {d, c}, {c, b}, {b, a}}[[Flatten@
   Position[IntegerDigits[n, 2, 7], 1]]]}, 
   Background -> Blue, PlotRange -> {{-1, 1}, {1, 5}}, PlotRangePadding -> 1]

The complete set of characters:

GraphicsGrid[Partition[Table[f[p], {p, 0, 128}], 16]]

characteris

The digits:

{f[63], f[6], f[91], f[79], f[102], f[109], f[125], f[7], f[127], f[111]}

enter image description here

share|improve this answer

PHP - 155 characters

<?php
$y=array("   \n"," - \n","   \n","  |\n","|  \n","| |\n"); $x = $argv[1]; echo $y[$x[0]].$y[2*$x[6]+$x[2]+2].$y[$x[7]].$y[2*$x[5]+$x[3]+2].$y[$x[4]];

it would be 150 characters if we use php 5.4 type array declaration, but i dont have that installed on my laptop so couldn't test it.

Sample out puts.

enter image description here

Explanation:

First i divided the 7 segment display to Five rows and 3 columns. With 1st, 3rd and 5th row havimg '-' in the middle column, and space otherwise.

The 2nd and 4th row has a pipe '|' character in the first and last column. Now the presence of these character should be guided by the input values.

I created a lookup table, which is basically two lookup table. First one for the calculation of values for 1st, 3rd and 5th row. And another one at offset 2 ( 3rd item ) for calculation of rows 2nd and 4th.

share|improve this answer

VBA - 263

It's ugly but it works, I think. I am having trouble viewing the proper bit order, so I'm inferring from others' answers. Even if that piece is wrong, the code length should remain the same.

Sub d(b)
Dim c(1 To 7)
For a=1 To 7
c(a)=Mid(b,a,1)
Next
x=" - "
y="|"
z=" "
w="   "
v=vbCr
MsgBox IIf(c(1)=1,x,w) & v & IIf(c(6)=1,y,z) & z & IIf(c(2)=1,y,z) & v & IIf(c(7)=1,x,w) & v & IIf(c(5)=1,y,z) & z & IIf(c(3)=1,y,z) & v & IIf(c(4)=1,x,w)End Sub
share|improve this answer

VBScript - 178 characters

m=Split("2 7 11 10 9 5 6")
s=" _ "&vbCr&"|_|"&vbCr&"|_|"
For x=1 To 7
If Mid(WScript.Arguments.Item(0),x,1)=0 Then r=m(x-1):s=Left(s,r-1)&" "&Right(s,Len(s)-r)
Next
MsgBox s
share|improve this answer
    
+1 Welcome to the site! –  luser droog Mar 3 '13 at 4:16
    
Thanks! Couple bonehead mistakes cost me 6 characters. I can almost get this down to the size of the VBA one even with the absurdly long call to get the function line argument. –  Comintern Mar 3 '13 at 4:31
    
Unless your code is functional without the newline characters, you have to count them too. This has 178 characters according to my count. –  manatwork Mar 4 '13 at 9:58
    
Thanks for the counting tip. Corrected. –  Comintern Mar 6 '13 at 1:02

CJam - 29

l0N]s7078571876784728737Ab\f=

CJam is a new language I am developing, similar to GolfScript - http://sf.net/p/cjam. Here is the explanation:

l reads a line from the input
0 is the number 0
N is a variable preinitialized to the newline string
] gathers the elements on the stack into an array
s converts a value (the array) to string, thus appending a zero and a newline to the given input
7078571876784728737 is a number (the same number I used in python, but it was in hex there)
A is a variable preinitialized to 10
b does a base conversion, generating the array [7 0 7 8 ... 3 7]
\ swaps the last two values on the stack
f= applies the = operator (here, indexed array access) on the input string (plus zero and newline) and each number 7, 0, 7, ...
The index 7 corresponds to the appended zero, and 8 corresponds to the appended newline.

My python solution does exactly the same thing (except the digit separation is done via string conversion)

share|improve this answer
    
What am I looking at here? Care to offer a brief explanation of the code? –  daniero Apr 10 at 16:47
    
@daniero I added an explanation –  aditsu Apr 10 at 18:30

Java - 204 characters

class A{public static void main(String[]a){char[]c=new char[7];for(int i=0;i<7;i++)c[i]=a[0].charAt(i)==49?i%3==0?95:'|'):32;System.out.printf(" %c %n%c%c%c%n%c%c%c",c[0],c[5],c[6],c[1],c[4],c[3],c[2]);}}

Sample output:

 _ 
 _|
|_ 

Formatted properly:

class A {
    public static void main(String[] a) {
        char[] c = new char[7];
        for (int i = 0; i < 7; i++)
            c[i] = a[0].charAt(i) == 49 ? (i % 3 == 0 ? 95 : '|') : 32;
        System.out.printf(" %c %n%c%c%c%n%c%c%c", c[0], c[5], c[6], c[1], c[4], c[3], c[2]);
    }
}

Really wish I could avoid that for loop, but I tried a few other things and they were all longer. There's probably a better way to do this, but this is my first attempt at code golf. (And Java's like the worst language for it, which is why I thought it would be interesting.) Even Brainfuck has me beat, but at least my output looks nicer.

EDIT: can get rid of "public" on class, saves me 7 chars!

And thanks, daniero, for showing me printf! (18 chars saved)

Rewrote the output format, changed character literals to decimal, 12 chars saved.

share|improve this answer
    
Welcome to codegolf.se! Java introduced a printf method a few versions back, which is basically println and format in one (like the C function); This will save you some characters, –  daniero Apr 10 at 22:01
    
Here's another trick: You can combine the "body" of the for loop and the update part into one, saving one character: for(int i=7;i>0;c[--i]=a[0].charAt(i)==49?(i%3==0?95:'|'):32); –  daniero Apr 11 at 14:51

VBA - 188 characters

Note that the one has to type 188 characters if only including mandatory whitespace -- the IDE expands it out when you copy it into the VBA editor.

Sub f(i)
Dim c() As Byte
m=Split("1 6 10 9 8 4 5")
c=StrConv(" _  |_| |_|",128)
c(3)=10
c(7)=10
For x=1 To 7
If Mid(i,x,1) = 0 Then c(m(x-1))=32
Next
MsgBox StrConv(c,64)
End Sub

Sadly, VBScript doesn't have a strongly typed Byte array, or that one could be much shorter using this method.

share|improve this answer
    
Unless your code is functional without the newline characters, you have to count them too. This has 188 characters according to my count. –  manatwork Mar 4 '13 at 10:03

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.