93
\$\begingroup\$

This question has been spreading like a virus in my office. There are quite a variety of approaches:

Print the following:

        1
       121
      12321
     1234321
    123454321
   12345654321
  1234567654321
 123456787654321
12345678987654321
 123456787654321
  1234567654321
   12345654321
    123454321
     1234321
      12321
       121
        1

Answers are scored in characters with fewer characters being better.

\$\endgroup\$
8
  • 5
    \$\begingroup\$ What is the winning criterion ? And is this a challenge or a golf ? \$\endgroup\$
    – Paul R
    Oct 12, 2012 at 15:33
  • 25
    \$\begingroup\$ I read "kolmogorov-complexity" as "code-golf". \$\endgroup\$
    – DavidC
    Oct 12, 2012 at 16:44
  • 2
    \$\begingroup\$ @DavidCarraher "kolmogorov-complexity" was edited in after the question was asked. The original questioner has not specified the winning criteria yet. \$\endgroup\$
    – Gareth
    Oct 12, 2012 at 20:56
  • 1
    \$\begingroup\$ @Gareth My comment was made after the "kolmogorov-complexity" tag was added but before the "code-golf" tag was added. At that time people were still be asking whether it was a code-golf question. \$\endgroup\$
    – DavidC
    Oct 12, 2012 at 22:00
  • 3
    \$\begingroup\$ perlmonks.com/?node_id=891559 has perl solutions. \$\endgroup\$
    – b_jonas
    Oct 20, 2012 at 19:51

126 Answers 126

1 2 3 4
5
0
\$\begingroup\$

Javascript, 103 96 93 89 88 87

f=(i=[...Array(17)].map((x,i)=>i<8?i:i-2*(i-8)))=>i.map(x=>i.map(y=>x+y>7?(x+y-7):' ').join``).join`\n`

I haven't looked at the other answers up until this point, and just started out with this spreadsheet. Just saw that this answer isn't too bad of a golf compared to the other Javascript answers.

I tried to be clever coming up with something that generates a particular array (i) that I use for the x and y-axis. But it turns out, spelling out that array is shorter.

f=(i=[0,1,2,3,4,5,6,7,8,7,6,5,4,3,2,1,0])=>i.map(x=>i.map(y=>x+y>7?(x+y-7):' ').join``).join`\n`

Update. Spelling out the array differently, but I'm sure that I can golf this further

f=(i=[...'01234567876543210'])=>i.map(x=>i.map(y=>1*x+1*y>7?(1*x+1*y-7):' ').join``).join`\n`

Last update for tonight:

f=(i=[...'01234567876543210'])=>i.map(x=>i.map(y=>+x+1*y>7?+x+1*y-7:' ').join``).join`\n`

Getting rid of one more byte:

f=(i=[...'0'+10*11111111**2])=>i.map(x=>i.map(y=>+x+1*y>7?+x+1*y-7:' ').join``).join`\n`

And one more:

f=(i=[0,...11111111**2+'0'])=>i.map(x=>i.map(y=>+x+1*y>7?+x+1*y-7:' ').join``).join`\n`
\$\endgroup\$
0
\$\begingroup\$

Duocentehexaquinquagesimal, 58 bytes

(Úv×H.F°ǝ=ÉÈNŸ=~ʒ€
₁˨Dûl`N₆rÇX³ΣJd‘×∞*Åv€rØ(0՜֤ΔNā—St€ô

Try it online!

\$\endgroup\$
0
\$\begingroup\$

FreeBASIC, 100 characters

Dim As Byte i,j,k
for i=-8 to 8 : for j=-8 to 9
k=abs(i)+abs(j)
if k > 8 then print " "; : else : ? chr(57-k);
next
? 
next 
Sleep
End
\$\endgroup\$
1
0
\$\begingroup\$

Perl 5, 33 bytes

say$"x abs.(1x(9-abs))**2for-8..8

Try it online!

Using 11 * 11 = 121, 111 * 111 = 12321, 1111 * 1111 = 1234321, ...

3 bytes saved thanks to naffetS.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 33: say$"x abs.(1x(9-abs))**2for-8..8 \$\endgroup\$
    – naffetS
    Mar 15 at 2:53
0
\$\begingroup\$

Go, 239 196 187 185 bytes

import(."fmt";."strings")
func g(i int){P:=Print;P(Repeat(" ",(9-i)))
for j:=1;j<=i;j++{P(j)}
for k:=i-1;k>0;k--{P(k)}
P("\n")}
func f(){
for i:=1;i<10;i++{g(i)}
for i:=8;i>0;i--{g(i)}}

Attempt This Online!

My first attempt at golfing in Go. This can definitely be shortened, but I don't know how.

Commented

import (."fmt"; ."strings")        // Import modules for later
func g(i int) {                    // Helper function g taking an int i
P := Print                         //  Useful function for later
P(Repeat(" ", (9 - i)))            //  Repeat a space 9-i times and print
for j := 1; j <= i; j++ { P(j) }   //  Print each j for j in [1 .. i]
for k := i-1; k > 0; k-- { P(k) }  //  Print each k for k in [i .. 1]
P("\n") }                          //  Print a newline
func f(){                          // Function boilerplate
for i := 1; i < 10; i++ { g(i) }   //  Call g for each i in [1 .. 9]
for i := 8; i > 0; i-- { g(i) }}   //  Call g for each i in [8 .. 1]
\$\endgroup\$
0
\$\begingroup\$

Java 8 (OpenJDK 8), 157 161 bytes

-4 bytes thanks to @ceilingcat

Tried to use only 1 for instead of 2 for two-dimensional printing, but couldn't beat the best Java answers.

interface M{static void main(String[]b){for(int r,s,a=0;a<289;System.out.print((r<1?s<1?" ":"":r)+(s>7?"\n":"")))r=9-Math.abs(s=a%17-8)-Math.abs(8-a++/17);}}

Try it online!

\$\endgroup\$
0
1 2 3 4
5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.