Re: (Golf) Friday Golf: Print Formatted Sequence of Digits
by sauoq (Abbot) on Aug 14, 2003 at 19:17 UTC
|
perl -le 'print qq(@{["002".."020"]})'
Update:
perl -le'$,=$";print"002".."020"'
-sauoq
"My two cents aren't worth a dime.";
| [reply] [d/l] [select] |
|
perl -l40eprint,for'002'..'020'
Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
If I understand your problem, I can solve it! Of course, the same can be said for you.
| [reply] [d/l] |
|
$ perl -MO=Deparse -l40eprint,for'002'..'020'
BEGIN { $/ = "\n"; $\ = " "; }
print($_), 'for002' .. 16;
-e syntax OK
-sauoq
"My two cents aren't worth a dime.";
| [reply] [d/l] |
|
|
|
|
That didn't work for me. All it did was print a space. (Both 5.6.0 on AIX and 5.8.0 on Solaris.)
------ We are the carpenters and bricklayers of the Information Age. The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6 Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.
| [reply] |
|
| [reply] |
|
I was suprised no one else came up with the magic increment. I had perl -e 'print"$_ "for"002".."020"' since I didn't remember $,.
| [reply] [d/l] [select] |
Re: (Golf) Friday Golf: Print Formatted Sequence of Digits
by Aristotle (Chancellor) on Aug 14, 2003 at 19:30 UTC
|
22, you probably want to count the -l though, so 23.
# 1234567890123456789012
perl -le'$#=" %03g";print 2..20'
Why are we obscuring golf solutions?
Makeshifts last the longest. | [reply] [d/l] |
|
Nice. Except for that leading space in the output... Still, you get an upvote from me.
Update: Of course, you can fix the leading space with "$#="%03g " instead. I can't tell if the example output had a space on the end, so we'd have to let that slide, I guess. (BrowserUk's has a space on the end too, afterall.) That just leaves us with the fact that $# is deprecated. It works... for now. :-)
-sauoq
"My two cents aren't worth a dime.";
| [reply] |
Re: (Golf) Friday Golf: Print Formatted Sequence of Digits(25?)
by BrowserUk (Patriarch) on Aug 14, 2003 at 19:15 UTC
|
| [reply] |
|
I'm sure that you meant:
perl -e"printf'%03d 'x19 .$/,2..20" |
By the way, it's 26 unless you started counting with 0.
| [reply] [d/l] |
|
25
Have you miscounted? I count that as 26 between the quotes... you should really count your switches though. So, I'd give that a 30.
And I'm up two strokes with 28. :-P
(Or, we are at 28 27 and 26 25¹ repectively if we put our solutions in files to avoid problems that result from quoting on different platforms.)
And it would be 24 but I think -l is warranted! :-)
-sauoq
"My two cents aren't worth a dime.";
| [reply] |
|
Don't mind that thumping sound in the background--that's just my head slamming against the monitor, saying "Why didn't I think of that?" :)
---- I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer
Note: All code is untested, unless otherwise stated
| [reply] |
|
I'm not sure this is a correct solution. The original printed a newline at the end, your version prints a space.
Jenda
Always code as if the guy who ends up maintaining your code
will be a violent psychopath who knows where you live.
-- Rick Osborne
Edit by castaway: Closed small tag in signature
| [reply] |
Re: (Golf) Friday Golf: Print Formatted Sequence of Digits
by ajdelore (Pilgrim) on Aug 14, 2003 at 19:10 UTC
|
Well, I've never really golfed before, but I'll give it a whirl...
# original example, 58 chars
perl -e 'print join(" ", map { sprintf("%03u", $_) } (2..20)), "\n"'
# my golf score: 45 chars
perl -e 'print((map{sprintf("%03u ",$_)}(2..20)),"\n")'
</ajdelore> | [reply] [d/l] [select] |
Re: (Golf) Friday Golf: Print Formatted Sequence of Digits
by hardburn (Abbot) on Aug 14, 2003 at 19:14 UTC
|
Slight trick to get rid of the join.
perl -e '$,=" ";print map(sprintf("%03u",$_),2..20),"\n"'
|
Saves 1 char. I feel special!
---- I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer
Note: All code is untested, unless otherwise stated
| [reply] [d/l] [select] |
Re: (Golf) Friday Golf: Print Formatted Sequence of Digits
by dragonchild (Archbishop) on Aug 14, 2003 at 19:31 UTC
|
*grins* I don't know how cheating this is, but ...
echo '"000" .. "020"' | perl -ple '@f=eval;$_="@f"'
Between the quotes, I'm at 14. Woo-hoo! *laughs*
------ We are the carpenters and bricklayers of the Information Age. The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6 Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified. | [reply] [d/l] |
|
Well, if you want to go that route..
echo '@_ = "002" .. "020"; print "@_\n"' | perl -ne 'eval'
Four characters! (Reminds me of "how do you write a LISP interpreter in LISP?".)
Makeshifts last the longest. | [reply] [d/l] |
Re: (Golf) Friday Golf: Print Formatted Sequence of Digits
by Mr. Muskrat (Canon) on Aug 14, 2003 at 19:21 UTC
|
perl -le 'printf("%03u ",$_)for(2..20)' | '1234567890123456789012345678' |
| [reply] [d/l] [select] |
|
Minor improvement: all the parentheses are unnecessary.
perl -le'printf"%03u ",$_ for 2..20'
-- Mike
--
XML::Simpler does not require XML::Parser or a SAX parser.
It does require File::Slurp.
-- grantm, perldoc XML::Simpler | [reply] [d/l] |
Re: (Golf) Friday Golf: Print Formatted Sequence of Digits
by benn (Vicar) on Aug 14, 2003 at 19:51 UTC
|
perl -e 'print"0$_ "for"02".."20"'
123456789012345678901234
Ben.
| [reply] [d/l] |
Re: (Golf) Friday Golf: Print Formatted Sequence of Digits (First Attempt)
by Enlil (Parson) on Aug 14, 2003 at 19:15 UTC
|
perl -e 'printf("%03u ",$_) for 2..19;print"020\n"' |
for 40
-enlil
| [reply] |
Re: (Golf) Friday Golf: Print Formatted Sequence of Digits
by demerphq (Chancellor) on Aug 15, 2003 at 09:58 UTC
|
perl -e "$,=' ';print'002'..'020'"
perl -e '$,=" ";print"002".."020"'
I must be missing something though.. Surely this is obvious?
---
demerphq
<Elian> And I do take a kind of perverse pleasure in having an OO assembly language...
• Update:
Yep. It _was_ obvious. I'm so glad. :-) Nice one to those that remembered the value of $" (which didnt occur to me to use for some reason.)
| [reply] [d/l] [select] |
(it's thursday here) Re: (Golf) Friday Golf: Print Formatted Sequence of Digits
by particle (Vicar) on Aug 14, 2003 at 19:49 UTC
|
| [reply] |
Re: (Golf) Friday Golf: Print Formatted Sequence of Digits
by jsprat (Curate) on Aug 15, 2003 at 01:12 UTC
|
23 (24 with the -l), and doesn't print the trailing space!
$perl -le'$,=$";print"002".."020"'
002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 01
+9 020
$perl -e'print length q/$,=$";print"002".."020"/'
23
Update: I guess I was beaten to the punch. sauoq's is an update, and it wasn't there when I began writing this post (I searched the page for $" before I started writing my post).
| [reply] [d/l] |
|
| [reply] |
Re: (Golf) Friday Golf: Print Formatted Sequence of Digits
by fletcher_the_dog (Friar) on Aug 15, 2003 at 23:24 UTC
|
Total Cheater method:
First: Create module called "i.pm" that looks like this:
print join(" ", map { sprintf("%03u", $_) } (2..20)), "\n";exit 0
Then I can get it to 7 characters:
perl -Mi
| [reply] [d/l] [select] |
|
| [reply] [d/l] |
|
That is why it says "exit 0" at the end! :)
| [reply] |