http://www.perlmonks.org?node_id=948530

kikuchiyo has asked for the wisdom of the Perl Monks concerning the following question:

The problem:

Write a function -- with as few characters as possible -- that, given a positive integer, prints it in a seven segment display style, similar to that found on calculators.

Input:

w(1234567890);

Output:

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

My best effort so far:

sub w{for\$;(0..2){say map{substr' _ _|_ | |_|',('030030000054116224 +666421416461'=~/./g)[\$_+\$;*10]*2,3}split//,\$_[0]}}

Replies are listed 'Best First'.
Re: Golf: Seven segment display
by Anonymous Monk on Jan 18, 2012 at 12:25 UTC
http://codegolf.stackexchange.com/questions/997/render-digital-clock-style-numbers
Re: Golf: Seven segment display
by kikuchiyo (Hermit) on Jan 18, 2012 at 12:38 UTC

Better approach:

sub w{for\$;(-2..0){say map{substr' _ _|_ |_| |',2*ord(('u\$SR,Z]dmj' +=~/./g)[\$_])>>-3*\$;&14,3}split//,\$_[0]}}

Applying the ...=~/./g trick (which you already used once inside the map) again:

sub w{for\$;(-2..0){say map{substr'   _ _|_  |_| |',2*ord(('u\$SR,Z]dmj'=~/./g)[\$_])>>-3*\$;&14,3}\$_[0]=~/./g}}

I think my idea is a "cul-de-sac" but this is a little bit shorter (7 char.).
sub w{for\$;(1005,924745596,341831533){say map{\$;>>\$_&1?/../?'|':_:\$"}m +ap{\$_+20,\$_,\$_+10}\$_[0]=~/./g}}
-- edit: Some explanations about 1005,924745596,341831533
Re: Golf: Seven segment display
by jffry (Hermit) on Jan 23, 2012 at 23:22 UTC

The only problem is that this is not "9"

_ |_| _|

This is:

_ |_| |

Otherwise, the old trick of "59009" upside down doesn't work anymore.

sub w{for\$;(1005,924745596,341831021){say map{\$;>>\$_&1?/../?'|':_:\$"}m +ap{\$_+20,\$_,\$_+10}\$_[0]=~/./g}} w("59009");
output:
_ _ _ _ _ |_ |_|| || ||_| _| ||_||_| |

/me flips laptop upside down, laughs like Beavis & Butthead.