Code Golf: Diamonds
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
1Bonus Level: For N>9 you can either reuse 1-9 or supply arbitrary characters.
Comments
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 !
}
}
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)@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
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
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>BINGO! ![]()
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
1d(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,!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,afor on "" is great, but just <=9
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 rUTC: 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 rN>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#10eg: 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
1even 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
indeed 66 , THANKS! ![]()
N<=9 (59 chr)
N>9 (62 chr)
Code
f n=1:1:n f j=n-1:-1:1,0:1:n w !?j f i=1:1:n-j,i-1:-1:1 w i#10 (62 chr)
Who is shorter? ;)
Right part should go back to 1, so
1
121
1and not
1
123
1Sorry, I didn't notice. Updated
And now it's right?
Yes!