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IJKLMNOPQRQPONMLKJIHGFEDCBA
ABCDEFGHIJKLMNOPQPONMLKJIHGFEDCBA
ABCDEFGHIJKLMNOPONMLKJIHGFEDCBA
ABCDEFGHIJKLMNONMLKJIHGFEDCBA
ABCDEFGHIJKLMNMLKJIHGFEDCBA
ABCDEFGHIJKLMLKJIHGFEDCBA
ABCDEFGHIJKLKJIHGFEDCBA
ABCDEFGHIJKJIHGFEDCBA
ABCDEFGHIJIHGFEDCBA
ABCDEFGHIHGFEDCBA
ABCDEFGHGFEDCBA
ABCDEFGFEDCBA
ABCDEFEDCBA
ABCDEDCBA
ABCDCBA
ABCBA
ABA
A
Notes
- You can have any amount of leading and trailing whitespace on all directions, as long as all non-whitespace characters are properly aligned to make the diamond shape.
- A multiline string, character matrix, list of strings, list of lists of characters, and equivalent are acceptable outputs.
- Shortest answer per language, scored in bytes, wins!
[Brain-Flak], 250 bytes …
5y ago
[V (vim)], 21 bytes ¬AZ …
5y ago
BQN, 19 bytesSBCS ``` ' '+32 …
5y ago
Stax, 12 bytes ç ∩<▬%▌ê▓jF …
5y ago
Canvas, 10 7 6 bytes Z[]/ …
5y ago
Japt `-R`, 14 13 bytes …
5y ago
[JavaScript (Node.js)], 78 byt …
5y ago
Vyxal `C`, 8 bytes …
5y ago
[Jelly], 13 bytes ØAUƤz …
5y ago
[Python 3], 99 bytes …
5y ago
[Uiua], 38 bytes (SBCS) Ful …
6mo ago
Perl, 73 bytes $="A …
6mo ago
Japt `-R`, 11 bytes ;Bå …
5y ago
13 answers
You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.
JavaScript (Node.js), 78 bytes
(m=(a,b=26,c=a(--b))=>b?[c,...m(a,b),c]:[c])=>m(a=>m(b=>(c=25-a-b)<0?32:c+65))
0 comment threads
V (vim), 21 bytes
¬AZ¬YA|òlÄ26|xx>>YGpH
Explanation:
¬AZ " Insert every character from A to Z
¬YA " Insert every character from Y to A
| " Go back to the beginning of this line
ò " While True...
l " Move to the right (if there is only one character
" on this line, break the loop)
Ä " Duplicate this line upwards
26| " Go to the 26th character on this line
xx " Delete two characters
>> " Indent this line with one space
Y " Yank this line
G " Go to the last line
p " Paste the line that was just yanked downwards
H " Go back to the first line
" (Implicit) End loop
Hexdump:
00000000: ac41 5aac 5941 7cf2 6cc4 3236 7c78 783e .AZ.YA|.l.26|xx>
00000010: 3e59 4770 48 >YGpH
0 comment threads
Brain-Flak, 250 bytes
((((()()()){}){}){}()){(({})[()])}{}(<>)<>{({}<>)<>}<>{(({})[()])}([]){{}([(({}))](((()()()){}){}){}()){({}[()])<>((((()()()()){}){}){})<>}{}<>(((((()()()()){}){}){}){}())<>{({}[()])<>(({})())<>}{}{({}[()])<>(({})[()])<>}{}<>((()()()()()){})<>([])}<>
Readable, commented version:
# Push 25
((((()()()){}){}){}())
# Push range(25, 1)
{
(({})[()])
}
{}
# Push a 0 on to the alternate stack and return to main stack
(<>)<>
# Reverse values onto other stack
{
({}<>)
<>
}
<>
# Push range(25, 0)
{
(({})[()])
}
# Push the stack height.
([])
# At this point, the stack has 0, 1, 2... 23, 24, 25, 24, 23... 2, 1, 0
# Go through each number of these numbers, and call it N
{
# Pop the stack height counter
{}
# Push (25 - N), and underneath it push N an additional time
([(({}))](((()()()){}){}){}())
# That many times...
{
# Decrement
({}[()])
<>
# Push a space onto the ascii-art stack
((((()()()()){}){}){})
<>
}
{}
# on the ascii-art stack, push the letter A
<>
(((((()()()()){}){}){}){}())
<>
# N times...
{
# Decrement N
({}[()])
# Push "The last character + 1"
<>
(({})())
<>
}
{}
# N times...
{
# Decrement N
({}[()])
# Push "The last character - 1"
<>
(({})[()])
<>
}
{}
<>
# Push a newline
((()()()()()){})
<>
([])
}<>
0 comment threads
BQN, 19 bytesSBCS
' '+32⊸<⊸×+⌜˜⌽⊸⌊↕59
This uses the whitespace allowance, padding by four spaces on all sides to make the arithmetic work better. It can be adjusted by replacing ↕59 with 4+↕51 at the cost of two characters.
BQN has no built-in idea of the alphabet, but it has a nice system for Unicode character arithmetic. This solution depends on the length 26 of the alphabet, and the distance 33 ('A'-' ') from space to the start. Fortunately these are close together. We will build a matrix of numbers that goes from 0, for ' ', at the corners to 33+26-1, or 58, at the center, then bring all numbers below 33 to 0. At the middle of each edge we get 29, leaving 4 spaces before 'A'. Before plays a big role here.
' '+32⊸<⊸×+⌜˜⌽⊸⌊↕59
↕59 # Range 0...58
⌽ # Reverse: 58...0
⊸⌊ # Before Minimum: 0…29…0
+⌜ # Addition table
˜ # with itself
32⊸< # More than 32
⊸× # times the original
' '+ # Add to space
Another 21-character "exact" solution is (+⌜˜⊏-∘≠↑'A'+⍷)⌽⊸⌊↕51. Now 'A' is used as the basis and spaces come from fill elements in Take (↑), so it will work for any starting character.
0 comment threads
Canvas, 10 7 6 bytes
Z[]/┼┼
Creates the top portion, centers it, and mirrors it.
-3 bytes from dzaima's hint.
-1, found the 6 byter!
Explanation
Z[]/++
Z push the alphabet
[] get all prefixes
/ diagonalize by padding each line with one less than the previous
++ Quad palindromize, without reflecting letters
Vyxal C, 8 bytes
kA¦ømƛøm
Explanation:
kA # Push the alphabet
¦ # Prefixes
øm # Palindromize list without duplicating middle
ƛ # For each:
øm # Palindromize without duplicating middle
# 'C' flag - Center and print, separated by newlines
0 comment threads
Jelly, 13 bytes
ØAUƤz⁶ZUŒBŒḄY
How it works
ØAUƤz⁶ZUŒBŒḄY - Main link. Takes no arguments
ØA - Yield the uppercase alphabet
Ƥ - Over each prefix:
U - Reverse it
z⁶ - Zip, padding with spaces
Z - Transpose
U - Reverse each
ŒB - Bounce each
ŒḄ - Bounce the entire string
Y - Join by newlines
0 comment threads
Python 3, 99 bytes
r=[*range(26)]
a="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
for i in r+r[-2::-1]:print(" "*(26-i)+a[:i]+a[i::-1])
0 comment threads
Uiua, 38 bytes (SBCS)
Full program, prints diamond to STDOUT. Some nifty tricks turned out to be longer than some straightforward ways. Uses one leading whitespace even on the longest line to make "⍥@ " work smoothly even then (it chokes on 0 input).
F ← &p⊂⟜(↘₁⇌)+@A&pf⍥@ ˜-26⟜⇡+1
≡F⊂⇡26⇌⇡25
Scoring remark: I count "F ← " as 2 bytes, as it will be auto-formatted even when entering F=.
Explanation
F ← &p⊂⟜(↘₁⇌)+@A&pf⍥@ ˜-26⟜⇡+1 # helper function F: print diamond line
≡F⊂⇡26⇌⇡25 # apply F to range [0 ... 25 ... 0]
Helper function
&p⊂⟜(↘₁⇌)+@A&pf⍥@ ˜-26⟜⇡+1 # argument: diamond line number (0-based)
# example input: 4
+1 # 5 increment
⟜⇡ # 5 [0 .. 4] range, but keep on top
˜-26 # 21 [0 .. 4] subtract from 26
⍥@ # " [...] " [0 .. 4] repeat " " that many times
&pf # [0 .. 4] print string w/o trailing newline
+@A # "ABCDE" convert to character range
⟜(↘₁⇌) # "ABCDE" "DCBA" push reversed shortened character range, but keep original on top
&p⊂ # join both and print them (with newline)
Main
⇌⇡25 # push [24 .. 0]
⇡26 # push [0 .. 25]
⊂ # join both [0 .. 24 25 24 .. 0]
≡F # call F for each element
0 comment threads
Perl, 73 bytes
$_="A$/";$b=A;$a=$b++,s|^| |gm,s| (.*$a)(.*$/)|$&$1$b$a$2$&|
until/Z/;say
Explanation:
# initialise output string and replacement character
$_ = "A$/";
$b = A;
# (
# set check character, update replacement character
$a = $b++,
# prepend a space to every line
s|^| |gm,
# find the line with check character
# - replicate before and after
# - adjust check -> check+replacement+check
# - fix up space count
s| (.*$a)(.*$/)|$&$1$b$a$2$&|
# )
# stop when string contains a Z
until /Z/;
# display result
say

0 comment threads