Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Golf: Seven segment display

by kikuchiyo (Monk)
on Jan 18, 2012 at 11:52 UTC ( #948530=perlquestion: print w/ replies, xml ) Need Help??
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.




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

My best effort so far:

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

Comment on Golf: Seven segment display
Select or Download Code
Replies are listed 'Best First'.
Re: Golf: Seven segment display
by Anonymous Monk on Jan 18, 2012 at 12:25 UTC
Re: Golf: Seven segment display
by kikuchiyo (Monk) 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");
      _ _ _ _ _ |_ |_|| || ||_| _| ||_||_| |

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://948530]
Approved by marto
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2015-11-27 14:01 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (729 votes), past polls