Re: (golf) Triangle of numbers
by japhy (Canon) on Oct 26, 2001 at 04:21 UTC
|
Here's my program, assuming $a holds the number. It's 68 characters.
$_=@;=1..$a++*$a/2;printf" %@{[y///c]}d"x++$;.$/,splice@;,0,$;while@;
_____________________________________________________
Jeff[japhy]Pinyan:
Perl,
regex,
and perl
hacker.
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??; | [reply] [d/l] |
|
|
Thats some nice obfued golfing! I took a few minutes to understand and translate it:
$_ = ($a*($a+1))/2; # calculate last number in triangle
my @arr = 1..$_; # list of all numbers in triangle
my $i;
while(@arr) {
$i++;
my $format = ' %' . y///c . 'd'; # y///c is a short version of lengt
+h($_)
# originally in "@{[2 + 2]}" form -
+- which
# creates, dereferences and stringi
+fies
# a one element array ref
my @row = splice(@arr,0,$i); # take $i elements off the front of
+ @arr
printf $format x $i . $/, @row; # print the whole row, instead of u
+sing an
# explicit loop like the rest of us
}
-Blake
| [reply] [d/l] |
Re: (golf) Triangle of numbers
by blakem (Monsignor) on Oct 26, 2001 at 04:02 UTC
|
Heres my initial entry at 56:
It take the 'at least one space between numbers' condition
to its logical conclusion.
sub g {
# 1 2 3 4 5
#2345678901234567890123456789012345678901234567890123456
$^=pop;for(1..$^){printf"%$^d",++$,for(0..$@++);print$/}
}
=g(3) OUTPUT
1
2 3
4 5 6
=g(5) OUTPUT
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
=g(7) OUTPUT
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
Update: with the param already in $a, I can cut it down to an even 50....
$a = 5;
# 1 2 3 4 5
#2345678901234567890123456789012345678901234567890
for(1..$a){printf"%$a.d",++$;for(0..$@++);print$/}
Update2: Jumping through the new rules clarification (and borrowing the cleverly ordered calculation) I'm at 70....
# 1 2 3 4 5 6 7
#234567890123456789012345678901234567890123456789012345678901234567890
$b=length$a++*$a/2;for(2..$a){printf" %$b.d",++$;for(0..$@++);print$/}
Final update before I have to go
63 chars... follows the letter (if not the spirit) of the new rules. ;-)
# 1 2 3 4 5 6
#23456789012345678901234567890123456789012345678901234567890123
map{map{$a=0if$_>$a-2;printf" %$a.d",++$;}0..$@++;print$/}1..$a
-Blake
| [reply] [d/l] [select] |
Re: (golf) Triangle of numbers
by runrig (Abbot) on Oct 26, 2001 at 04:07 UTC
|
Conditions state we assume the # of rows is in $a, so not
counting the initial assignment, 41 characters: $a=5;
print map{@_=$j+1..($j+=++$i);"@_\n"}1..$a
#Update 2 chars better (39)
print map{"@{[$j+1..($j+=++$i)]}\n"}1..$a
Update: blakem pointed out that mine doesn't align the numbers. Darn.
Another update: I don't see any reason why any one of the following does not work, there seems to be a bug with the deprecated '$#' variable. In fact, the one with 's' segfaults on 5.6.1 (though if it worked, it would left justify the numbers). It woulda been 64 characters (if someone submits a patch to the perl core, they can have the credit for this :)$#="%@{[2*length $a]}d";print map"@{[$j+1..($j+=++$i)]}\n",1..$a
$#="%@{[2*length $a]}u";print map"@{[$j+1..($j+=++$i)]}\n",1..$a
$#="%@{[2*length $a]}s";print map"@{[$j+1..($j+=++$i)]}\n",1..$a
Last Update: Thanks to help from master caddy Albannach (for figuring out that 'f' works), 60 characters:$#=" %@{[2*length$a]}.f";print map{$j+1..($j+=++$i),$/}1..$a
Really last update: Following the letter AND spirit of that last 'updated' condition, at 67 characters:$#=" %@{[length int$a*$a/2]}.f";print map{$j+1..($j+=++$i),$/}1..$a
# or
$#=" %@{[length$a++*$a/2]}.f";print map{$j+1..($j+=++$i),$/}1..$a-1
# or
$#=" %@{[length$a*($a+1)/2]}.f";print map{$j+1..($j+=++$i),$/}1..$a
Tired of updating :) But more caddy help, this time from blakem(65 characters):$#=" %@{[length$a++*$a/2]}g";map{print$j+1..($j+=++$i),$/}1..$a-1
| [reply] [d/l] [select] |
|
|
Based on a few ideas from the other posts I came up with the following improvement of your 65 character version, squeezing it to 62. (Now all I have to go and read the docs and figure out how it works. :-) The one I was working on (and understand) was in the line of hopes latest post which I reduced down to 64.
1 2 3 4 5 6
12345678901234567890123456789012345678901234567890123456789012345
#runrig improvement
$#=" %@{[length$a++*$a/2]}g";print$j+1..($j+=++$i),$/for 2..$a
#blakem/hopes/jynx improvement
$;=length$a++*$a/2;map{printf" %$;d",++$,for 2..$_;print$/}2..$a
Ahhh, Latecomers Advantage. :-)
Yves / DeMerphq
--
Have you registered your Name Space? | [reply] [d/l] |
Re: (golf) Triangle of numbers
by hopes (Friar) on Oct 26, 2001 at 04:40 UTC
|
Update 3: seeing japhys and blakes code, I reduce my code to 70
$n=1+length$a++*$a/2;map{map{printf"%${n}d",++$k}1..$_;print$/}1..$a-1
| [reply] [d/l] |
|
|
I can trim two chars from that.... to tie japhy at 68
# 1 2 3 4 5 6
#2345678901234567890123456789012345678901234567890123456789012345678
$n=length$a++*$a/2;map{map{printf" %$n.d",++$k}1..$_;print$/}1..$a-1
-Blake
| [reply] [d/l] |
|
|
I can beat 2 characters.
How about this (66 chars):
$n=length$a++*$a/2;map{map{printf" %$n.d",++$k}2..$_;print$/}0..$a
There are two "\n", I know, but writes exactly what I want... :-)
Hopes
| [reply] [d/l] |
Re: (golf) Triangle of numbers
by jynx (Priest) on Oct 27, 2001 at 00:20 UTC
|
here's a couple/three things,
Firstly, you can drop the extra $/'s being printed on yours by changing the 0..$a to 2..$a as shown in #1. The second thing i worked on was a one-liner version, which is #2. It weighs in at 71 characters. However, if i'm allowed to disregard numbers over 1000 (which would take more than 45 rows to get) i can drop that by three characters to 68 as shown in #3.
# 1
$n=length$a++*$a/2;map{map{printf" %$n.d",++$k}2..$_;print$/}2..$a
# 2
print+(map$"x(length(($a+1)*$a/2)+1-length++$l).$l,2..$_),$/for 2..$a+
+1
# 3
print+(map$"x(($a>13?4:$a>3?3:2)-length++$l).$l,2..$_),$/for 2..$a+1
Anyway, thanks for the fun,
jynx | [reply] [d/l] |
Re: (golf) Triangle of numbers
by Anarion (Hermit) on Oct 29, 2001 at 17:12 UTC
|
Perhaps its to late, but i want to post my try, i did it before looking at others code:
for(1..$a){printf"%${\length($a*$a/2)}d",$-++;$_==++$?||redo;print$/;$
+?=0}
$anarion=\$anarion;
s==q^QBY_^=,$_^=$[x7,print
| [reply] [d/l] |