93
\$\begingroup\$

Inspired by the bugged output in @Carcigenicate's Clojure answer for the Print this diamond challenge.

Print this exact text:

1        2        3        4        5        6        7        8        9        0
1       2       3       4       5       6       7       8       9       0
1      2      3      4      5      6      7      8      9      0
1     2     3     4     5     6     7     8     9     0
1    2    3    4    5    6    7    8    9    0
1   2   3   4   5   6   7   8   9   0
1  2  3  4  5  6  7  8  9  0
1 2 3 4 5 6 7 8 9 0
1234567890
1 2 3 4 5 6 7 8 9 0
1  2  3  4  5  6  7  8  9  0
1   2   3   4   5   6   7   8   9   0
1    2    3    4    5    6    7    8    9    0
1     2     3     4     5     6     7     8     9     0
1      2      3      4      5      6      7      8      9      0
1       2       3       4       5       6       7       8       9       0
1        2        3        4        5        6        7        8        9        0

(From the middle outward in both directions, each digit is separated by one more space than the previous line.)

Challenge rules:

  • There will be no input (or an empty unused input).
  • Trailing spaces are optional.
  • A single trailing new-line is optional.
  • Leading spaces or new-lines are not allowed.
  • Returning a string-array isn't allowed. You should either output the text, or have a function which returns a single string with correct result.

General rules:

  • This is , so shortest answer in bytes wins.
    Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.
  • Standard rules apply for your answer, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
  • Default Loopholes are forbidden.
  • If possible, please add a link with a test for your code.
  • Also, please add an explanation if necessary.
\$\endgroup\$
5
  • \$\begingroup\$ Is outputting an array of strings - 1 string per line - allowed? \$\endgroup\$
    – Shaggy
    Jul 3 '17 at 9:37
  • \$\begingroup\$ @Shaggy Sorry, in this case it should either return a single string with new-lines, or output the result. I've added this as rule to the challenge. \$\endgroup\$ Jul 3 '17 at 11:06
  • \$\begingroup\$ No worries, Kevin; was just chancing my arm to see if I could save myself a couple of bytes. \$\endgroup\$
    – Shaggy
    Jul 3 '17 at 11:09
  • 3
    \$\begingroup\$ @Shaggy Hehe. What other reason would we have to ask question in a code-golf challenge, besides having the purpose of saving those few bytes? ;) \$\endgroup\$ Jul 3 '17 at 11:23
  • 12
    \$\begingroup\$ Ha, that's awesome. I was wondering why that answer suddenly got so much attention. Thanks! \$\endgroup\$ Jul 3 '17 at 19:41

86 Answers 86

4
\$\begingroup\$

MathGolf, 11 bytes

8╤{± *♂r╫up

Try it online!

Explanation

8             push 8
 ╤            range(-n, n+1)
  {           for each element in range:
   ±          absolute value
              space character
     *        multiply space character x number of times
      ♂r      push 10, create range
        ╫     left-rotate list (0 goes to the end)
         u    join with separator (joins using x spaces)
          p   print with newline

Has a trailing newline in the end from printing. @ovs found a mistake in the original code that could be fixed by switching the first 9 to an 8.

\$\endgroup\$
2
  • \$\begingroup\$ I'm pretty sure the 9 should be an 8 instead. This has more lines in the output than the text in the challenge. \$\endgroup\$
    – ovs
    Sep 4 at 12:57
  • \$\begingroup\$ Good catch! This was a long time ago, and I guess that I must've just thought that the outputs were identical. \$\endgroup\$
    – maxb
    Sep 6 at 11:26
65
\$\begingroup\$

Python 2, 59 57 56 55 bytes

i=8;exec"print(' '*abs(i)).join('1234567890');i-=1;"*17

Try it online!

@Leaky Nun helped golfing this a lot, @Praind Suggested a method to save 1 byte, which I formerly thought of, but forgot to edit, @CotyJohnathanSaxman suggested changing the loop.


Explanation

  • i=8 - Assigns the value 8 to a variable called i.

  • exec"..."*17 - Execute that block of code (...) 17 times.

  • print(...) - Output the result.

  • ' '*abs(i) - Create a String with a space repeated |i| times.

  • .join('1234567890') - Interleave the string created above with 1234567890, such that |i| spaces are inserted between the digits.

  • i-=1 - Decrement i, and by executing it 17 times, it reaches -8, which creates te repetitive pattern with the help of abs().

\$\endgroup\$
11
  • 5
    \$\begingroup\$ 59 bytes \$\endgroup\$
    – Leaky Nun
    Jul 3 '17 at 7:30
  • 23
    \$\begingroup\$ This is farm more impressive than 25 bytes in any golfing language imho. \$\endgroup\$
    – Jylo
    Jul 3 '17 at 9:30
  • 4
    \$\begingroup\$ @Jylo Thanks, but the 25 byte-solutions are very impressive, since they show lots of golfing efforts made by their authors. You should first know what each character in their source means, and then you will understand how beautifully golfed they are. \$\endgroup\$
    – Mr. Xcoder
    Jul 3 '17 at 9:33
  • 2
    \$\begingroup\$ @JasonChen No, Python does not allow that syntax, because that's specific to C-like languages. \$\endgroup\$
    – Mr. Xcoder
    Jul 4 '17 at 20:11
  • 5
    \$\begingroup\$ Possible inspiration: Can be done without exec in 55 bytes as well. Used a similar approach to get Python 3 down to 57 bytes. Didn't see any obvious way to get it shorter, by omitting exec, but figured I'd mention it as a possibility. \$\endgroup\$ Jul 6 '17 at 2:27
34
\$\begingroup\$

Vim, 35 bytes:

i1234567890<esc>qqYP:s/\d/& /g
YGp{q7@q

Explanation:

i1234567890<esc>    " insert '1234567890'
qq                  " Record the following into register 'q'
  Y                 "   Yank a line
   P                "   Paste it above us
    :s/\d/& /g      "   Put a space after each number
Y                   "   Yank this line
 G                  "   Move the end of the buffer
  p                 "   Paste the line
   {                "   Move the beginning of the buffer
    q               " Stop recording
     7@q            " Call macro 'q' 7 times
\$\endgroup\$
2
  • 7
    \$\begingroup\$ I don't even care that this isn't the winner, it's beautiful. \$\endgroup\$
    – MrDuk
    Jul 5 '17 at 15:19
  • \$\begingroup\$ @MrDuk Thankyou! I'm glad you like it :) \$\endgroup\$
    – DJMcMayhem
    Jul 5 '17 at 20:09
23
\$\begingroup\$

05AB1E,  14  13 bytes

17F9ÝÀN8αð×ý,

Try it online!

Explanation

17F            # for N in [0 ... 16] do
   9Ý          # push [0 ... 9]
     À         # rotate left
      N8α      # compute the absolute difference between N and 8
         ð×    # push that many spaces
           ý   # merge the list of digits with the space string as separator
            ,  # print
\$\endgroup\$
10
  • 1
    \$\begingroup\$ Hmm...you could've beaten SOGL if you didn't need the s...does it make sense to join a string with a list or a list with a string? I'll suggest improvement to Adnan. Oh, and there's .∊ for û». \$\endgroup\$ Jul 3 '17 at 13:06
  • \$\begingroup\$ @EriktheOutgolfer: Or I could switch up my method and save that 1 byte I needed :) \$\endgroup\$
    – Emigna
    Jul 3 '17 at 13:32
  • \$\begingroup\$ Sorry already upvoted. \$\endgroup\$ Jul 3 '17 at 13:33
  • 3
    \$\begingroup\$ That means I'd have upvoted again... \$\endgroup\$ Jul 3 '17 at 13:36
  • 5
    \$\begingroup\$ 8F9ÝÀNð×ý}».∊ I done did made a spaceship maw! And I was all excited about suggesting a 12-byte edit. \$\endgroup\$ Jul 17 '17 at 20:10
23
\$\begingroup\$

C64 ASM, 358 bytes (102 bytes compiled with basicstub)

This is the closest I could get due to obvious limitations:

C64

jsr $E544
lda #9
sta $FD
nl1: jsr dl
jsr il
dec $FD
bne nl1
inc $FD
nl2: ldy $FD
cpy #9
bne nt1
bl: jmp bl
nt1: iny
sty $FD
jsr dl
jsr il
jmp nl2
dl: clc
ldx #$31
ldy #0
lp: txa
sm: sta $0400, y
inx
cpx #$31
bne c1
rts
c1: cpx #$3A
bne nt2
ldx #$30
clc
nt2: tya
adc $FD
cmp #40
bcc c2
rts
c2: tay
jmp lp
il: lda sm+1
adc #39
bcc nc
inc sm+2
nc: sta sm+1
rts

(Could probably be optimized quite a bit)

\$\endgroup\$
2
  • \$\begingroup\$ Welcome to PPCG! Please use code tag to post pretty. I edited you post. After changes apply you can see, what changed by clicking edit \$\endgroup\$ Jul 4 '17 at 18:32
  • \$\begingroup\$ I've just checked and it won't even fit on an 80 cols PET as the first and last line require 82 columns. \$\endgroup\$ Apr 5 '18 at 16:25
17
\$\begingroup\$

TSQL, 220 148 bytes

Improvement posted by ZLK:

DECLARE @ VARCHAR(MAX)=''SELECT TOP 17@+=REPLACE('1@2@3@4@5@6@7@8@9@0','@',SPACE(ABS(9-RANK()OVER(ORDER BY object_id))))+'
'FROM sys.objects PRINT @

Output:

1        2        3        4        5        6        7        8        9        0
1       2       3       4       5       6       7       8       9       0
1      2      3      4      5      6      7      8      9      0
1     2     3     4     5     6     7     8     9     0
1    2    3    4    5    6    7    8    9    0
1   2   3   4   5   6   7   8   9   0
1  2  3  4  5  6  7  8  9  0
1 2 3 4 5 6 7 8 9 0
1234567890
1 2 3 4 5 6 7 8 9 0
1  2  3  4  5  6  7  8  9  0
1   2   3   4   5   6   7   8   9   0
1    2    3    4    5    6    7    8    9    0
1     2     3     4     5     6     7     8     9     0
1      2      3      4      5      6      7      8      9      0
1       2       3       4       5       6       7       8       9       0
1        2        3        4        5        6        7        8        9        0
\$\endgroup\$
5
  • \$\begingroup\$ DECLARE @ VARCHAR(MAX)=''SELECT TOP 17@+=REPLACE('1@2@3@4@5@6@7@8@9@0','@',SPACE(ABS(9-RANK()OVER(ORDER BY object_id))))+' 'FROM sys.objects PRINT @ \$\endgroup\$
    – ZLK
    Jul 4 '17 at 1:23
  • \$\begingroup\$ Replace sys.objects with sysobjects, and 1object_id` with id I looked for shorter tables in the sys schema, but nothing short had columns as short as sysobjects \$\endgroup\$
    – Jaloopa
    Jul 4 '17 at 11:19
  • \$\begingroup\$ And you can save another few bytes by changing the inner CAST (with the POWER(10)) to VARCHAR \$\endgroup\$
    – Jaloopa
    Jul 4 '17 at 11:21
  • \$\begingroup\$ In fact, avoid all that replace stuff by using LOG10 and REPLICATE: DECLARE @o VARCHAR(MAX)=''SELECT TOP 17 @o+=REPLACE('1@2@3@4@5@6@7@8@9@0','@',replicate(' ',LOG10(POWER(10,ABS(ROW_NUMBER()OVER(ORDER BY id)-9)+1)/10)))+' 'FROM sysobjects PRINT @o \$\endgroup\$
    – Jaloopa
    Jul 4 '17 at 11:32
  • \$\begingroup\$ You can change the variable name to just @ to shave off 3 characters. \$\endgroup\$
    – Bridge
    Jul 4 '17 at 13:18
14
\$\begingroup\$

APL (Dyalog), 22 bytes

↑∊¨(1+|¯9+⍳17)↑¨¨⊂1⌽⎕D

Try it online!

⎕DDigits from zero to nine

1⌽ rotate one step right (puts zero at end)

 enclose (to treat as unit)

()↑¨¨ for each of these numbers, take that many characters from each of the letters:

⍳17 one through seventeen

¯9+ subtract nine

| find the absolute values

1+ add one

∊¨ enlist (flatten) each

 change one layer of depth into a level of rank (matrify)

\$\endgroup\$
4
  • 4
    \$\begingroup\$ In a compact language like this it seriously takes two bytes to add one? \$\endgroup\$
    – aschepler
    Jul 4 '17 at 12:05
  • 3
    \$\begingroup\$ @aschepler Yes. APL is not a golfing language, and doesn't have an increment primitive. J does, but it takes two bytes; >:. \$\endgroup\$
    – Adám
    Jul 4 '17 at 12:07
  • 4
    \$\begingroup\$ APL is not a golfing language ... ↑⊃¨,/¨(1+|¯9+⍳17)↑¨¨⊂1⌽⎕D ... ಠ_ಠ \$\endgroup\$
    – Alexander
    Jul 6 '17 at 15:54
  • 1
    \$\begingroup\$ @Alexander Thanks for getting my attention back to this one. I could golf 3 bytes. \$\endgroup\$
    – Adám
    Jul 6 '17 at 15:59
13
\$\begingroup\$

Haskell, 58 55 bytes

unlines["1234567890">>=(:(' '<$[1..abs n]))|n<-[-8..8]]

Try it online!

This is basically @nimi 's solution :)

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

Java 11 (JDK), 98 bytes

o->{for(int i=-9;++i<9;)System.out.println("".join(" ".repeat(i<0?-i:i),"1234567890".split("")));}

Try it online!

  • -14 bytes by switching to JDK 11, which now has a native String::repeat.

Previous answer (Java (OpenJDK 8)), 113 112 bytes

o->{for(int i=-9;++i<9;)System.out.printf("".join("%1$"+(i<0?-i:i>0?i:"")+"s","1234567890".split(""))+"%n","");}

Try it online!

Explanations

I'm basically constructing the following String 17 times (N is a variable, not an actual value):

"1%1$Ns2%1$Ns3%1$Ns4%1$Ns5%1$Ns6%1$Ns7%1$Ns8%1$Ns9%1$Ns0%n"

It's all the expected digits, joined by %1$Ns where N is the number of spaces between each digit.

%1$Ns basically means "take the first argument and pad it until length is at least N". Also, %1$0s is not supported so a special case %1$s is made for 0.

Finally, I format-print that string using a single argument: "", so the formatter reuses always the same empty string, padding it with spaces as needed.

Saves

  • 1 byte thanks to Kevin Cruijssen
\$\endgroup\$
6
  • 3
    \$\begingroup\$ Nice answer! Btw, I explicitly mentioned in the challenge an unused empty parameter is allowed, so you can replace ()-> with o-> to save a byte. :) \$\endgroup\$ Jul 3 '17 at 10:57
  • \$\begingroup\$ Can you make o an int and use it in your for loop ? for(o=-9;++o<9;) \$\endgroup\$
    – Winter
    Jul 9 '17 at 16:15
  • 3
    \$\begingroup\$ @Winter No, because the challenge says "unused", and that would use the parameter variable, even if the value wouldn't be used. \$\endgroup\$ Jul 9 '17 at 16:44
  • \$\begingroup\$ @OlivierGrégoire It links to this post codegolf.meta.stackexchange.com/questions/12681/… and this posts only says it should be empty (0 for ints). \$\endgroup\$
    – Winter
    Jul 9 '17 at 16:47
  • \$\begingroup\$ @Indeed Indeed, but the challenge itself says "unused". Specific rulings in a challenge overrule defaults. \$\endgroup\$ Jul 9 '17 at 17:43
12
\$\begingroup\$

Japt -R, 19 18 16 14 13 bytes

Aõ%A
£qYçÃÔÅê

Test it or try it online


Explanation

A             :10
 õ           :Range [1,10]
  %A         :Modulo each by 10
\n           :Assign to variable U
£            :Map each element at 0-based index Y
 q           :  Join U with
  Yç         :   Space repeated Y times
    Ã        :End map
     Ô       :Reverse
      Å      :Slice off first element
       ê     :Mirror
             :Implicitly join with newlines and output
\$\endgroup\$
1
  • 2
    \$\begingroup\$ Nice. My 16-byte solution was 9ÆAõ%A qXîÃw ê · \$\endgroup\$ Jul 3 '17 at 20:22
9
\$\begingroup\$

SOGL V0.12, 12 bytes

9{SUē↕∑}¹№╬«

Try it Here! Explanation:

9{     }      9 times do
  SU            push "1234567890"
    ē           push e, predefined with the input, which defaults to 0, and then increase it
     ↕          get that many spaces
      ∑         join the string of digits with those spaces
        ¹     collect the results in an array
         №    reverse the array vertically
          ή  palindromize vertically with 1 overlap
\$\endgroup\$
2
  • \$\begingroup\$ Oh nice, this is even shorter than the accepted answer. Hmm, what is the policy on shorter answers a couple months after an answer has been accepted? Does this now get the check (kinda unfair to the currently accepted answer I think), or should I do something else? Great answer regardless! +1 from me. \$\endgroup\$ Oct 29 '17 at 10:35
  • \$\begingroup\$ @KevinCruijssen Usually the best practice is to update the accepted answer, but as this uses a couple new features, I'd say there's reason to go either way - you choose. \$\endgroup\$
    – dzaima
    Oct 29 '17 at 10:44
8
\$\begingroup\$

Clojure, 126 99 bytes

-27 by fixing a couple of dumb mistakes. The outer use of a wasn't necessary, so I was able to get rid of a altogether and just write apply str once. That also allowed me to use a function macro for the main function, which saved some bytes. I also inlined the call to Math/abs instead of rebinding n-spaces.

Basically a Clojure port of @Mr.Xcoder's Python idea. In retrospect, I should have used the abs/range method for the diamond challenge originally, but I then I may not have produced the bugged output!

Pretty simple. Joins the number string with a number of spaces that depends on the current row.

#(doseq[n(range -8 9)](println(clojure.string/join(apply str(repeat (Math/abs n)\ ))"1234567890")))

(defn bugged []
  (doseq [n-spaces (range -8 9)]
    (println
        (clojure.string/join
          ; "String multiplication"
          (apply str
                 (repeat (Math/abs n-spaces) \space))
          "1234567890"))))
\$\endgroup\$
8
\$\begingroup\$

JavaScript(ES2017), 83 73 72 68 bytes

Thanks Shaggy for saving 10 bytes. Thanks Craig for saving 1 byte. Thanks arcs for saving 4 bytes.

for(i=-9,a="";i++<8;)a+=[...`1234567890
`].join("".padEnd(i<0?-i:i))

for(i=-9,a="";i++<8;)a+=[...`1234567890
`].join("".padEnd(i<0?-i:i))

console.log(a);
.as-console-wrapper { max-height: 100% !important; top: 0; }
.as-console-row:after { display: none !important; }

\$\endgroup\$
11
  • \$\begingroup\$ Save 1 byte by using a polyfill for Math.abs. I also made the a list not hardcoded, but that's a preference... a=[...Array(10).keys()];for(i=-8;i<9;i++)console.log(a.join(' '.repeat(i<0?-i:i))) \$\endgroup\$ Jul 3 '17 at 8:33
  • 1
    \$\begingroup\$ Save some bytes with [..."1234567890"].join("".padEnd(i<0?-i:i)) \$\endgroup\$
    – Shaggy
    Jul 3 '17 at 8:35
  • \$\begingroup\$ @Shaggy Thanks. That saved 10 bytes. :) \$\endgroup\$
    – Tushar
    Jul 3 '17 at 9:23
  • 1
    \$\begingroup\$ @ThomasW: [...Array(10).keys] would have 0 as the first element, not the last. \$\endgroup\$
    – Shaggy
    Jul 3 '17 at 10:29
  • 1
    \$\begingroup\$ You can save a byte using your method storing the result in a string rather than logging: for(i=-8,s="";i<9;i++)s+=[..."1234567890"].join("".padEnd(i<0?-i:i))+`X`, where X is an actual newline \$\endgroup\$
    – Craig Ayre
    Jul 4 '17 at 9:25
7
\$\begingroup\$

Brachylog, 30 29 28 bytes

Ị↺{∧≜;Ṣj₍g;?↔zcc}ᶠ⁹↔;XcP↔Pẉᵐ

Try it online!

Saved one byte thanks to Leaky Nun.

Explanation

Ị↺                               The string "1234567890"
  {               }ᶠ⁹            Find the first 9 outputs of:
   ∧≜                              Take an integer
       ;Ṣj₍                        Juxtapose " " that number of times
           g;?↔z                   Zip that string of spaces with "1234567890"
                cc                 Concatenate twice into one string
                    ↔            Reverse the resuling list
                     ;XcP↔P      Palindromize the list (concatenate X to it into the list P,
                                   P reversed is still P)
                           ẉᵐ    Map writeln
\$\endgroup\$
4
  • 2
    \$\begingroup\$ 28 bytes \$\endgroup\$
    – Leaky Nun
    Jul 3 '17 at 7:45
  • \$\begingroup\$ @LeakyNun I got stuck on the big "Print this exact text" and didn't read that we could have trailing lines… \$\endgroup\$
    – Fatalize
    Jul 3 '17 at 7:49
  • 4
    \$\begingroup\$ 23 bytes \$\endgroup\$
    – Leaky Nun
    Jul 3 '17 at 7:50
  • 1
    \$\begingroup\$ @LeakyNun Why didn't you post your own answer? \$\endgroup\$
    – Fatalize
    Jul 4 '17 at 7:19
7
\$\begingroup\$

Charcoal, 18 bytes

F⁹⪫⁺…I¹:⟦0¶⟧× ι‖B↑

Try it online! Link is to verbose version of code. Explanation:

F⁹      Repeat 9 times (i = loop variable)
  ⪫       Join
   ⁺       Concatentate
    …I¹:    All the characters from str(1) below ":" (i.e. to "9") as a list
    ⟦0¶⟧    A list whose element is the string "0\n"
   × ι     With i spaces
‖B↑     Reflect everything upwards but without duplicating the top line

Note: Cast(1) takes the same number of bytes because "1" would need a separator before ":" (which as it happens the deverbosifier fails to insert).

\$\endgroup\$
7
\$\begingroup\$

R, 108 bytes

for(i in abs(-8:8))cat(paste0(el(strsplit("1234567890","")),paste(rep(" ",i),collapse=""),collapse=""),"\n")

Just pasting and collapsing the strings.

Try it online!

Edit: thanks for Challenger5 for pointing out a problem. Fixed it now.

Edit 2: saved a byte thanks to bouncyball.

\$\endgroup\$
8
  • \$\begingroup\$ Welcome to the site! :) \$\endgroup\$
    – DJMcMayhem
    Jul 3 '17 at 14:31
  • 2
    \$\begingroup\$ This doesn't look quite right - the middle line shouldn't have any spaces at all. \$\endgroup\$ Jul 4 '17 at 6:32
  • \$\begingroup\$ @Challenger5 you are right! fixed it by using paste0 instead of paste so had to add a byte:( \$\endgroup\$
    – Probel
    Jul 4 '17 at 7:36
  • 1
    \$\begingroup\$ save 2 bytes using el: el(strsplit("1234567890","")) instead of strsplit("1234567890","")[[1]] \$\endgroup\$
    – bouncyball
    Jul 5 '17 at 16:21
  • \$\begingroup\$ You can get rid of one of the paste statements by using the sep argument to cat and doing a bit more to get it to 70 bytes \$\endgroup\$
    – Giuseppe
    Apr 5 '18 at 13:36
7
\$\begingroup\$

Python 3, 58 57 54 bytes

i=8
while i+9:print(*'1234567890',sep=' '*abs(i));i-=1

Try it online!

(thanks to @flornquake for the last three bytes; I completely forgot using sep to save vs. .join)

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 54 bytes: while i+9:print(*'1234567890',sep=' '*abs(i));i-=1 \$\endgroup\$
    – flornquake
    Jul 8 '17 at 12:30
7
\$\begingroup\$

Pyth, 14 13 bytes

1 byte thanks to Mnemonic.

V17j*da8NeMST

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ You can save a byte if you loop instead of mapping and joining. Try it here. \$\endgroup\$
    – user48543
    Apr 5 '18 at 14:57
6
\$\begingroup\$

Java 8, 235 234 206 163 bytes

interface A{static void main(String[]a){for(int i=-8;i<9;i++){String s="";for(int j=1,k;j<10;j++){s+=j;for(k=0;k<(i>0?i:-i);k++)s+=" ";}System.out.println(s+0);}}}

Update : -28 bytes thanks to Leaky Nun !
Update 2 : -43 bytes thanks to Leaky Nun again !

Ungolfed :

interface A {
    static void main(String[] a) {
        for (int i = -8; i < 9; i++) {
            String s = "";
            for (int j = 1, k; j < 10; j++) {
                s += j;
                for (k = 0; k < (i > 0 ? i : -i); k++)
                    s += " ";
            }
            System.out.println(s + 0);
        }
    }
}

Try it online

EDIT : The code earlier was wrong ! Made a mistake while golfing the code, it should work as intended now !

\$\endgroup\$
12
  • 4
    \$\begingroup\$ Welcome to PPCG! Tips for golfing in Java and Tips for golfing in <all languages> might be interesting to read through. :) Some things you can golf is removing the public, and get rid of some unnecessary spaces. And there are some more things to improve, but I suggest to read through the tips, and see where you can improve yourself. Again welcome, and enjoy your stay! :) \$\endgroup\$ Jul 3 '17 at 8:24
  • 2
    \$\begingroup\$ Hmm, also, are you sure you've posted the correct code? When I copy-paste your code into TryItOnline it doesn't give the correct output. \$\endgroup\$ Jul 3 '17 at 8:27
  • 1
    \$\begingroup\$ Aww damn, i did something wrong while golfing the code ! I'll fix this ! \$\endgroup\$ Jul 3 '17 at 8:31
  • 1
    \$\begingroup\$ 148 bytes: one loop \$\endgroup\$
    – Leaky Nun
    Jul 3 '17 at 9:07
  • 1
    \$\begingroup\$ 144 bytes \$\endgroup\$
    – Leaky Nun
    Jul 3 '17 at 9:36
6
\$\begingroup\$

Husk, 21 bytes

mΣṪ`:§+↔tḣR8' ṁs`:0ḣ9

This is a full program that prints to STDOUT. Try it online! There are lots of trailing spaces.

Explanation

Husk is still missing lots of essential stuff like a two-argument range function, so parts of this solution are a little hacky.

mΣṪ`:§+↔tḣR8' ṁs`:0ḣ9
              ṁs`:0ḣ9  This part evaluates to the string "1234567890".
                   ḣ9   Range from 1 to 9.
                `:0     Append 0.
              ṁs        Convert each number to string and concatenate.
     §+↔tḣR8'          This part evaluates to a list like ["  "," ",""," ","  "]
                       but with 17 elements instead of 5.
          R8'           A string of 8 spaces.
         ḣ              Take prefixes.
     §+                 Concatenate
       ↔                the reversal and
        t               tail of the prefix list, palindromizing it.
  Ṫ                    Take outer product of the two lists
   `:                  with respect to flipped prepeding.
                       This prepends each digit to each string of spaces.
mΣ                     Map concatenation over the results, computing the rows.
                       Implicitly join with newlines and print.
\$\endgroup\$
1
  • \$\begingroup\$ You could replace ṁs`:0ḣ9 with uṁsḣ10 to generate the string "1234567890" with one fewer byte! \$\endgroup\$ Apr 5 '18 at 16:48
6
\$\begingroup\$

T-SQL 145 152 bytes

DECLARE @ VARCHAR(MAX)=''SELECT TOP 17@+=REPLACE('1x2x3x4x5x6x7x8x9x0','x',SPACE(ABS(number-8)))+'
'FROM master..spt_values WHERE type='P'PRINT @

Updated to use:

  • master..spt_values to generate numbers (WHERE type = 'P', these are always consecutive, starting at 0)
  • @ZLK's TOP 17 idea
  • PRINT (to obey the rules - no stringlists)
  • @JanDrozen's great idea of including the carriage return in the string (I counted that as just one byte - Windows CRLF what?)
  • Idea of @Bridge to use just @ for variable name - all these tricks!!

Results:

1        2        3        4        5        6        7        8        9        0
1       2       3       4       5       6       7       8       9       0
1      2      3      4      5      6      7      8      9      0
1     2     3     4     5     6     7     8     9     0
1    2    3    4    5    6    7    8    9    0
1   2   3   4   5   6   7   8   9   0
1  2  3  4  5  6  7  8  9  0
1 2 3 4 5 6 7 8 9 0
1234567890
1 2 3 4 5 6 7 8 9 0
1  2  3  4  5  6  7  8  9  0
1   2   3   4   5   6   7   8   9   0
1    2    3    4    5    6    7    8    9    0
1     2     3     4     5     6     7     8     9     0
1      2      3      4      5      6      7      8      9      0
1       2       3       4       5       6       7       8       9       0
1        2        3        4        5        6        7        8        9        0

(Thanks @JanDrozen for the REPLACE idea)

\$\endgroup\$
4
  • \$\begingroup\$ This is a nice answer! Welcome to the site :) \$\endgroup\$
    – DJMcMayhem
    Jul 3 '17 at 18:59
  • \$\begingroup\$ Thanks. I see some of us have invented our own languages here :) \$\endgroup\$ Jul 3 '17 at 19:22
  • 1
    \$\begingroup\$ A mix between this answer and the other is optimal: e.g. SELECT TOP 17REPLACE('1x2x3x4x5x6x7x8x9x0','x',SPACE(ABS(9-RANK()OVER(ORDER BY object_id))))FROM sys.objects (assuming you don't need to print). \$\endgroup\$
    – ZLK
    Jul 4 '17 at 1:12
  • \$\begingroup\$ Aha @ZLK - a few nice tricks - TOP 17, RANK OVER, and sys.objects - thank you! \$\endgroup\$ Jul 4 '17 at 8:02
5
\$\begingroup\$

Jelly, 15 bytes

8ŒRA⁶ẋØDṙ1¤jЀY

Try it online!

\$\endgroup\$
5
\$\begingroup\$

Mathematica, 92 bytes

Column@Join[Reverse@(s=Row/@Table[Riffle[Range@10~Mod~10,""<>Table[" ",i]],{i,0,8}]),Rest@s]

Try it online

copy/paste code with ctrl-v
press shift+enter to run

\$\endgroup\$
4
  • \$\begingroup\$ Could you perhaps add a TryItOnline link? \$\endgroup\$ Jul 3 '17 at 7:34
  • \$\begingroup\$ @KevinCruijssen Mathematica is not a free language, and Mathics doesn't seem to work. \$\endgroup\$
    – Leaky Nun
    Jul 3 '17 at 7:36
  • \$\begingroup\$ @LeakyNun Ah ok, thanks. I see MartinEnder post TIO-links sometimes, but those were indeed Mathics links. \$\endgroup\$ Jul 3 '17 at 7:41
  • 1
    \$\begingroup\$ @Kevin Cruijssen yes of course, I added link with instructions \$\endgroup\$
    – ZaMoC
    Jul 3 '17 at 7:43
5
\$\begingroup\$

C, 97 bytes

i=-9;main(j){for(;++i<9;putchar(10))for(j=0;++j<11;printf("        "+8-abs(i)))putchar(48+j%10);}

Your compiler will probably complain a lot about this code; mine threw 7 warnings of 4 different types. Might improve the code later.

Try it online!

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

SOGL V0.12, 14 bytes

ā9{SUčf@*∑Κ}╬Æ

Try it Here!

Explanation:

ā               push an empty array
 9{        }    9 times do
   SU             push "1234567890"
     č            chop into characters
      f@*         get current iteration (0-based) amount of spaces
         ∑        join with that
          Κ       prepend this to the array
            Β  palindromize vertically with 1 overlap
\$\endgroup\$
4
\$\begingroup\$

Charcoal, 21 20 19 18 bytes

F⁹«F…¹χ⁺κ… ι0⸿»‖B↑

Try it online!

Link to the verbose version. Basically I create the lower part of the drawing and then reflect the text up.

\$\endgroup\$
7
  • 1
    \$\begingroup\$ When I saw the question I though I'd try this myself in Charcoal before looking at the answer. I started with a 21-byte answer but golfed it to 20 bytes... spooky! \$\endgroup\$
    – Neil
    Jul 3 '17 at 8:02
  • \$\begingroup\$ @Neil :-D Well, it's 19 bytes now! \$\endgroup\$
    – Charlie
    Jul 3 '17 at 8:08
  • \$\begingroup\$ Yup, just found that golf myself. I was also looking into E…⁹⪫E…·¹χI﹪κχ× ι‖B↑ (also for 19 bytes) but the interpreter doesn't seem to like it (I can't see what's wrong with it myself). \$\endgroup\$
    – Neil
    Jul 3 '17 at 8:24
  • \$\begingroup\$ Oh, Range takes two arguments, silly me. \$\endgroup\$
    – Neil
    Jul 3 '17 at 8:53
  • 1
    \$\begingroup\$ Turns out Map was a red herring, so I've now posted my 18-byte answer. \$\endgroup\$
    – Neil
    Jul 3 '17 at 10:30
4
\$\begingroup\$

CJam, 21 bytes

A,(+aH*ee{:\8-zS**n}/

Try it online!

Explanation

A,     e# Get [0 1 2 ... 9].
(+     e# Rotate the 0 to the end.
aH*    e# Wrap in an array and repeat 17 times.
ee     e# Enumerate. This pairs each copy of the array with its index in
       e# the list.
{      e# For each [index array] pair...
  :\   e#   Unwrap the pair and swap its order.
  8-z  e#   Get the absolute difference of the index from 8.
  S*   e#   Get a string of that many spaces.
  *    e#   Riffle the list of digits with the string of spaces.
  n    e#   Print it with a trailing linefeed.
}/
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Alternative solution (also 21 bytes): 9{S*A,(+\*}%_W%);\+N* \$\endgroup\$ Jul 4 '17 at 6:10
4
\$\begingroup\$

Batch, 163 bytes

@set s=1 2 3 4 5 6 7 8 9 0
@set t=         
@for %%l in (9 7)do @for /l %%i in (1,1,%%l)do @call:%%l
:7
@set t=  %t%
:9
@set t=%t:~1%
@call echo %%s: =%t%%%

Note: First line ends in 9 spaces. Explanation: Uses creative line numbering! The first loop needs to run 9 times and delete a space each time, while the second loop needs to run 8 times and add a space each time. The former is achieved by running it 7 times and falling through for the 8th time, while the latter is achieved by adding two spaces and falling through to delete one of them again.

\$\endgroup\$
4
\$\begingroup\$

bash (and shell utils), 69 bytes

for i in `seq 9 -1 1;seq 2 9`;do seq -f%-$i.f 9|tr -d \\n;echo 0;done
\$\endgroup\$
4
\$\begingroup\$

C (gcc), 76 bytes

f(x,y){for(y=-9;++y<9;puts(""))for(x=10;x++;printf("%-*d",abs(y)+1,x%=10));}

Try it online!

It outputs some trailing spaces, which is supposed to be OK.

It prints the numbers using left-justified fields of dynamic length - that's what the format string %-*d is for.

The inner loop has some funny initialization (starts from 10; any multiple of 10 would be fine) to "simplify" its termination condition.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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