| Bytes | Lang | Time | Link |
|---|---|---|---|
| 188 | JavaScript V8 | 240717T074436Z | Andrew B |
| 146 | JavaScript Node.js | 200218T002447Z | Arnauld |
| 341 | Java 8 | 180504T134516Z | Kevin Cr |
| 319 | C | 150609T011053Z | some use |
| 719 | Brainfuck | 150613T024403Z | primo |
| 062 | CJam | 150611T130855Z | Dennis |
| 103 | Perl 103 Bytes | 150610T112320Z | primo |
| 136 | KDBQ | 150610T153020Z | WooiKent |
| nan | Pip | 150611T084459Z | DLosc |
| 191 | JavaScript ES6 | 150610T220214Z | edc65 |
| 270 | Perl | 150610T195949Z | fvla |
| 331 | C# | 150609T120900Z | Shion |
| 162 | JavaScript | 150609T103104Z | Regret |
| nan | python 2 | 150609T012206Z | Alexey B |
| 174 | Python 3 | 150609T021927Z | Sp3000 |
JavaScript (V8), 188 bytes
f=x=>{o='';t=' ';p=`
`
for(k in x){q=r=s=''
for(i=0;i<7;i++){b="wƒk[ƞ]}Ɠÿ_".charCodeAt(x[k])>>i&1?i%3?'\\':'/':' '
i<2?q+=b:i>4?s+=b:r+=b}o=r+p+t+' '+s+p+o
t=q
p+=' '}return p+t+p+o}
Inspired by @Regret's answer, I attempted to do this by building the string from the bottom up thus avoiding the split().reverse().join() code.
However, this requires a lot of extra code to handle the temporary storage.
JavaScript (Node.js), 149 146 bytes
Contains a few unprintable characters which are escaped in the code below.
s=>(g=([d,...s],p='',q=p)=>d?g(s,p+q,88)+(p+=q?`10 65
${p}234
`:865).replace(/\d/g,n=>'\\/ '[Buffer("w\x11k;\x1d>~\x13\x7f?")[n>4?s[0]:d]>>n&1?n&1:2]):s)(0+s)
How?
This recursively builds the following pattern (here with 4 digits):
88888810 65
888888234
888810 65
8888234
8810 65
88234
10 65
234
865
Where:
- \$8\$ is a padding character
- other even digits are replaced with either
\or a space - odd digits are replaced with either
/or a space
Leading to:
______/\ \/
______\/\
____/\ \/
____\/\
__/\ \/
__\/\
/\ \/
\/\
_\/
Java 8, 341 bytes
n->{int i=n.length*2,j=i+1,k=0,t;String l[]=new String[j],x;for(;j-->0;l[j]="");for(;i>0;l[i--]+=" "+x.substring(5,7),l[i--]+=x.substring(2,5),l[i]+=x.substring(0,2))for(x="/~~ ~~/ ~ ~ /~ / ~//~ /~ / ~~/~ / ~/~ // ~/~~//~ ~ /~~/~~//~~/~ /".replace('~','\\').substring(t=(n[k++]-48)*7,t+7),j=i-2;j-->0;)l[j]+=" ";return"".join("\n",l);}
Port of @Shion's C# .NET answer, so make sure to upvote him as well!
Explanation:
n->{ // Method with character-array parameter and String return-type
int i=n.length*2, // Two times the length of the input array
j=i+1, // Index integer, starting at `i+1`
k=0,t; // Temp integers
String l[]=new String[j],// String-array for the rows, default filled with `null`
x; // Temp-String
for(;j-->0;l[j]=""); // Replace all `null` with empty Strings
for(;i>0 // Loop `i` downwards in the range [`n.length*2`, 0)
; // After every iteration:
l[i--]+= // Append the row at index `i` with:
// (and decrease `i` by 1 afterwards with `i--`)
" " // A space
+x.substring(5,7),// And the 6th and 7th characters of temp-String `x`
l[i--]+= // Append the row at index `i` with:
// (and decrease `i` by 1 afterwards with `i--`)
x.substring(2,5), // The 3rd, 4th and 5th characters of temp-String `x`
l[i]+= // Append the row at index `i` with:
x.substring(0,2)) // The 1st and 2nd characters of the temp-String `x`
for(x="/~~ ~~/ ~ ~ /~ / ~//~ /~ / ~~/~ / ~/~ // ~/~~//~ ~ /~~/~~//~~/~ /".replace('~','\\')
// String containing all digit-parts
.substring(t=(n[k++]-48)*7,t+7),
// and take the substring of 7 characters at index
// `n[k]` as integer multiplied by 7
j=i-2;j-->0;) // Inner loop `j` in the range (`i`-2, 0]
l[j]+=" "; // And append the rows at index `j` with two spaces
return"".join("\n",l);} // Return the rows delimited with new-lines
C, 1098 345 323 319 bytes
First Second Third attempt. Finally decided to ditch the screen buffer to save a few bytes. This program takes a parameter of digits and prints the digits in 7-segment format.
First time participant. Just for fun. Be gentle.
a[]={100489,2056,98569,67849,2440,67969,100737,2057,100745,67977},i,j,k,n,m;char*c=" /\\";
#define f(q,r) for(q=0;q<(r);q++)
#define P(w) putchar(w)
#define Q(d,i,j) P(c[a[v[1][d]-48]>>(i*3+j)*2&3])
main(w,char**v){f(i,n=strlen(v[1]))f(k,(m=n-i-1)?2:3){f(j,m*2)P(32);f(w,3)Q(m,k,w);if(!k&&i)f(w,2)Q(m+1,2,w+1);P(10);}}
Expanded, warning free:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int a[]={100489,2056,98569,67849,2440,67969,100737,2057,100745,67977};
char *c=" /\\";
#define f(q,r) for(q=0;q<(r);q++)
#define P(w) putchar(w)
#define Q(d,i,j) P(c[a[v[1][d]-48]>>(i*3+j)*2&3])
int main(int w, char **v)
{
int i,j,k,n,m;
f(i,n=strlen(v[1])) {
m=n-i-1;
f(k,m?2:3) {
f(j,m*2) P(32);
f(w,3) Q(m,k,w);
if (!k&&i) f(w,2) Q(m+1,2,w+1);
P(10);
}
}
}
Brainfuck - 719 bytes
For historical context only, credits to Daniel B Cristofani. I'm not exactly sure when this was created, but it is available from the Internet Archive as early as May 9th, 2003.
The output for 9 is different than in the problem description.
>>>>+>+++>+++>>>>>+++[
>,+>++++[>++++<-]>[<<[-[->]]>[<]>-]<<[
>+>+>>+>+[<<<<]<+>>[+<]<[>]>+[[>>>]>>+[<<<<]>-]+<+>>>-[
<<+[>]>>+<<<+<+<--------[
<<-<<+[>]>+<<-<<-[
<<<+<-[>>]<-<-<<<-<----[
<<<->>>>+<-[
<<<+[>]>+<<+<-<-[
<<+<-<+[>>]<+<<<<+<-[
<<-[>]>>-<<<-<-<-[
<<<+<-[>>]<+<<<+<+<-[
<<<<+[>]<-<<-[
<<+[>]>>-<<<<-<-[
>>>>>+<-<<<+<-[
>>+<<-[
<<-<-[>]>+<<-<-<-[
<<+<+[>]<+<+<-[
>>-<-<-[
<<-[>]<+<++++[<-------->-]++<[
<<+[>]>>-<-<<<<-[
<<-<<->>>>-[
<<<<+[>]>+<<<<-[
<<+<<-[>>]<+<<<<<-[
>>>>-<<<-<-
]]]]]]]]]]]]]]]]]]]]]]>[>[[[<<<<]>+>>[>>>>>]<-]<]>>>+>>>>>>>+>]<
]<[-]<<<<<<<++<+++<+++[
[>]>>>>>>++++++++[<<++++>++++++>-]<-<<[-[<+>>.<-]]<<<<[
-[-[>+<-]>]>>>>>[.[>]]<<[<+>-]>>>[<<++[<+>--]>>-]
<<[->+<[<++>-]]<<<[<+>-]<<<<
]>>+>>>--[<+>---]<.>>[[-]<<]<
]
[Enter a number using ()-./0123456789abcdef and space, and hit return.
Daniel B Cristofani (cristofdathevanetdotcom)
http://www.hevanet.com/cristofd/brainfuck/]
CJam, 77 71 70 69 63 62 bytes
r_,5*_Sa*a*\{~"÷Ðëúܾ¿ðÿþ"=i2bS"\/"4*W<+.*3/..e>2fm>2m>}/Wf%N*
All characters are printable, so copy and paste should work just fine.
Try it online in the CJam interpreter.
Idea
We start by examining the number of digits n in the input and pushing a square of spaces big enough to cover the output. In the implementation, this square will be encoded as a two-dimensional array of one-character strings.
A square of length 2n+1 would be just right (i.e., no surrounding whitespace) for a straightforward implementation, but we'll use one of length 5n to save a couple of bytes. Thankfully, surrounding whitespace is allowed.
If we reverse the lines of the seven slash representation of 8, we obtain the following:
\/
\/\
/\
The representation of all digits can be encoded as an 8-bit integer, where the ith bit is 0 iff the ith character should get replaced with a space. For the digits 0 to 9, the resulting integers are
247 208 235 250 220 190 191 240 255 254
which correspond to the following ISO-8559-1 characters:
÷Ðëúܾ¿ðÿþ
For each digit in the input, after selecting the corresponding 8-bit integer, we repeat the ith character of the representation of 8 exactly ai times, where ai is the ith bit of the integer. This pushes an array of strings of either one or zero characters. By dividing this array into chunks of length 3, we obtain an array where each element corresponds to a line of the representation.
Now, we compute the vectorized maximum of the strings that represent the square and the strings that represent the digit. The strings / and \ are bigger than the string , so they will replace the spaces in the square. The empty string, however, is smaller than the string , so empty strings in the digit representation will preserve the spaces in the square.
We now rotate the rows and columns by two units to place the following digit representation in the proper part of the square and repeat the process for the remaining digits in the input.
Finally, we reverse each row and insert a linefeed between the individual rows.
Code
r_, e# Read a token from STDIN and push the length of a copy.
5*_ e# Multiply the length by 5 and push a copy.
Sa* e# Repeat the array [" "] that many times.
a* e# Repeat the array [[" " ... " "]] that many times.
\{ e# For each character C in the input:
~ e# Push eval(C), i.e., the digit the character represents.
"÷Ðëúܾ¿ðÿþ"
e# Push the encodings of all 10 seven slash representations.
= e# Select the proper one.
i2b e# Push the resulting characters code point in base 2, i.e., its bits.
S e# Push " ".
"\/"4* e# Push "\/\/\/\/".
+W< e# Concatenate and eliminate the last character.
.* e# Vectorized repetition.
e# For the digit 5, e.g., we have [1 0 1 1 1 1 1 0] and " \/\/\/\" on
e# the stack, so .* yields [" " "" "/" "\" "/" "\" "/" ""].
3/ e# Divide the representation into chunks of length 3, i.e., its lines.
..e> e# Compute the twofold vectorized maximum, as explained above.
2fm> e# Rotate each line to characters to the right.
2m> e# Rotate the lines two units down.
}/
Wf% e# Reverse each line.
N* e# Place linefeeds between them.
The last rotations would mess up the output if the square's side length was smaller than 2n+3. Since 5n ≥ 2n+3 for all positive integers n, the square is big enough to prevent this.
Perl - 103 Bytes
#!perl -n
print$i+$%2?U^(u,$i--%2?v9:z)[$i<4+$%2&vec$_,4*$-3-$i,1]:$/.!($i=$--)
while$+=2*y/0-9/wPkz\\>?p~/
The above contains 6 unprintable characters (the source can be downloaded at Ideone), and is equivalent to the following:
#!perl -n
print$i+$^F%2?U^(u,$i--%2?v9:z)[$i<4+$^F%2&vec$_,4*$^F-3-$i,1]:$/.!($i=$^F--)
while$^F+=2*y/0-9/wPkz\\>?p\177~/
Each ^F may be replaced by a literal character 6 (ACK), and \177 replaced by character 127 (DEL).
The shebang is counted as 1, the second newline is uncessary. Input is taken from stdin.
Sample Usage
$ echo 0123 | perl seven-slash.pl
/\
/\
/\ /
/
\ \/
\
/\
\ \
\/
$ echo 456789 | perl seven-slash.pl
/\
\/\
/\ /
\/\
/\ \/
\
/
\/\
/ \/
\/\
\ /
\/\
Explanation
Output is generated one byte at a time. Each character is transliterated, and this is then interpreted as a bit array using vec. The bits are stored in the following way:
/\ 56
\/\ 234
/\ \/ -> 56 01
\/\ 234
\/ 01
Output alternates between 3 and 5 slashes, so that bits 56 spills over into 01 of the next digit. Bit 7 is not used.
KDB(Q), 172 136 bytes
{d:(9#1 2 0 2)*/:-9#'0b vs'427 136 403 409 184 313 315 392 443 441;
-1" /\\"{m+(c,/:y),c:(count[m:0 0,x,\:0 0]-3)#0}/[3 3#/:d"J"$'(),x];}
Explanation
1) Create d map with all the digits' shapes.
2) Pad the matrix with extra zeros and add them together. i.e. "01"
0 0 0 0 2 0
0 0 0 0 0 2
1 2 0 0 0 + 0 0 0 0 0
2 0 2 0 0 0
0 2 1 0 0 0
3) Use the index to map " /\" and print with -1.
Test
q){d:(9#1 2 0 2)*/:-9#'0b vs'427 136 403 409 184 313 315 392 443 441;-1" /\\"{m+(c,/:y),c:(count[m:0 0,x,\:0 0]-3)#0}/[3 3#/:d"J"$'(),x];}"0123456789"
/\
\/\
/\ /
\/\
/\ \/
\
/
\/\
/ \/
\/\
\ /
\/\
/\
/\
/\ /
/
\ \/
\
/\
\ \
\/
I'm sure this can be shorter!!
Thanks @h.j.k.
Pip, 122 + 1 = 123 bytes
Uses the -n flag. Takes input via command-line argument.
l:$.(J"\/ "@^(A_TB3M"⮐䫶ヷ䓳ⴷⴥㅕ⬿⭑")@_.2<>2Ma)z:2*#ap:sXz+2RLz+2Fi,5Fj,z{c:[4-ii]//2+j(pc@0c@1):(lij)}RVp
The characters in the UTF-8 string have the following code points: 11152, 19190, 12535, 12547, 17651, 11575, 11557, 12629, 11071, 11089.
Slightly ungolfed:
t:[120022001 222022202 122012021 122012201 220012202 120212201 120212001 122022202 120012001 120012201]
l:$.({J"\/ "@^t@a.2<>2}Ma)
z:2*#a+2
p:sXzRLz
Fi,5
Fj,2*#a {
x:i//2+j
y:(4-i)//2+j
p@y@x:l@i@j
}
P RVp
The basic strategy is to find each number's constituent characters and then skew them appropriately. For example, for 8, we want this (spaces represented by dots):
/.
\\
/.
\\
/.
which will turn into this:
.
/\.
\/\.
\/
The nice feature of this strategy is that multiple pre-skewed numbers can simply be concatenated side by side.
Now, we can encode /.\\/.\\/. in base 3 as 1200120012. Then we can convert this to decimal and treat it as a UTF-8 code point.
The expression J"\/ "@^(A_TB3M"⮐䫶ヷ䓳ⴷⴥㅕ⬿⭑")@_.2<>2Ma gets the pre-skewed data by the following process:
Ma Map this lambda function to each character in input:
(A_TB3M"...") Create list of the code points of each character in UTF-8
string, converted to base 3
@_ Index into that list using the input character
.2 Concatenate 2 to the end of the base-3 value (all of the
pre-skewed number grids end in 2, i.e. space)
^ Split the number into a list of its digits
"\/ "@ Index into this string with those digits, giving a list
of slashes & spaces
J Join the list together into a string
<>2 Group string two characters at a time
Once we have concatenated these strings side-by-side using $., we then create a grid of spaces (2*n+2 square), loop through the pre-skewed grid, and replace the corresponding spaces in the post-skewed grid with the appropriate characters. To see it happening, one can modify the code to print each stage and pause for user input:

The grid is actually built upside down, because that seemed to make the math easier.
I'm sure there's better algorithms to use. But I wanted to come up with my own idea rather than copying somebody else's.
JavaScript (ES6), 191 206
Run snippet in Firefox to test.
F=m=>(
a=' \\/ /\\/\\ / /\\ \\\\ \\'.match(/.../g),
o=f='',r=' ',
[for(d of m)(
n=1e3+'¯B\x91ÿ$ê\x86A\x87ë'.charCodeAt(d)+'', // here there are 3 valid characters tha the evil stackoverflow editor just erase off, so I had to put them as hex escape
o='\n'+f+' '+a[n[2]]+'\n'+r+a[n[1]]+o,
r=f+a[n[3]],
f+=' ')],
r+o
)
//TEST
go=_=>O.innerHTML =(v=I.value)+'\n'+F(v)
go()
<input id=I value='0123456789'><button onclick='go()'>-></button>
<pre id=O></pre>
Perl, 270 bytes
I really shouldn't have wasted my time on this.
$e="\\";$g=" ";$_=reverse<>;$l=length;push@a,(119,18,107,91,30,93,125,19,127,95)[$1]while/(.)/g;for($i=0;$i<=$l;$i++){$j=2*($l-$i);$b=$a[$i];$c=$i&&$a[$i-1];print" "x$j,$b&1?"/":$g,$b&2?$e:$g,$g,$c&32?$e:$g,$c&64?"/":$g,"
"," "x$j,$b&4?$e:$g,$b&8?"/":$g,$b&16?$e:$g,"
"}
C#, 360 355 331 bytes
Hi there, first attempt at code-golf. Hope this doesn't score too badly for a C#-entry.
string p(string n){var l=new string[n.Length*2+1];var i=l.Length-1;for(;i>0;){var x=@"/\\ \\/ \ \ /\ / \//\ /\ / \\/\ / \/\ // \/\\//\ \ /\\/\\//\\/\ /".Substring((n[0]-48)*7,7);for(var j=i-3;j>=0;){l[j--]+=" ";}l[i--]+=" "+x[5]+x[6];l[i--]+=""+x[2]+x[3]+x[4];l[i]+=""+x[0]+x[1];n=n.Remove(0, 1);}return string.Join("\n",l);}
Usage: p("159114"); will return
\
\/\
\
\
\
\
/\
\/\
/ /
\/\
\ /
\
Expanded:
string p(string n)
{
var l = new string[n.Length * 2 + 1];
var i = l.Length - 1;
for (; i > 0; )
{
var x = @"/\\ \\/ \ \ /\ / \//\ /\ / \\/\ / \/\ // \/\\//\ \ /\\/\\//\\/\ /".Substring((n[0] - 48) * 7, 7);
for (var j = i - 3; j >= 0; )
{
l[j--] += " ";
}
l[i--] += " " + x[5] + x[6];
l[i--] += "" + x[2] + x[3] + x[4];
l[i] += "" + x[0] + x[1];
n = n.Remove(0, 1);
}
return string.Join("\n", l);
}
JavaScript, 192 178 167 162 bytes
f=x=>{n=b="\n";for(k in x)for(i=0;i<8;)b+=("î\xA0Öô¸|~àþü".charCodeAt(x[k])>>i++&1?i%2?"/":"\\":" ")+(i%3?"":n+" ".repeat(k));return b.split(n).reverse().join(n)}
Usage: f("1337"); will return
/\
\
/\
/\
/\ /
/\
\ /
\
It uses features of ES6 and may have some implementation dependent behavior due to omission of semicolons and parentheses and such, but it works in Firefox.
Expanded:
f=x=>
{
n = b = "\n";
for (k in x)
for (i=0; i<8;)
b += ("î\xA0Öô¸|~àþü".charCodeAt(x[k]) >> i++ & 1? i%2? "/" : "\\" : " ") + (i%3? "" : n+" ".repeat(k));
return b.split(n).reverse().join(n)
}
Explanation:
l is an array containing 10 single byte characters which correspond to the shape of each digit. For example, the digit 0 is represented by the character î:
/\ 11
\ \ --> 101 --> 11 101 110 = î
\/ 011
The input characters are used as keys to the array holding their shape representing counterparts, which get read bit by bit.
python 2, 317 298 278 273.15
def f(s):
r=range;n=len(s)*2;l=[[' ']*-~n for x in r(-~n)]
for x in r(0,n,2):
for i,[d,y,c]in enumerate(zip('0112012','1021012',r'\\\\///')):l[n-2-x+int(y)][x+int(d)]=[' ',c][('%7s'%(bin(ord('}(7/jO_,\x7fo'[map(int,s)[x/2]])))[2:])[i]=='1']
for x in l:print''.join(x)
I considered 4-spaces as tabs while counting.
Uncompressed and readable:
def f(s):
r=['1111101','0101000','0110111','0101111','1101010','1001111','1011111','0101100','1111111','1101111']
''.join(map(lambda x:chr(eval('0b'+x)),r))
n=len(s)*2
l=[[' ']*(n+1) for x in xrange(n+1)]
shifts=[(0,1,'\\'),(1,0,'\\'),(1,2,'\\'),(2,1,'\\'),(0,0,'/'),(1,1,'/'),(2,2,'/')]
for x in xrange(0,n,2):
y=n-2-x
for i,[dx,dy,c] in enumerate(shifts):
l[y+dy][x+dx]=c if r[map(int,s)[x/2]][i]=='1' else ' '
return '\n'.join(''.join(x) for x in l)
Python 3, 189 183 174 bytes
s="a%sa"%input()
while s[1:]:b,a,c,d,e,f,g=[c*(ord(n)>>int(s[~(n>"Ͱ")],16)&1)or" "for c,n in zip("\/"*4,"ΟϭŅͭͱͼϻ")];S=len(s)*" ";print(S+a+b,c+d+"\n"+S+e+f+g);*s,_=s
The compression looks okay to me, but I'm having trouble coming up with a good way of ditching the seven variables...
Thankfully the spec is fairly relaxed on whitespace rules, because there's a lot of leading/trailing whitespace.
Expanded:
s="a%sa"%input()
while s[1:]:
b,a,c,d,e,f,g=[c*(ord(n)>>int(s[~(n>"Ͱ")],16)&1)or" "
for c,n in zip("\/"*4,"ΟϭŅͭͱͼϻ")]
S=len(s)*" "
print(S+a+b,c+d+"\n"+S+e+f+g)
*s,_=s
Explanation
The segment positions represented by the variables are:
ab /\
efg /\
ab cd /\ /
efg \ \
ab cd /\ \/
efg /
cd \/
Each segment is encoded by a single 2-byte Unicode character. For example, ϻ encodes g's segment like so:
bin(ord("ϻ")) = bin(1019) = "0b1111111011"
^^^^^^^^^^
9876543210
Indeed, 2 is the only digit to not use the bottom-right segment of a seven-segment display.