Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Golf: Seven segment display

by kikuchiyo (Hermit)
on Jan 18, 2012 at 11:52 UTC ( [id://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.

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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2025-07-08 15:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.