Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Golf for unique digits

by Cody Fendant (Pilgrim)
on Jan 09, 2013 at 11:24 UTC ( #1012437=perlquestion: print w/ replies, xml ) Need Help??
Cody Fendant has asked for the wisdom of the Perl Monks concerning the following question:

Inspired by the fact that 2013 is the first year since 1987 which has four unique digits, what's your best code to determine the number of unique digits in a number?

Here's what I ended up with, skipping the %unique method:

$y = 2013; ($y = join('',sort(split(//,$y)))) =~ tr/0-9//s; print "unique digits: " . length($y) . $/;

I'm sure there's something smarter.

Comment on Golf for unique digits
Select or Download Code
Re: Golf for unique digits
by choroba (Abbot) on Jan 09, 2013 at 11:31 UTC
    perl -E'$y=2013;$y=~/$_/&&$c++for 0..9;say$c'
    Update: and -> &&. Thanks space_monk.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      Can you save a character by using && instead of and?

      A Monk aims to give answers to those who have none, and to learn from those who know more.
        Yup, just answered my own question and got a birdie
        perl -E '$y=2013;$y=~/$_/&&$c++for 0..9;say$c'
        A Monk aims to give answers to those who have none, and to learn from those who know more.

        perl -E'$y=2013;$c+=$y=~$_ for 0..9;say$c' perl -E'$y=2013;say~~map$y=~$_,0..9'

Re: Golf for unique digits
by BrowserUk (Pope) on Jan 09, 2013 at 11:40 UTC

    C:\test>perl -nlE"s[(.)(?=.*?\1)][]g;say length;" 1987 4 2013 4 2113 3 122398723456321467891356178653534 9 1111111111111111 1 121212121212121212121212 2

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

       .*? can be  .* saving one whole character!

        And s[][] -> s/// for another :) Habits!


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
      You can drop the final semicolon, too. How comes there are not two of them? :-)
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

        tye++ ... but I'm stuck to understand how it works?

        According to perlop:

        Searches a string for a pattern, and if found, replaces that pattern with the replacement text and returns the number of substitutions made.

        How does the number of substitutions made equate to the number of characters left after the substitutions?


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        This seems to work just as well?

        perl -nlE"say s/(.)(?!.*\1)//g"

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Golf for unique digits
by LanX (Canon) on Jan 09, 2013 at 11:41 UTC
    > perl -E'$_=2013;@h{split//}=();say 0+keys%h' 4

    UPDATE: please ignore, breaks the rulez

    Cheers Rolf

      Isn't this the %uniq method?
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        it is!

        I oversaw the skipping the %unique method part... sorry! =)

        Cheers Rolf

      as a compensation a new approach: =)

      > perl -ple'$_=join"",sort split//;y///cs;$_=length' 1987 4 1988 3 2012 3 2013 4

      EDIT: shortened the say away...

      Cheers Rolf

Re: Golf for unique digits
by space_monk (Chaplain) on Jan 09, 2013 at 11:47 UTC
    choroba's method with ampersands and removing variable
    perl -E'2213=~/$_/&&$c++for 0..9;say$c'
    A Monk aims to give answers to those who have none, and to learn from those who know more.
      Even shorter: perl -E'say~~grep+2013=~$_,0..9'
Re: Golf for unique digits
by trizen (Friar) on Jan 09, 2013 at 13:03 UTC
    perl -E'$_="2013";/(.)(?=.*\1)(?{++$%})(?!)/||say+y+++c-$%'
Re: Golf for unique digits
by davido (Archbishop) on Jan 10, 2013 at 09:34 UTC

    Hmm, this doesn't beat tye's, but matches it, with a different technique. I wish I could find a way to shave a stroke using this method, but I think I've reached the dead end.

    perl -lnE'@h{/./g}--;say~~keys%h'

    And this adaptation of tye's shaves a stroke:

    perl -lnE'say~~s/(.)(?!.*\1)//g'

    Dave

      This saves 2 strokes over yours:

      perl -plE"@h{/./g}++;$_=keys%h"

      And this saves 2 more over your version of tye's:

      perl -lpE'$_=s/(.)(?!.*\1)//g'

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1012437]
Approved by Ratazong
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (9)
As of 2014-08-21 20:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (143 votes), past polls