Written by

Senior Cloud Architect at InterSystems
MOD
Discussion Eduard Lebedyuk · Jan 10, 2021

Code Golf: Diamonds

To start the year let's have a round of CodeGolf!

You know the drill. Shortest solution wins.

Print a size ascending range of Diamonds using the numbers 1 to 9, ranging from size 1 to size N, each diamond separated by a blank line.

A size 1 diamond should look like this, a single centered 1:

         1

With the size N=9 diamond looking like this:

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

Bonus Level: For N>9 you can either reuse 1-9 or supply arbitrary characters.

Comments

Julius Kavay · Jan 10, 2021

Maybe there are shorter solutions, but somebody must start the game... (I hope, class- and method name does not count)

Class DC.CodeGolf Extends %RegisteredObject
{ 
/// Diamonds from 1 thru 9 (max)
ClassMethod Diamond9(n)
{
  f n=1:1:n f i=1:1:n-1,n,n-1:-1:1 {s a=$e(1234567890,1,i-1) w ?n-i,a,i,$re(a),!} w !
} 

/// Diamonds from 1 thru 16 (max)
ClassMethod Diamond16(n)
{
   f n=1:1:n f i=1:1:n-1,n,n-1:-1:1 {s a=$e("1234567890abcdef",1,i) w ?n-i,a,$re($e(a,1,*-1)),!} w !
} 

}
0
Sergei Shutov  Jan 10, 2021 to Julius Kavay

I shrunk your code a bit, it's 72 characters now:

f n=1:1:n f i=1:1:n,n-1:-1:1,"" s a=$e(112345678,2,i) w !?n-i,a,i,$re(a)
0
Robert Cemper  Jan 11, 2021 to Sergei Shutov

@Sergei Shutov   $e(112345678) prevents you to pass N>9

           1
          121
         12321
        1234321
       123454321
      12345654321
     1234567654321
    123456787654321
   12345678987654321
  123456781087654321
 123456781187654321
123456781287654321
 123456781187654321
  123456781087654321
   12345678987654321
    123456787654321
     1234567654321
      12345654321
       123454321
        1234321
         12321
          121
           1
 


@Julius Kavay    similar issue, but limit is 16

My interpretation was N>9  is any number >9

0
Sergei Shutov  Jan 11, 2021 to Robert Cemper

Yeah I thought N>9 is "bonus level" - it will definitely require more characters. Original answer (top one) was only working for N<=9 anyway

0
Julius Kavay  Jan 11, 2021 to Robert Cemper

OK, you want it short and endless? You can get it!

Diamond() works from 0 thru 10 using 63 chars

Infinite() works from 0 thru Cache's maxint and has 66 chars of source code.

ClassMethod Diamond(n)
{
   f n=1:1:n w ! f j=1:1:n,n-1:-1:1 w !?n-j f i=1:1:2*j-1 w i#-j+j
}

ClassMethod Infinite(n)
{
   f n=1:1:n w ! f j=1:1:n,n-1:-1:1 w !?n-j f i=1:1:2*j-1 w i#-j+j#10
}
 

Some output

USER>

USER>d ##class(DC.CodeGolf).Diamond(0)

USER>d ##class(DC.CodeGolf).Diamond(3)

1

 1
121
 1

  1
 121
12312
 121
  1
USER>d ##class(DC.CodeGolf).Infinite(13)

1

 1
121
 1

  1
 121
12312
 121
  1

   1
  121
 12312
1234123
 12312
  121
   1

    1
   121
  12312
 1234123
123451234
 1234123
  12312
   121
    1

     1
    121
   12312
  1234123
 123451234
12345612345
 123451234
  1234123
   12312
    121
     1

      1
     121
    12312
   1234123
  123451234
 12345612345
1234567123456
 12345612345
  123451234
   1234123
    12312
     121
      1

       1
      121
     12312
    1234123
   123451234
  12345612345
 1234567123456
123456781234567
 1234567123456
  12345612345
   123451234
    1234123
     12312
      121
       1

        1
       121
      12312
     1234123
    123451234
   12345612345
  1234567123456
 123456781234567
12345678912345678
 123456781234567
  1234567123456
   12345612345
    123451234
     1234123
      12312
       121
        1

         1
        121
       12312
      1234123
     123451234
    12345612345
   1234567123456
  123456781234567
 12345678912345678
1234567890123456789
 12345678912345678
  123456781234567
   1234567123456
    12345612345
     123451234
      1234123
       12312
        121
         1

          1
         121
        12312
       1234123
      123451234
     12345612345
    1234567123456
   123456781234567
  12345678912345678
 1234567890123456789
123456789011234567890
 1234567890123456789
  12345678912345678
   123456781234567
    1234567123456
     12345612345
      123451234
       1234123
        12312
         121
          1

           1
          121
         12312
        1234123
       123451234
      12345612345
     1234567123456
    123456781234567
   12345678912345678
  1234567890123456789
 123456789011234567890
12345678901212345678901
 123456789011234567890
  1234567890123456789
   12345678912345678
    123456781234567
     1234567123456
      12345612345
       123451234
        1234123
         12312
          121
           1

            1
           121
          12312
         1234123
        123451234
       12345612345
      1234567123456
     123456781234567
    12345678912345678
   1234567890123456789
  123456789011234567890
 12345678901212345678901
1234567890123123456789012
 12345678901212345678901
  123456789011234567890
   1234567890123456789
    12345678912345678
     123456781234567
      1234567123456
       12345612345
        123451234
         1234123
          12312
           121
            1
USER>
0
Julius Kavay  Jan 11, 2021 to Julius Kavay

I see right now,  cut-and-paste without looking-and-checking isn't good! Sorry, for some stupid reason, I copied the wrong lines. The correct ones are:

ClassMethod Diamond(n)
{
   f n=1:1:n w ! f j=1:1:n,n-1:-1:1 w !?n-j f i=1:1:j,j-1:-1:1 w i
}

ClassMethod Infinite(n)
{
   f n=1:1:n w ! f j=1:1:n,n-1:-1:1 w !?n-j f i=1:1:j,j-1:-1:1 w i#10
}

Also, Dimond() works from 0 thru 9 (and not thru 10). The line lengths (with 63 and 66 chars) were correct. Finally, the correct output:

do ##class(DC.CodeGolf).Diamond(3)

1

 1
121
 1

  1
 121
12321
 121
  1
0
Dmitry Maslennikov · Jan 10, 2021
d(s) d:s>1 d(s-1) w ! f i=1:1:s,s-1:-1:1 s j=i,a=j#10 f k=j-1:-1:1 {s a=k#10_a_(k#10)} w ?30-i,a,!
0
Sergei Shutov  Jan 11, 2021 to Dmitry Maslennikov

Shrunk your code a bit, 71 chars:

f s=1:1:s f i=1:1:s,s-1:-1:1,"" s a=i f k=a-1:-1:1{s a=k_a_k} w !?s-i,a
0
Robert Cemper  Jan 11, 2021 to Sergei Shutov

for on ""  is great,  but just <=9

0
Sergei Shutov  Jan 11, 2021 to Robert Cemper

Yeah I think we can shred a couple of characters using this technique from your code as well - it will become 71 characters, best so far:

f i=1:1:N f j="",1:1:i,i-1:-1:1 w !?N-j f l=1:1:j{w l} f r=l-1:-1:1 w r
0
Robert Cemper · Jan 10, 2021

UTC: 2021-01-10 21:39:34

N<=9 :   73 chr

f i=1:1:N w ! f j=1:1:i,i-1:-1:1 w !?N-j f l=1:1:j {w l} f r=l-1:-1:1 w r

N>9 : 78 chr

f i=1:1:N w ! f j=1:1:i,i-1:-1:1 w !?N-j f l=1:1:j {w l#10} f r=l-1:-1:1 w r#10
 

eg: N=14

 
             1

             1
            121
             1

             1
            121
           12321
            121
             1

             1
            121
           12321
          1234321
           12321
            121
             1

             1
            121
           12321
          1234321
         123454321
          1234321
           12321
            121
             1

             1
            121
           12321
          1234321
         123454321
        12345654321
         123454321
          1234321
           12321
            121
             1

             1
            121
           12321
          1234321
         123454321
        12345654321
       1234567654321
        12345654321
         123454321
          1234321
           12321
            121
             1

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

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

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

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

             1
            121
           12321
          1234321
         123454321
        12345654321
       1234567654321
      123456787654321
     12345678987654321
    1234567890987654321
   123456789010987654321
  12345678901210987654321
   123456789010987654321
    1234567890987654321
     12345678987654321
      123456787654321
       1234567654321
        12345654321
         123454321
          1234321
           12321
            121
             1

             1
            121
           12321
          1234321
         123454321
        12345654321
       1234567654321
      123456787654321
     12345678987654321
    1234567890987654321
   123456789010987654321
  12345678901210987654321
 1234567890123210987654321
  12345678901210987654321
   123456789010987654321
    1234567890987654321
     12345678987654321
      123456787654321
       1234567654321
        12345654321
         123454321
          1234321
           12321
            121
             1

             1
            121
           12321
          1234321
         123454321
        12345654321
       1234567654321
      123456787654321
     12345678987654321
    1234567890987654321
   123456789010987654321
  12345678901210987654321
 1234567890123210987654321
123456789012343210987654321
 1234567890123210987654321
  12345678901210987654321
   123456789010987654321
    1234567890987654321
     12345678987654321
      123456787654321
       1234567654321
        12345654321
         123454321
          1234321
           12321
            121
             1
0
Dmitry Maslennikov  Jan 11, 2021 to Robert Cemper

even less

f i=1:1:N w ! f j=1:1:i,i-1:-1:1 w !?N-j f l=1:1:j,l-1:-1:1 w l#10
0
Robert Cemper  Jan 11, 2021 to Dmitry Maslennikov

indeed 66  , THANKS!  yes

0
Vitaliy Serdtsev · Jan 12, 2021

N<=9 (59 chr)

N>9 (62 chr)

 

Code

n=1:1:j=n-1:-1:1,0:1:!?i=1:1:n-j,i-1:-1:1 i (59 chr)

n=1:1:j=n-1:-1:1,0:1:!?i=1:1:n-j,i-1:-1:1 i#10 (62 chr)

Who is shorter? ;)

0
Eduard Lebedyuk  Jan 12, 2021 to Vitaliy Serdtsev

Right part should go back to 1, so

 1
121
 1

and not

 1
123
 1
0