A Challenge...

Discussion in 'Perl Misc' started by someone, Dec 27, 2003.

  1. someone

    someone Guest

    someone, Dec 27, 2003
    #1
    1. Advertisements

  2. Nicholas Dronen, Dec 27, 2003
    #2
    1. Advertisements

  3. someone

    someone Guest

    sorry, forgot something -- you dont have to add in the command line, just
    the code will do.
     
    someone, Dec 27, 2003
    #3
  4. someone <> wrote:
    s> sorry, forgot something -- you dont have to add in the command line, just
    s> the code will do.

    Run with perl -wl. :)

    for (reverse 0..9) {
    $x=$x?"$_$x$_":$_;
    print " " x ((20-length $x)/2), $x;
    }

    Regards,

    Nicholas

    --
    "Why shouldn't I top-post?" http://www.aglami.com/tpfaq.html
    "Meanings are another story." http://www.ifas.org/wa/glossolalia.html
     
    Nicholas Dronen, Dec 27, 2003
    #4
  5. someone

    someone Guest

    nice try... but im sorry that isn't the shortest.
     
    someone, Dec 27, 2003
    #5
  6. "Nicholas Dronen" <> wrote in message
    news:3fedc33c$0$200$...
    > someone <> wrote:
    > s> Hi all,
    > s> here is a challenge for all of ya...
    >
    > s> http://m_12321.tripod.com/c.htm
    >
    > $ perl -wle 'for (reverse 0..9) { $x=$x?"$_$x$_":$_; print " " x

    ((20-length $x)/2), $x }'

    trivial improvement (untested):
    $ perl -wle 'for (reverse 0..9) { $x=$x?"$_$x$_":$_; print " " x $_, $x }'

    gnari
     
    Ragnar Hafstað, Dec 27, 2003
    #6
  7. someone

    someone Guest

    you're getting there. :)
     
    someone, Dec 27, 2003
    #7
  8. someone

    Ala Qumsieh Guest

    Ragnar Hafstað wrote:
    > "Nicholas Dronen" <> wrote in message
    > news:3fedc33c$0$200$...
    >
    >>someone <> wrote:
    >>s> Hi all,
    >>s> here is a challenge for all of ya...
    >>
    >>s> http://m_12321.tripod.com/c.htm
    >>
    >>$ perl -wle 'for (reverse 0..9) { $x=$x?"$_$x$_":$_; print " " x

    >
    > ((20-length $x)/2), $x }'
    >
    > trivial improvement (untested):
    > $ perl -wle 'for (reverse 0..9) { $x=$x?"$_$x$_":$_; print " " x $_, $x }'


    More trivial improvements:

    perl -le map{print$"x$_,$x=$x?"$_$x$_":$_}reverse 0..9

    --Ala
     
    Ala Qumsieh, Dec 27, 2003
    #8
  9. someone

    Ala Qumsieh Guest

    Ala Qumsieh wrote:

    > perl -le map{print$"x$_,$x=$x?"$_$x$_":$_}reverse 0..9


    Hmmm .. reverse() is such a long-named function.

    perl -le map{print$"x-$_,$x=$x?-$_.$x.-$_:-$_}-9..0

    --Ala
     
    Ala Qumsieh, Dec 27, 2003
    #9
  10. "Ala Qumsieh" <> wrote in message
    news:ZOkHb.2777$...
    > Ala Qumsieh wrote:
    >
    > > perl -le map{print$"x$_,$x=$x?"$_$x$_":$_}reverse 0..9

    >
    > Hmmm .. reverse() is such a long-named function.
    >
    > perl -le map{print$"x-$_,$x=$x?-$_.$x.-$_:-$_}-9..0


    actually, this does not work as typed.
    it does with quotes:
    perl -le'map{print$"x-$_,$x=$x?-$_.$x.-$_:-$_}-9..0'

    but then this is better:
    perl -le 'print$"x-$_,$x=$x?-$_.$x.-$_:-$_ for-9..0'


    gnari
     
    Ragnar Hafstað, Dec 27, 2003
    #10
  11. "Ragnar Hafstað" <> wrote in message
    news:bsko90$jed$...
    >
    > "Ala Qumsieh" <> wrote in message
    > news:ZOkHb.2777$...
    > > Ala Qumsieh wrote:
    > >
    > > > perl -le map{print$"x$_,$x=$x?"$_$x$_":$_}reverse 0..9

    > >
    > > Hmmm .. reverse() is such a long-named function.
    > >
    > > perl -le map{print$"x-$_,$x=$x?-$_.$x.-$_:-$_}-9..0

    >
    > actually, this does not work as typed.
    > it does with quotes:
    > perl -le'map{print$"x-$_,$x=$x?-$_.$x.-$_:-$_}-9..0'
    >
    > but then this is better:
    > perl -le 'print$"x-$_,$x=$x?-$_.$x.-$_:-$_ for-9..0'


    and even better:
    perl -le'print$"x-$_,$x=$x?-$_.$x.-$_:9 for-9..0'
     
    Ragnar Hafstað, Dec 27, 2003
    #11
  12. "Ragnar Hafstað" <> wrote in message
    news:bskokk$jh8$...
    > and even better:
    > perl -le'print$"x-$_,$x=$x?-$_.$x.-$_:9 for-9..0'


    one more character:
    perl -le'print$"x-$_,$x=$x?-$_.$x.-$_:9for-9..0'

    gnari
     
    Ragnar Hafstað, Dec 27, 2003
    #12
  13. someone

    Jay Tilton Guest

    "Ragnar Hafstað" <> wrote:

    : "Nicholas Dronen" <> wrote in message
    : news:3fedc33c$0$200$...
    : > someone <> wrote:
    : > s> Hi all,
    : > s> here is a challenge for all of ya...
    : >
    : > s> http://m_12321.tripod.com/c.htm
    : >
    : > $ perl -wle 'for (reverse 0..9) { $x=$x?"$_$x$_":$_; print " " x
    : ((20-length $x)/2), $x }'
    :
    : trivial improvement (untested):
    : $ perl -wle 'for (reverse 0..9) { $x=$x?"$_$x$_":$_; print " " x $_, $x }'
    ^
    ----------^
    Warnings have no benefit in golf, and scoring usually counts the switches
    as strokes.

    Same basic idea, more improvement:

    perl -le '$;=10;print$"x$;,$x=$x?"$;$x$;":$;while$;--'

    Ditching the ?: operator in favor of s///:

    perl -le '$i=10;s/^|$/$i/g,print$"x$i,$_ while$i--'
     
    Jay Tilton, Dec 27, 2003
    #13
  14. On Sat, 27 Dec 2003 19:10:49 GMT, Ala Qumsieh <>
    wrote:

    >> perl -le map{print$"x$_,$x=$x?"$_$x$_":$_}reverse 0..9


    BTW: can you really run these without quoting? Which shell are you
    using?

    Please note that to be definite and to avoid messing with the cmd line
    I'll switch to consider scripts contained in files.

    >Hmmm .. reverse() is such a long-named function.
    >
    >perl -le map{print$"x-$_,$x=$x?-$_.$x.-$_:-$_}-9..0


    Hmmm, that's a good idea! But then you can earn one more char by
    reverting to C<for>:

    #!/usr/bin/perl -l
    print$"x-$_,$x=$x?-$_.$x.-$_:-$_ for-9..0

    And you can still save two:

    #!/usr/bin/perl -l
    print$"x-$_,$x=-$_.$x.($x&&-$_)for-9..0

    In terms of e.g. <http://terje.perlgolf.org/>, these would count as

    -l print$"x-$_,$x=$x?-$_.$x.-$_:-$_ for-9..0

    -l print$"x-$_,$x=-$_.$x.($x&&-$_)for-9..0

    and would score 44 and 42 strokes respectively. Notwithstanding my
    (limited!) experience with Perlgolf gurus popping out with solutions
    you wouldn't have ever imagined about, I doubt that one can do
    anything better than the last one...


    Michele
    --
    # This prints: Just another Perl hacker,
    seek DATA,15,0 and print q... <DATA>;
    __END__
     
    Michele Dondi, Dec 29, 2003
    #14
  15. On Mon, 29 Dec 2003 12:45:02 +0100, Michele Dondi
    <> wrote:

    > -l print$"x-$_,$x=-$_.$x.($x&&-$_)for-9..0
    >
    >and would score 44 and 42 strokes respectively. Notwithstanding my
    >(limited!) experience with Perlgolf gurus popping out with solutions
    >you wouldn't have ever imagined about, I doubt that one can do
    >anything better than the last one...


    OTOH soon after writing these words of wisdom I read that

    On Sat, 27 Dec 2003 20:07:24 -0000, "Ragnar Hafstað" <>
    wrote:

    >one more character:
    >perl -le'print$"x-$_,$x=$x?-$_.$x.-$_:9for-9..0'


    i.e.

    -l print$"x-$_,$x=$x?-$_.$x.-$_:9for-9..0

    41 strokes.D'Oh!


    Michele
    --
    # This prints: Just another Perl hacker,
    seek DATA,15,0 and print q... <DATA>;
    __END__
     
    Michele Dondi, Dec 29, 2003
    #15
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Artco News

    Parsing challenge...

    Artco News, Oct 7, 2003, in forum: Perl
    Replies:
    6
    Views:
    509
    Ara.T.Howard
    Oct 8, 2003
  2. Artco News

    Parsing challenge...

    Artco News, Oct 7, 2003, in forum: Perl
    Replies:
    2
    Views:
    445
    Ed Morton
    Oct 7, 2003
  3. .Net Newbie
    Replies:
    0
    Views:
    543
    .Net Newbie
    Jul 29, 2004
  4. Random

    XML challenge!

    Random, Nov 16, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    2,307
    Marshal Antony
    Nov 16, 2004
  5. Raj Thakkar
    Replies:
    8
    Views:
    584
    Patrick Olurotimi Ige
    Dec 9, 2004
Loading...

Share This Page