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 code-golf, 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.
-
\$\begingroup\$ Is outputting an array of strings - 1 string per line - allowed? \$\endgroup\$Shaggy– Shaggy2017年07月03日 09:37:05 +00:00Commented Jul 3, 2017 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\$Kevin Cruijssen– Kevin Cruijssen2017年07月03日 11:06:21 +00:00Commented Jul 3, 2017 at 11:06
-
1\$\begingroup\$ No worries, Kevin; was just chancing my arm to see if I could save myself a couple of bytes. \$\endgroup\$Shaggy– Shaggy2017年07月03日 11:09:18 +00:00Commented Jul 3, 2017 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\$Kevin Cruijssen– Kevin Cruijssen2017年07月03日 11:23:48 +00:00Commented Jul 3, 2017 at 11:23
-
14\$\begingroup\$ Ha, that's awesome. I was wondering why that answer suddenly got so much attention. Thanks! \$\endgroup\$Carcigenicate– Carcigenicate2017年07月03日 19:41:10 +00:00Commented Jul 3, 2017 at 19:41
97 Answers 97
Python 2, (削除) 59 57 56 (削除ここまで) 55 bytes
i=8;exec"print(' '*abs(i)).join('1234567890');i-=1;"*17
@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 value8to a variable calledi.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 with1234567890, such that|i|spaces are inserted between the digits.i-=1- Decrementi, and by executing it 17 times, it reaches-8, which creates te repetitive pattern with the help ofabs().
-
5
-
26\$\begingroup\$ This is farm more impressive than 25 bytes in any golfing language imho. \$\endgroup\$Jylo– Jylo2017年07月03日 09:30:53 +00:00Commented Jul 3, 2017 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– Mr. Xcoder2017年07月03日 09:33:29 +00:00Commented Jul 3, 2017 at 9:33
-
3\$\begingroup\$ @JasonChen No, Python does not allow that syntax, because that's specific to C-like languages. \$\endgroup\$Mr. Xcoder– Mr. Xcoder2017年07月04日 20:11:19 +00:00Commented Jul 4, 2017 at 20:11
-
5\$\begingroup\$ Possible inspiration: Can be done without
execin 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 omittingexec, but figured I'd mention it as a possibility. \$\endgroup\$ShadowRanger– ShadowRanger2017年07月06日 02:27:46 +00:00Commented Jul 6, 2017 at 2:27
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
-
8\$\begingroup\$ I don't even care that this isn't the winner, it's beautiful. \$\endgroup\$MrDuk– MrDuk2017年07月05日 15:19:49 +00:00Commented Jul 5, 2017 at 15:19
-
\$\begingroup\$ @MrDuk Thankyou! I'm glad you like it :) \$\endgroup\$DJMcMayhem– DJMcMayhem2017年07月05日 20:09:40 +00:00Commented Jul 5, 2017 at 20:09
05AB1E, (削除) 14 (削除ここまで) 13 bytes
×ばつý,
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
-
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\$Erik the Outgolfer– Erik the Outgolfer2017年07月03日 13:06:03 +00:00Commented Jul 3, 2017 at 13:06 -
\$\begingroup\$ @EriktheOutgolfer: Or I could switch up my method and save that 1 byte I needed :) \$\endgroup\$Emigna– Emigna2017年07月03日 13:32:14 +00:00Commented Jul 3, 2017 at 13:32
-
\$\begingroup\$ Sorry already upvoted. \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年07月03日 13:33:57 +00:00Commented Jul 3, 2017 at 13:33
-
3\$\begingroup\$ That means I'd have upvoted again... \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年07月03日 13:36:26 +00:00Commented Jul 3, 2017 at 13:36
-
7\$\begingroup\$
8F9ÝÀNð×ý}».∊I done did made a spaceship maw! And I was all excited about suggesting a 12-byte edit. \$\endgroup\$Magic Octopus Urn– Magic Octopus Urn2017年07月17日 20:10:32 +00:00Commented Jul 17, 2017 at 20:10
C64 ASM, 358 bytes (102 bytes compiled with basicstub)
This is the closest I could get due to obvious limitations:
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 #3ドルA
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)
-
\$\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\$Евгений Новиков– Евгений Новиков2017年07月04日 18:32:41 +00:00Commented Jul 4, 2017 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\$Shaun Bebbers– Shaun Bebbers2018年04月05日 16:25:40 +00:00Commented Apr 5, 2018 at 16:25
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
-
\$\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– ZLK2017年07月04日 01:23:48 +00:00Commented Jul 4, 2017 at 1:23 -
\$\begingroup\$ Replace
sys.objectswithsysobjects, and 1object_id` withidI looked for shorter tables in thesysschema, but nothing short had columns as short assysobjects\$\endgroup\$Jaloopa– Jaloopa2017年07月04日 11:19:11 +00:00Commented Jul 4, 2017 at 11:19 -
\$\begingroup\$ And you can save another few bytes by changing the inner
CAST(with thePOWER(10)) toVARCHAR\$\endgroup\$Jaloopa– Jaloopa2017年07月04日 11:21:59 +00:00Commented Jul 4, 2017 at 11:21 -
\$\begingroup\$ In fact, avoid all that replace stuff by using
LOG10andREPLICATE: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– Jaloopa2017年07月04日 11:32:24 +00:00Commented Jul 4, 2017 at 11:32 -
\$\begingroup\$ You can change the variable name to just
@to shave off 3 characters. \$\endgroup\$Bridge– Bridge2017年07月04日 13:18:47 +00:00Commented Jul 4, 2017 at 13:18
Haskell, (削除) 58 (削除ここまで) 55 bytes
unlines["1234567890">>=(:(' '<$[1..abs n]))|n<-[-8..8]]
This is basically @nimi 's solution :)
APL (Dyalog), 22 bytes
↑∊ ̈(1+| ̄9+⍳17)↑ ̈ ̈⊂1⌽⎕D
⎕D Digits 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)
-
4\$\begingroup\$ In a compact language like this it seriously takes two bytes to add one? \$\endgroup\$aschepler– aschepler2017年07月04日 12:05:17 +00:00Commented Jul 4, 2017 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– Adám2017年07月04日 12:07:14 +00:00Commented Jul 4, 2017 at 12:07 -
6\$\begingroup\$
APL is not a golfing language...↑⊃¨,/¨(1+|¯9+⍳17)↑¨¨⊂1⌽⎕D... ಠ_ಠ \$\endgroup\$Alexander– Alexander2017年07月06日 15:54:17 +00:00Commented Jul 6, 2017 at 15:54 -
1\$\begingroup\$ @Alexander Thanks for getting my attention back to this one. I could golf 3 bytes. \$\endgroup\$Adám– Adám2017年07月06日 15:59:31 +00:00Commented Jul 6, 2017 at 15:59
Java 11 (JDK), 98 bytes
o->{for(int i=-9;++i<9;)System.out.println("".join(" ".repeat(i<0?-i:i),"1234567890".split("")));}
- -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","");}
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, %10ドルs 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
-
3\$\begingroup\$ Nice answer! Btw, I explicitly mentioned in the challenge an unused empty parameter is allowed, so you can replace
()->witho->to save a byte. :) \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2017年07月03日 10:57:28 +00:00Commented Jul 3, 2017 at 10:57 -
\$\begingroup\$ Can you make
oanintand use it in your for loop ?for(o=-9;++o<9;)\$\endgroup\$Winter– Winter2017年07月09日 16:15:08 +00:00Commented Jul 9, 2017 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\$Olivier Grégoire– Olivier Grégoire2017年07月09日 16:44:43 +00:00Commented Jul 9, 2017 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– Winter2017年07月09日 16:47:47 +00:00Commented Jul 9, 2017 at 16:47
-
\$\begingroup\$ @Indeed Indeed, but the challenge itself says "unused". Specific rulings in a challenge overrule defaults. \$\endgroup\$Olivier Grégoire– Olivier Grégoire2017年07月09日 17:43:23 +00:00Commented Jul 9, 2017 at 17:43
Japt -R, (削除) 19 (削除ここまで) (削除) 18 (削除ここまで) (削除) 16 (削除ここまで) (削除) 14 (削除ここまで) 13 bytes
Aõ%A
£qYçÃÔÅê
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
-
2\$\begingroup\$ Nice. My 16-byte solution was
9ÆAõ%A qXîÃw ê ·\$\endgroup\$ETHproductions– ETHproductions2017年07月03日 20:22:11 +00:00Commented Jul 3, 2017 at 20:22
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"))))
Python 3, (削除) 58 (削除ここまで) (削除) 57 (削除ここまで) 54 bytes
i=8
while i+9:print(*'1234567890',sep=' '*abs(i));i-=1
(thanks to @flornquake for the last three bytes; I completely forgot using sep to save vs. .join)
-
1\$\begingroup\$ 54 bytes:
while i+9:print(*'1234567890',sep=' '*abs(i));i-=1\$\endgroup\$flornquake– flornquake2017年07月08日 12:30:42 +00:00Commented Jul 8, 2017 at 12:30
SOGL V0.12, 12 bytes
9{SUē↕∑}1No╬«
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
1 collect the results in an array
No reverse the array vertically
ή palindromize vertically with 1 overlap
-
\$\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\$Kevin Cruijssen– Kevin Cruijssen2017年10月29日 10:35:34 +00:00Commented Oct 29, 2017 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– dzaima2017年10月29日 10:44:35 +00:00Commented Oct 29, 2017 at 10:44
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; }
-
\$\begingroup\$ Save 1 byte by using a polyfill for
Math.abs. I also made thealist 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\$Thomas Wagenaar– Thomas Wagenaar2017年07月03日 08:33:09 +00:00Commented Jul 3, 2017 at 8:33 -
1\$\begingroup\$ Save some bytes with
[..."1234567890"].join("".padEnd(i<0?-i:i))\$\endgroup\$Shaggy– Shaggy2017年07月03日 08:35:24 +00:00Commented Jul 3, 2017 at 8:35 -
\$\begingroup\$ @Shaggy Thanks. That saved 10 bytes. :) \$\endgroup\$Tushar– Tushar2017年07月03日 09:23:33 +00:00Commented Jul 3, 2017 at 9:23
-
1\$\begingroup\$ @ThomasW:
[...Array(10).keys]would have0as the first element, not the last. \$\endgroup\$Shaggy– Shaggy2017年07月03日 10:29:30 +00:00Commented Jul 3, 2017 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`, whereXis an actual newline \$\endgroup\$Craig Ayre– Craig Ayre2017年07月04日 09:25:29 +00:00Commented Jul 4, 2017 at 9:25
Brachylog, (削除) 30 (削除ここまで) (削除) 29 (削除ここまで) 28 bytes
Ị↺{∧≜;Ṣj(g;?↔zcc}f9↔;XcP↔Pẉm
Saved one byte thanks to Leaky Nun.
Explanation
Ị↺ The string "1234567890"
{ }f9 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)
ẉm Map writeln
-
2
-
\$\begingroup\$ @LeakyNun I got stuck on the big "Print this exact text" and didn't read that we could have trailing lines... \$\endgroup\$Fatalize– Fatalize2017年07月03日 07:49:58 +00:00Commented Jul 3, 2017 at 7:49
-
4
-
1\$\begingroup\$ @LeakyNun Why didn't you post your own answer? \$\endgroup\$Fatalize– Fatalize2017年07月04日 07:19:22 +00:00Commented Jul 4, 2017 at 7:19
Charcoal, 18 bytes
×ばつ ι‖B↑
Try it online! Link is to verbose version of code. Explanation:
F9 Repeat 9 times (i = loop variable)
⪫ Join
+ Concatentate
...I1: 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).
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.
Edit: thanks for Challenger5 for pointing out a problem. Fixed it now.
Edit 2: saved a byte thanks to bouncyball.
-
\$\begingroup\$ Welcome to the site! :) \$\endgroup\$DJMcMayhem– DJMcMayhem2017年07月03日 14:31:09 +00:00Commented Jul 3, 2017 at 14:31
-
2\$\begingroup\$ This doesn't look quite right - the middle line shouldn't have any spaces at all. \$\endgroup\$Esolanging Fruit– Esolanging Fruit2017年07月04日 06:32:02 +00:00Commented Jul 4, 2017 at 6:32
-
\$\begingroup\$ @Challenger5 you are right! fixed it by using
paste0instead ofpasteso had to add a byte:( \$\endgroup\$Probel– Probel2017年07月04日 07:36:22 +00:00Commented Jul 4, 2017 at 7:36 -
1\$\begingroup\$ save 2 bytes using
el:el(strsplit("1234567890",""))instead ofstrsplit("1234567890","")[[1]]\$\endgroup\$bouncyball– bouncyball2017年07月05日 16:21:23 +00:00Commented Jul 5, 2017 at 16:21 -
-
\$\begingroup\$ You can save a byte if you loop instead of mapping and joining. Try it here. \$\endgroup\$user48543– user485432018年04月05日 14:57:12 +00:00Commented Apr 5, 2018 at 14:57
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);
}
}
}
EDIT : The code earlier was wrong ! Made a mistake while golfing the code, it should work as intended now !
-
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\$Kevin Cruijssen– Kevin Cruijssen2017年07月03日 08:24:53 +00:00Commented Jul 3, 2017 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\$Kevin Cruijssen– Kevin Cruijssen2017年07月03日 08:27:53 +00:00Commented Jul 3, 2017 at 8:27
-
1\$\begingroup\$ Aww damn, i did something wrong while golfing the code ! I'll fix this ! \$\endgroup\$Alex Ferretti– Alex Ferretti2017年07月03日 08:31:03 +00:00Commented Jul 3, 2017 at 8:31
-
1\$\begingroup\$ 148 bytes: one loop \$\endgroup\$Leaky Nun– Leaky Nun2017年07月03日 09:07:20 +00:00Commented Jul 3, 2017 at 9:07
-
1
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.
-
\$\begingroup\$ You could replace
ṁs`:0ḣ9withuṁsḣ10to generate the string "1234567890" with one fewer byte! \$\endgroup\$Sophia Lechner– Sophia Lechner2018年04月05日 16:48:45 +00:00Commented Apr 5, 2018 at 16:48
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_valuesto generate numbers (WHERE type = 'P', these are always consecutive, starting at 0)- @ZLK's
TOP 17idea - 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)
-
\$\begingroup\$ This is a nice answer! Welcome to the site :) \$\endgroup\$DJMcMayhem– DJMcMayhem2017年07月03日 18:59:52 +00:00Commented Jul 3, 2017 at 18:59
-
\$\begingroup\$ Thanks. I see some of us have invented our own languages here :) \$\endgroup\$Reversed Engineer– Reversed Engineer2017年07月03日 19:22:01 +00:00Commented Jul 3, 2017 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– ZLK2017年07月04日 01:12:28 +00:00Commented Jul 4, 2017 at 1:12 -
\$\begingroup\$ Aha @ZLK - a few nice tricks - TOP 17, RANK OVER, and sys.objects - thank you! \$\endgroup\$Reversed Engineer– Reversed Engineer2017年07月04日 08:02:47 +00:00Commented Jul 4, 2017 at 8:02
Mathematica, 92 bytes
Column@Join[Reverse@(s=Row/@Table[Riffle[Range@10~Mod~10,""<>Table[" ",i]],{i,0,8}]),Rest@s]
copy/paste code with ctrl-v
press shift+enter to run
-
\$\begingroup\$ Could you perhaps add a TryItOnline link? \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2017年07月03日 07:34:50 +00:00Commented Jul 3, 2017 at 7:34
-
\$\begingroup\$ @KevinCruijssen Mathematica is not a free language, and Mathics doesn't seem to work. \$\endgroup\$Leaky Nun– Leaky Nun2017年07月03日 07:36:27 +00:00Commented Jul 3, 2017 at 7:36
-
\$\begingroup\$ @LeakyNun Ah ok, thanks. I see MartinEnder post TIO-links sometimes, but those were indeed Mathics links. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2017年07月03日 07:41:16 +00:00Commented Jul 3, 2017 at 7:41
-
1\$\begingroup\$ @Kevin Cruijssen yes of course, I added link with instructions \$\endgroup\$ZaMoC– ZaMoC2017年07月03日 07:43:45 +00:00Commented Jul 3, 2017 at 7:43
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.
SOGL V0.12, 14 bytes
ā9{SUčf@*∑Κ}╬Æ
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
MathGolf, 11 bytes
8╤{± *♂r╫up
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.
-
\$\begingroup\$ I'm pretty sure the
9should be an8instead. This has more lines in the output than the text in the challenge. \$\endgroup\$ovs– ovs2021年09月04日 12:57:17 +00:00Commented Sep 4, 2021 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– maxb2021年09月06日 11:26:16 +00:00Commented Sep 6, 2021 at 11:26
Charcoal, (削除) 21 20 19 (削除ここまで) 18 bytes
F9«F...1χ+κ... ι0⸿»‖B↑
Link to the verbose version. Basically I create the lower part of the drawing and then reflect the text up.
-
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– Neil2017年07月03日 08:02:17 +00:00Commented Jul 3, 2017 at 8:02
-
\$\begingroup\$ @Neil :-D Well, it's 19 bytes now! \$\endgroup\$Charlie– Charlie2017年07月03日 08:08:39 +00:00Commented Jul 3, 2017 at 8:08
-
\$\begingroup\$ Yup, just found that golf myself. I was also looking into
E...9⪫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– Neil2017年07月03日 08:24:06 +00:00Commented Jul 3, 2017 at 8:24 -
\$\begingroup\$ Oh, Range takes two arguments, silly me. \$\endgroup\$Neil– Neil2017年07月03日 08:53:46 +00:00Commented Jul 3, 2017 at 8:53
-
1\$\begingroup\$ Turns out
Mapwas a red herring, so I've now posted my 18-byte answer. \$\endgroup\$Neil– Neil2017年07月03日 10:30:29 +00:00Commented Jul 3, 2017 at 10:30
CJam, 21 bytes
A,(+aH*ee{:8円-zS**n}/
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.
}/
-
1\$\begingroup\$ Alternative solution (also 21 bytes):
9{S*A,(+\*}%_W%);\+N*\$\endgroup\$Esolanging Fruit– Esolanging Fruit2017年07月04日 06:10:35 +00:00Commented Jul 4, 2017 at 6:10
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.
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
C (gcc), 76 bytes
f(x,y){for(y=-9;++y<9;puts(""))for(x=10;x++;printf("%-*d",abs(y)+1,x%=10));}
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.
Explore related questions
See similar questions with these tags.