views:

1076

answers:

8

What's the shortest Perl one-liner that print out the first 9 powers of a hard-coded 2 digit decimal (say, for example, .37), each on its own line?

The output would look something like:

1
0.37
0.1369
[etc.]

Official Perl golf rules:

  1. Smallest number of (key)strokes wins
  2. Your stroke count includes the command line
A: 
perl -e "for(my $i = 1; $i < 10; $i++){ print((.37**$i). \"\n\"); }"

Just a quick entry. :)

Fixed to line break!

Abyss Knight
I think you forgot the newline - you probably need something like this:perl -e "for(my $i = 0; $i < 10; $i++){ print(.37**$i, \"\n\"); }"
Jason Sundram
Great minds think alike. Was just fixing it, but I think toolkit might just have me!
Abyss Knight
perl -e 'print .37**$_ ."\n" foreach (1..10);' # a slightly shorter version of what Abyss is doing.
J.J.
If you don't want "\n", you can use $/ instead. It holds the input record separator, defaults to "\n" and is two characters shorter.
moritz
+1  A: 
print join("\n", map { 0.37**$_ } (0..9));
toolkit
+15  A: 

With perl 5.10.0 and above:

perl -E'say 0.37**$_ for 0..8'

With older perls you don't have say and -E, but this works:

perl -le'print 0.37**$_ for 0..8'

Update: the first solution is made of 30 key strokes. Removing the first 0 gives 29. Another space can be saved, so my final solution is this with 28 strokes:

perl -E'say.37**$_ for 0..8'
moritz
Dammit, that was exactly what I was going to post (though shouldn't that 9 be an 8?)
Leon Timmermans
Yes, corrected. (Leon, you don't happen to frequent perlmonks as well? if yes, under which nick?)
moritz
No, I don't frequent perlmonks (though have an account there using this full name), why?
Leon Timmermans
Your answers resemble those that you can find on perlmonks, so I was curios ;-)
moritz
Cut the space and 0, and you can cut 2 strokes.
Axeman
I'm more a comp.lang.perl.misc user actually
Leon Timmermans
+2  A: 
perl -e 'print .37**$_,"\n" for 0..9'

If you add -l to options you can skip the ,"\n" part

kixx
+1  A: 
print.37**$_.$/for 0..8

23 strokes if you chop the program before submitting. :-P

Chris Jester-Young
+9 for perl -e'..'. Remeber, "2 Your stroke count includes the command line"
moritz
I thought that was ambiguous, actually. Tournament rules sometimes allow just the program, but add in the length of any command line switches plus a space.
ysth
+6  A: 
perl -le'map{print.37**$_}0..8'

31 characters - I don't have 5.10 to try out the obvious improvement using "say" but this is 28:

perl -E'map{say.37**$_}0..8'
1800 INFORMATION
They both work, I just tested them.
Brad Gilbert
+4  A: 
seq 9|perl -nE'say.37**$_'

26 - Yes, that's cheating. (And yes, I'm doing powers from 1 to 9. 0 to 8 is just silly.)

ysth
seq 9|perl -pE'$_=.37**$_' is also 26
1800 INFORMATION
seq 9|perl -pe's/./.37**$_/e' not as short, but a bit obfuscated and doesn't need a new perl.
moritz
+3  A: 

Just for fun in Perl 6:

  1. 28 characters:

    perl6 -e'.say for .37»**»^9'
    
  2. 27 characters:

    perl6 -e'say .37**$_ for^9'
    

(At least based on current whitespace rules.)

Aristotle Pagaltzis