Seven Slash Display
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.
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);
}
}
}
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.