| Bytes | Lang | Time | Link |
|---|---|---|---|
| 135 | Swift | 230721T175718Z | Bbrk24 |
| 056 | Perl 5 | 240722T082434Z | Donat |
| 104 | Bash | 240728T163234Z | Donat |
| 095 | JavaScript V8 | 230725T162055Z | Fhuvi |
| 088 | Zsh | 240728T140459Z | Donat |
| nan | R | 240725T145127Z | David Jo |
| 067 | Raku Perl 6 rakudo | 240723T221122Z | bb94 |
| 117 | Setanta | 240723T214409Z | bb94 |
| 016 | Japt R | 240722T171948Z | Shaggy |
| 065 | Perl 5 | 230729T070153Z | good old |
| 045 | J | 230728T083545Z | Jonah |
| 069 | Ruby | 230721T202643Z | Value In |
| 108 | Zsh | 230726T042103Z | roblogic |
| 012 | Vyxal | 230724T154209Z | lynn |
| 014 | Thunno 2 | 230721T163411Z | The Thon |
| 016 | Vyxal | 230721T170041Z | The Thon |
| 021 | 05AB1E | 230722T042028Z | The Thon |
| 015 | 05AB1E | 230724T103045Z | Kevin Cr |
| 098 | C gcc | 230721T195835Z | c-- |
| 098 | C gcc | 230723T181240Z | matteo_c |
| 060 | Retina | 230722T061358Z | Neil |
| 107 | Python 3 | 230721T171349Z | The Thon |
| 102 | JavaScript V8 | 230721T171437Z | Arnauld |
| 083 | Ruby | 230721T192813Z | Level Ri |
| 214 | Scala | 230722T025742Z | 138 Aspe |
| 020 | Jelly | 230721T220413Z | Jonathan |
| 080 | Python 3 | 230721T202559Z | xnor |
| 023 | Pip l | 230721T193619Z | DLosc |
| 017 | Charcoal | 230721T184830Z | Neil |
| 278 | Vyxal | 230721T162133Z | The Empt |
Swift, 156 135 bytes
-21 thanks to @macOSisthebestOS, for figuring out how to remove the argument labels to String.init(repeating:count:)
let f={(1...$0)+(1..<$0).reversed()},r={{String.init}()(" ",$0)}
for n in f(9){print(r((n-6)*(n-5))+f(n).map{"\($0)"+r(9-n)}.joined())}
Port of @TheThonnu's various answers. f is a shorthand for counting from 1 to n and back. I put the joining spaces inside the map callback rather than passing them to joined because the latter would involve an extra argument label (joined(separator:)).
Perl 5, 64 57 56 bytes
say$"x(4-abs)**2,(1x(9-abs))**2=~s//$"x abs/ger for-8..8
Saved a few bytes by using non-destructive regex and eliminating variable $n.
Perl 5, 64 bytes
$n=abs,$_=(1x(9-$n))**2,s//$"x$n/ge,say$"x($n-4)**2 .$_ for-8..8
Based on my Perl solution for Print this diamond. After computing the numbers I insert the required spaces using a regex. Indentation is added in the parameter of say.
Bash, 107 104 bytes
for c in {1..9} {8..1}
do printf "%$[(5-c)**2]s"
sed "s/\(\)/`printf %$[9-c]s`/g"<<<$[(10**c/9)**2]
done
Derived from my solution for zsh.
JavaScript (V8), 95 98 bytes
-3 bytes thanks to Donat
I don't know if it's very polite to answer my own challenge, but here it is!
z=9
for(z of(s=(n=1,p="".padEnd(9-z))=>p+(n<z?n+s(n+1)+n:z)+p)())print("".padEnd((5-z)**2)+s())
This is the combined works of Donat's recent Javascript answer to the challenge "Print this diamond"
and The Thonnu's formula: (n−6)(n−5) for the leading spaces (which has become (n−5)**2 since).
@Donat, don't hesitate if you want to post this as your own answer, this is mostly your work after all :)
Zsh, 88 bytes
for c ({1..9} {8..1}){w=$[(10**c/9)**2]
printf %$[(5-c)**2]s${w///`printf %$[9-c]s`}\\n}
Inspired by @roblogic's nice answer.
R, 123 Bytes 113 bytes
p=paste0;for(i in s<-c(1:9,8:1))message(rep(" ",(i-6)*(i-5)),p(s[c(1:i,i:1-1)],p(rep(" ",abs(i-9)),collapse="")))
rep(" ",(i-6)*(i-5)) gives the trailing white spaces
s[c(1:i,i:1-1)] gives the numbers on each row
rep(" ",abs(i-9) gives the number of white space between each character
Raku (Perl 6) (rakudo), 68 67 bytes
Port of Donat’s Perl 5 solution.
say ' 'x(4-.abs)²,(1 x(9-.abs))².subst('',' 'x.abs):g:e for -8..8
Setanta, 117 bytes
d:=dearbh@mata le k idir(-8,9){n:=9-d(k)k=[]le i idir(-n+1,n)k+=[n-d(i)]scriobh(" "*(n*n-11*n+30)+nasc@k(" "*(9-n)))}
Japt -R, 16 bytes
9
õÈõ ê q´UçÃû ê
9\nõÈõ ê q´UçÃû ê
9\n :Assign 9 to variable U
õ :Range [1,U]
È :Map each X
õ : Range [1,X]
ê : Palindromise
q : Join with
´U : Prefix decrement U
ç : That many spaces
à :End map
û :Centre pad each with spaces to the length of the longest
ê :Palindromise
:Implicit output joined with newlines
Perl 5, 65 bytes
$a=-1-abs,print map/\D|0/?$":$_,map$a+10+abs()/$a,-20..20for-8..8
I'd count it 63+1 (instead of 65), the +1 for -E and say instead of print, can't figure out how to get it in TIO which also doesn't add 1 for -l switch. But it beats direct competitors anyway :), it's strange nobody used this strategy.
Ruby, 71 69 bytes
Function outputting an array of lines. A synthesis of my old answer with Level River St's answer as per their suggestions.
-2 bytes by switching to Ruby 2.7+ (TIO is on 2.5.5)
->{(-8..8).map{([*1...x=9-k=_1.abs,*x.downto(1)]*(' '*k)).center 41}}
Ruby, 77 bytes
Recursion, ahoy! Function outputting an array of lines.
f=->x=1{s=([*1...x,*x.downto(1)]*(' '*(9-x))).center 41;x<9?[s,*f[x+1],s]: s}
Zsh, 108 bytes
for c ({1..9} {8..1}){w=$[(10**c/9)**2];printf \\n%$[(5-c)*(6-c)]s
for x (${(s::)w})printf %-$[9-$#w/2]s $x}
Adapted from @Donat's "bash diamond" code and my "spaceship maw" solution.
Vyxal, 12 bytes
9ƛɾ9n-Ij;∞øṗ
Uses the strangely specific øṗ (Flip Brackets Vertical Palindromise, Center, Join on Newlines) builtin.
9ƛ ; for n in [1...9]:
ɾ9n-Ij join [1..n] by 9-n spaces
∞ palindromize the list
øṗ palindromize and center all the strings and join by \n
Thunno 2, 14 bytes
9Rƒıṅ8_ṣjⱮ;ⱮØC
Seems like the compressed list part (¿qJŒḣ“ɲþẇɦɠ¿i) could be golfed a bit with a formula, but I couldn't find it.
I found the formula \$(n-6)(n-5)\$ for the leading spaces.
I don't need that any more. Port of Kevin Cruijssen's 05AB1E answer.
-5 by porting @KevinCruijssen's 05AB1E answer
Explanation
9Rƒıṅ8_ṣjⱮ;ⱮØC # Full program
9Rƒ # Prefixes of [1..9]
ı ; # Map over:
ṅ8_ # 8 - iteration index
ṣj # Join by that many spaces
Ɱ # Palindromise
Ɱ # Palindromise
ØC # Center
# Implicit output
Old:
9RⱮı65d-pṣnRⱮn9_ṣj+ # Full program
9RⱮı # Map over [1,2,...,8,9,8,...,2,1]:
65d-pṣ # Push (n-6) * (n-5) spaces
nRⱮ # Push [1,2,...,n,...2,1]
n9_ṣj # Join by (9-n) spaces
+ # Prepend the leading spaces
# Implicit output, joined on newlines
Vyxal, 16 bytes
9ɾKėƛ÷$8εIj∞;∞øĊ
Port of my Thunno 2 answer Kevin Cruijssen's 05AB1E answer.
-1 thanks to @lyxal
-3 by porting @KevinCruijssen's 05AB1E answer
Explanation
9ɾKėƛ÷$8εIj∞;∞øĊ # Full program
9ɾK # Prefixes of [1..9]
ėƛ ; # Enumerate and map over:
÷$8ε # Absolute difference between 8 and the iteration index
Ij # Join by that many spaces
∞ # Palindromise
∞ # Palindromise
øĊ # Center
# Implicit output
Old:
9ɾ∞ƛ65f-ΠInɾ∞n9εIj+ # Full program
9ɾ∞ƛ # Map over [1,2,...,8,9,8,...,2,1]:
65f-ΠI # Push (n-6) * (n-5) spaces
nɾ∞ # Push [1,2,...,n,...2,1]
n9εIj # Join by (9-n) spaces
+ # Prepend the leading spaces
# Implicit output, joined on newlines
05AB1E, 21 bytes
9Lûε65S-Pð×yLûy9αð×ý«
Port of my Thunno 2 answer. Unfortunately 05AB1E doesn't have the "push n spaces" built-in that Thunno 2 and Vyxal have.
Output as a list of lines. TIO footer joins by newlines.
-2 thanks to @KevinCruijssen
Explanation
9Lûε65S-Pð×yLûy9αð×ý« # Implicit input
9Lûε # Map over [1,2,...,8,9,8,...,2,1]:
65S-P×J # Push (n-6) * (n-5) spaces
yLû # Push [1,2,...,n,...2,1]
y9αð×ý # Join by (9-n) spaces
« # Prepend the leading spaces
# Implicit output
05AB1E, 16 15 bytes
9Lηε8N-ð×ýû}û.c
Explanation:
9L # Push a list in the range [1,9]
η # Pop and push its prefixes: [[1],[1,2],[1,2,3],...]
ε # Map over each prefix:
8N- # Push 8 minus the 0-based map index
ð× # Pop and push a string of that many spaces
ý # Join the list with those spaces (or empty string for N=8) as delimiter
û # Palindromize this string
}û # After the map: palindromize the list of lines as well
.c # Join the list by newlines,
# and add leading spaces where necessary to centralize the lines
# (after which the result is output implicitly)
C (gcc), 98 bytes
i,c=8;main(j){for(;--i/j?i=j=9-abs(c--):1;)printf("%*c",i-j?j-10:11*i-i*i-31,i-j?48+j-abs(i):10);}
C (gcc), 98 bytes
j;main(z,x){for(;z<10;putchar(x>20?10:x%z|(x=58-z-abs(x/z))<49?32:x))x=j%43-21,z=abs(j++/43-8)+1;}
Retina, 60 bytes
9*
$`;$'¶
v`( *;)( *)
$.1$2
P`.+
%`^.
$^$<'
L$`9
$>`$^$`
Try it online! Explanation:
9*
Insert 9 spaces.
$`;$'¶
Generate a 9×9 square of spaces, but with ;s on the main diagonal.
v`( *;)( *)
$.1$2
Use the spaces before the ; to generate the numbers from n down to 1 on the nth row, spaced using the spaces after the ;.
P`.+
%`^.
$^$<'
Reflect horizontally.
L$`9
$>`$^$`
Reflect vertically (actually around the central 9 but it comes to the same thing).
92 bytes in Retina 0.8.2:
9$*
$`;$'¶
(?=( *;)( *)).
$.1$2
.+
$&12$*
(.(.{20})).*
$2$1
%20O^$`.
¶.+¶$
$&$`
9>O^$`
Try it online! Explanation:
9$*
Insert 9 spaces.
$`;$'¶
Generate a 9×9 square of spaces, but with ;s on the main diagonal.
(?=( *;)( *)).
$.1$2
Use the spaces before the ; to generate the numbers from n down to 1 on the nth row, spaced using the spaces after the ;.
.+
$&12$*
(.(.{20})).*
$2$1
%20O^$`.
Reflect horizontally by prefixing each line with a copy of the 20 characters after the first and then reversing those 20 characters.
¶.+¶$
$&$`
9>O^$`
Reflect vertically by duplicating all the lines except the last and then reversing only the duplicates.
Python 3, 107 bytes
R=range
for n in[*R(1,9),*R(9,0,-1)]:print((n-6)*(n-5)*' '+((9-n)*' ').join(map(str,[*R(1,n),*R(n,0,-1)])))
Port of my Thunno 2 answer.
-5 thanks to @Neil
JavaScript (V8), 102 bytes
A full program.
for(n=17;n--;print(s))for(q=n<9?n:16-n,x=9-q,i=s=''.padEnd(20-q*x);~q;)s+=`${++i>q?1+q--:i}`.padEnd(x)
C (gcc), 116 bytes
-1 thanks to @c--
q;x;i;main(n){for(n=17;n--;i=!puts(""))for(q=n<9?n:16-n,x=9-q;~q;q-=i>q)printf("%*d",i>1?x:x+12-q*x+q,++i>q?1+q:i);}
Ruby, 83 bytes
Updated per suggestion from @ValueInk
(-8..8).map{|i|puts (((-8+c=i.abs)..8-c).map{|j|"#{9-c-j.abs}"}*(" "*c)).center 41}
Ruby, 86 bytes
(-8..8).map{|i|puts (((-8+c=i.abs)..8-c).map{|j|(9-c-j.abs).to_s}*(" "*c)).center(41)}
Scala, 214 bytes
Port of @Level River St's Ruby answer in Scala.
Golfed version. Try it online!
object M extends App{(-8 to 8).map{i=>val c=i.abs
println((" "*(20-((-8+c)to(8-c)).map{j=>(9-c-j.abs).toString}.mkString(" "*c).length/2)+((-8+c)to(8-c)).map{j=>(9-c-j.abs).toString}.mkString(" "*c)).slice(0,41))}}
Ungolfed version. Try it online!
object Main {
def main(args: Array[String]): Unit = {
(-8 to 8).map { i =>
val c = i.abs
val line = ((-8 + c) to (8 - c)).map { j =>
(9 - c - j.abs).toString
}.mkString(" " * c)
println(center(line, 41))
}
}
def center(s: String, width: Int): String = {
val padSize = width - s.length
if(padSize <= 0) s
else (" " * (padSize/2)) + s + (" " * (padSize - padSize/2))
}
}
Jelly, 20 bytes
9Rjạ9⁶xƊƊ€Uz⁶ZUŒBŒḄY
A full program that prints the diamond.
How?
9Rjạ9⁶xƊƊ€Uz⁶ZUŒBŒḄY - Main Link: no arguments
9 - 9
€ - for each {i in [1..9]}:
Ɗ - last three links as a monad f(i):
R - range -> [1..i]
Ɗ - last three links as a monad f(i):
ạ9 - absolute difference with 9 -> 9-i
⁶ - space character
x - repeat -> ' '*(9-i)
j - join -> e.g. "1 2 3"
U - reverse each
z⁶ - transpose with space character as filler
Z - transpose
U - reverse each
ŒB - bounce each
ŒḄ - bounce
Y - join with newline characters
- implicit, smashing print
Python 3, 80 bytes
i=8
while i+9:k=abs(i);print(f"{(' '*k).join(str((10**(9-k)//9)**2)):^41}");i-=1
Uses the classic arithmetic trick where, for example,
(10**9//9)**2 == 111111111**2 == 12345678987654321
Python 3, 79 bytes
f=lambda k=8,c=1:(s:=[f"{(' '*k).join(str(c*c)):^41}"])[k:]or s+f(k-1,c*10+1)+s
Recursive function that generates a list of lines when called with no inputs. The idea is to start with the center line, then repeatedly sandwich what we have between two copies of a new line.
Python 3, 79 bytes
f=lambda p=8*' ',c=1:(s:=f"{p.join(str(c*c)):^41}\n")+(p and f(p[1:],c*10+1)+s)
Recursive function that generates the whole multiline string, with a trailing newline, when called with no input.
Pip -l, 23 bytes
QPZJ RZJ:R\,_JsX9-_M\,9
Explanation
QPZJ RZJ:R\,_JsX9-_M\,9
\,9 ; Range from 1 to 9, inclusive
M ; Map this function to each:
\,_ ; Range from 1 to fn arg, inclusive
R ; Reversed
J ; Join on this string:
sX ; A number of spaces equal to
9-_ ; 9 minus fn arg
ZJ: ; Transpose list of strings, filling gaps with spaces
R ; Reverse list of lines
ZJ ; Transpose again
QP ; Quad-palindromize
; Autoprint, joining on newlines (-l flag)
Here's the step-by-step evolution, using size 5 instead of 9 and showing spaces using periods:
\,5
1
2
3
4
5
R\,_JsX5-_M
1
2...1
3..2..1
4.3.2.1
54321
ZJ:
12345
....4
...33
..2.2
.1.21
.....
..11.
R
..11.
.....
.1.21
..2.2
...33
....4
12345
ZJ
......1
..1...2
1..2..3
1.2.3.4
..12345
QP
......1......
..1...2...1..
1..2..3..2..1
1.2.3.4.3.2.1
..123454321..
1.2.3.4.3.2.1
1..2..3..2..1
..1...2...1..
......1......
Charcoal, 17 bytes
E⁹⪫⮌…·¹⊕ι× ⁻⁸ι‖O¬
Try it online! Link is to verbose version of code. Explanation:
E⁹⪫⮌…·¹⊕ι× ⁻⁸ι
For n from 1 to 9, output the numbers from n down to 1 joined with 9-n spaces, but doing everything 0-indexed.
‖O¬
Reflect both left and down to complete the diamond gone beautifully wrong.
Vyxal, 278 Bytes
»×ẏ∴⁺¢ɽƒ3∴ĿṘ€ȯLdd2)"lY∇λµ≠Ḟw⅛!ḋ₌₁ẊṘ?
‡₄¥D‹Ḣ€ɾȯḟǑLdẇm₁≤ɖ↵$hḭ«€X⇧0A2&/•p[₀‟₃5Ǔ≈Π>ḋṠßĠ-⋏↳ĖOUḃ`∑ß∨(V⋏⋎ṁ7Ż≥×h'D℅p×>¥D↲‛℅ddlʀɾ⌐D%₆₄⁋ŻṘġȧ₅ḣ'ɾp0Ḋ¢lǐ⟨6K⌊∵¶⟨Ḣ,g#;¬‛^4⟨β•qrṠg₅Þf⋎→__↑¢[ẏNcḟ≠H꘍C¼↔Π꘍∧≬ǏḭḞǔṄꜝ⌐ø₇µ`ʁȯ⁋√ÞVe↲ <†∧↔⊍n÷PḂė?ġ¦$₌$e⋎\]↵⇩ṁn€⋏+₀ǒ&Ċ⌐∩ṁṗ½VḃḞβ#WżL∵ṖðǓmẊOur,8‹»`1
23456789`τ
Compressed number converted to custom string base.