A Challenge...

S

someone

sorry, forgot something -- you dont have to add in the command line, just
the code will do.
 
N

Nicholas Dronen

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
 
R

Ragnar Hafstað

Nicholas Dronen said:
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
 
A

Ala Qumsieh

Ragnar said:
((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
 
A

Ala Qumsieh

Ala said:
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
 
R

Ragnar Hafstað

Ala Qumsieh said:
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
 
R

Ragnar Hafstað

Ragnar Hafstað said:
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'
 
R

Ragnar Hafstað

Ragnar Hafstað said:
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
 
J

Jay Tilton

: : > 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--'
 
M

Michele Dondi

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
 
M

Michele Dondi

-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

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top