| Bytes | Lang | Time | Link |
|---|---|---|---|
| 216 | Tcl | 250707T170846Z | sergiol |
| 112 | Zsh | 230727T060854Z | roblogic |
| 014 | Thunno 2 N | 230722T045927Z | The Thon |
| 011 | Vyxal C | 220707T214427Z | naffetS |
| 014 | Canvas | 201106T110030Z | Razetime |
| 090 | Julia 1.0 | 201105T171039Z | MarcMush |
| 014 | 05AB1E | 201105T163957Z | Kevin Cr |
| 015 | Japt R | 201105T153355Z | Shaggy |
| 127 | JavaScript ES6 | 170705T142729Z | Saming |
| 014 | Charcoal | 170705T133344Z | Erik the |
| 163 | Haskell | 170705T213152Z | Mephy |
| 124 | Python 3 | 170705T153725Z | Chase |
| 017 | 05AB1E | 170705T125150Z | Erik the |
| 131 | PHP | 170705T125033Z | Titus |
| 095 | Python 3 | 170705T124044Z | Mr. Xcod |
| 196 | Java OpenJDK 8 | 170705T135550Z | Olivier |
| 141 | Mathematica | 170705T142526Z | ZaMoC |
| 037 | APL Dyalog | 170705T123532Z | Adá |
| 018 | SOGL V0.12 | 170705T120220Z | dzaima |
| 021 | Japt | 170705T115411Z | ETHprodu |
Tcl, 216 bytes
proc P s {set n -[set m [expr [string le $s]-1]]
time {set x [join [split $s ""] [format %[set b [expr abs([incr n]-1)]]s ""]]
puts [format %[expr $m*($b-$m)]s ""][string ra [string rev $x] 0 end-1]$x} [expr 2*$m+1]}
Zsh, 117 113 109 112 bytes
b=`rev<<<$1`${1:1};j=$#1
for c ({$j..1} {2..$j}){printf %$[(j-1)*(j-c)]s
for x (${(s::)b})printf %-$c\s $x;echo}
Try it online!
109b invalid
113b
117b
b is the palindrome; j is the length of input string $1. Iterate c over the range {j..1..j}. For each c, print a prefixed number of spaces. For every character x in b, print x with c spaces between each.
Thunno 2 N, 14 bytes
ḣı$ṅṣj;ðƬrⱮṬrⱮ
I have absolutely no idea why this works, but it does.
Explanation
ḣı$ṅṣj;ðƬrⱮṬrⱮ # Implicit input
ḣı ; # len(input) - 1 times:
$ j # Join the input by...
ṅṣ # ...loop index spaces
ðƬ # Transpose with filler space
rⱮ # Reverse and palindromise
Ṭ # Tranpose it
rⱮ # Reverse and palindromise
# Implicit output, joined on newlines
I did quite a bit of experimenting, producing a lot of buggy outputs. Here's my favourite:
0987654321234567890
0 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 0
0 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 0
0 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 0
0 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 0
0 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 0
0 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 0
0 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 0
0 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 0
0 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 0
0 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 0
0 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 0
0 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 0
0 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 0
0 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 0
0 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 0
0987654321234567890
Vyxal C, 11 bytes
ṪẏṘ∞ƛ?Ṙ∞$Ij
Explanation
ṪẏṘ∞ƛ?Ṙ∞$Ij
Ṫ # Remove the last element (removing the first element would be fine too)
ẏ # Make a range [0, length)
Ṙ # Reverse
∞ # Palindromize, a + a[::-1][1:]
ƛ # Map, and for each:
?Ṙ # Push the input reversed
∞ # Palindromize that (call this X)
$ # Swap so the current item is at the top
I # Push that many spaces
j # Join the characters of X by that
# C flag centers and joins on newlines
Canvas, 14 bytes
L╷rH ×⁸;*∔}↔↕┼
Explanation
L|rH ×⁸;*∔}↔↕┼
L length of input
|r decrement, make range [0..n-1]
H create an empty art object, start a loop with i
× repeat space i times
⁸; swap with the input
* join input with i spaces
∔ add to the bottom of the previous iteration
} close the loop
↔↕ mirror horizontally and vertically
┼ quad palindromize with 1 character overlap
Julia 1.0, 100 90 bytes
s->(l=length(s)-2;[' '^(-~l*(l-j))*prod(s[abs(i)+1]*' '^j for i=~l:l+1) for j=abs.(-l:l)])
-10 bytes thanks to @KevinCruijssen
05AB1E, 14 bytes
¦ε\RðN×ýû}Rû.c
Input as a list of characters.
Try it online or verify all test cases.
14 bytes alternative:
ā¨RjJíðδÜ€ûû.c
Also takes a list of characters as input.
Try it online or verify all test cases.
Explanation:
¦ # Remove the first item of the (implicit) input-list
# (could have removed the last as alternative as well)
ε # Map over each character:
\ # Discard the character
R # Reverse the (implicit) input-list
N # Push the 0-based map-index
ð × # Repeat a space character that many times as string
ý # Join the reversed input-list with this spaces-string as delimiter
û # Palindromize the entire string
}R # After the map: reverse the resulting list
û # Palindromize this entire list
.c # And centralize it, which also implicitly joins by newlines
# (after which the result is output implicitly)
ā # Push a list in the range [1, (implicit) input-length]
¨ # Remove the last item to make the range [1, input-length)
R # Reverse the list to (input-length, 1]
j # Pad each character in the (implicit) input-list with leading spaces so
# the string lengths become equal to the current integer
J # Join each inner list of strings together to a single string
í # Reverse each string
δ # Map over each string:
ð Ü # And remove all trailing spaces
€û # Palindromize each string
û # Palindromize the entire list
.c # And centralize it, which also implicitly joins by newlines
# (after which the result is output implicitly)
Japt -R, 15 bytes
ÊÆÔ¬qXç)êÃÔÅê û
ÊÆÔ¬qXç)êÃÔÅê û :Implicit input of string
Ê :Length
Æ :Map each X in the range [0,length)
Ô : Reverse U
¬ : Split
q : Join with
Xç : X spaces
) : End join
ê : Palindromise
à :End map
Ô :Reverse
Å :Slice off first element
ê :Palindromise
û :Centre pad each element with spaces to the length of the longest
:Implicit output joined with newlines
JavaScript (ES6), 159 136 129 127 bytes
f=(i,x=1-(l=i.length-1),y=x<0?-x:x,[,...b]=i)=>l>x?''.padEnd(l*(l+~y))+[...b.reverse(),...i].join(''.padEnd(y))+`
`+f(i,x+1):''
Try it online! Explanation below
// This is a recursive function
// First, inputs and various variable initializations
// by using defaults
let func = (
// Text input, will not be modified through recursion
input,
// Current line, for the first function call we start from -lines to +lines
// It's roughly equivalent to lines*2 but this helps us computing the spacing
// Also computing the total amount of lines
currentLine = 1 - (totalLines = input.length - 1),
// Getting the absolute value of the current line (like Math.floor)
absCurrentLine = currentLine < 0 ? -currentLine : currentLine,
// Getting the input without it's first letter, useful for the palidrome of the input
[,...slicedInput] = input
// Base case, stopping the recursion if the current line
// is still below the total amount of lines
) => totalLines > currentLine
// Leading spacing
? ''.padEnd(totalLines * (totalLines + ~absCurrentLine)) +
// Putting together the palindrome version and adding spaces between the chars
[...slicedInput.reverse(), ...input].join(''.padEnd(absCurrentLine)) + `
// Line return + recursion call
` + f(input, currentLine + 1)
: ''
First entry to codegolf, I apologise in advance for any obvious mistakes.
Thanks to Justin Mariner for saving 23 bytes! Thanks to Craig Ayre for saving 11 bytes and for the bug report.
Charcoal, 14 bytes
E⁻Lθ¹⪫θ× ι‖O←↑
AST:
Program
├Print
│└E: Map
│ ├⁻: Difference
│ │├L: Length
│ ││└θ: Identifier θ
│ │└1: Number 1
│ └⪫: Join
│ ├θ: Identifier θ
│ └×: Product
│ ├' ': String ' '
│ └ι: Identifier ι
└‖O: Reflect overlap
└Multidirectional
├←: Left
└↑: Up
Haskell, 177 163 bytes
import Data.List
k n=[1..n]>>" "
f s=let n=length s in map(\x->(k(((n-1)*(n-(abs x)))))++(intercalate (k(abs x))$map(\z->[z])$((++)=<<reverse.tail) s))[n,n-1.. -n]
The function f is the challenge function and returns a list of strings ([String]), using unlines on it should provide the same visual output as the test cases (main = putStr $ unlines $ f "test string" to compile it).
-14 bytes thanks to @nimi
Python 3, 134 124 bytes
f=lambda s:'\n'.join([' '*(len(s)-1)*abs(len(s)-abs(i)-2)+(' '*abs(i)).join(s[::-1]+s[1:]) for i in range(2-len(s),len(s)-1)])
First post to PPCG after lurking for a while. Looking for any suggestions/advice!
Thanks to @LyricLy and @Łukasz Rogalski for improvements!
PHP, 145 131 bytes
It took some thinking to golf that additional byte; but it was worth it.
while($y<=2*$e=strlen($a=$argn)-1)echo($p=str_pad)("
",$e*($e-$d=abs($y++-$e))+1),chunk_split($a.substr(strrev($a),1),1,$p("",$d));
prints a leading newline. Run as pipe with -nR or try it online.
breakdown
while($y<=2*$e=strlen($a=$argn)-1) # $e=length-1, loop through rows
# 1. print linebreak and left padding
echo($p=str_pad)("\n",$e*($e-$d=abs($y++-$e))+1),
chunk_split(
$a.substr(strrev($a),1) # 2. palindromize input
,1,$p("",$d)); # 3. insert $e..0..$e spaces between characters
alternative solution, same length:
for($d=-$e=strlen($a=$argn)-1;$d<$e;)echo($p=str_pad)("
",$e*($e-$b=abs($d++))+1),chunk_split($a.substr(strrev($a),1),1,$p("",$b));
Python 3, 149 141 95 bytes
def f(s):l=len(s)-1;[print((' '*abs(i)).join(s[:0:-1]+s).center(2*l*l+1))for i in range(1-l,l)]
Thanks to @KevinCruijssen and @ETHproductions for saving some bytes
Special thanks to @notjagan for saving 46 bytes!
Java (OpenJDK 8), 201 196 bytes
s->{for(int l=s.length()-1,i=-l,x=0;++i<l;x+=i<0?l:-l)System.out.printf("%1$"+(x<1?"":x)+"s"+s.join("%1$"+(i<0?-i:i>0?i:"")+"s",(new StringBuffer(s.substring(1)).reverse()+s).split(""))+"%n","");}
It's the same idea as the one I used for the previous challenge, except that the generator string is now a tad longer and with more hard to handle cases.
%1$Ns0%1$Ns9%1$Ns8%1$Ns7%1$Ns6%1$Ns5%1$Ns4%1$Ns3%1$Ns2%1$Ns1%1$Ns2%1$Ns3%1$Ns4%1$Ns5%1$Ns6%1$Ns7%1$Ns8%1$Ns9%1$Ns0%n
Mathematica, 141 bytes
Column[Join[Reverse@(s=Row/@Table[Riffle[Reverse@Rest@b~Join~b,""<>Table[" ",i]],{i,0,Length[b=Characters@#]-1}]),Rest@s],Alignment->Center]&
APL (Dyalog), 37 bytes
Requires ⎕IO←0 which is default on many systems.
{⍉m(2-d)↓⍉(m←⊖⍪1↓⊢)↑∊¨(1↓⍳d←≢⍵)↑¨¨⊂⍵}
{…} anonymous function where the argument is represented by ⍵
(…)↑¨¨⊂⍵ for each (¨) of the following numbers take (↑) that many characters from each (¨) of the entire (⊂) argument, padding with spaces as necessary:
≢w the number of characters in the argument
d← store that in d
⍳ that many ɩndices (0 … d − 1)
1↓ drop one (the zero)
∊¨ ϵnlist (flatten) each
↑ raise the rank (convert the list of lists into a matrix)
(m←…) apply the following tacit function m, defined as:
⊖ the upside-down argument
⍪ on top of
1 one [row]
↓ dropped from
⊢ the argument
⍉ transpose
(…)↓ drop:
2-d = −(d − 2), i.e. d − 2 rows from the bottom
m apply m
⍉ transpose
SOGL V0.12, 22 18 bytes
ā,⁄H{,čFH@*∑Κ}▓±╬-
Explanation:
ā push an empty array - canvas
,⁄H{ } input length-1 times do
, push the input
č chop it into chars
FH@* get the current iteration-1 amount of spaces
∑ join the chopped input with the spaces
Κ prepend it to the array
▓ space to a square
± reverse each string in that list
╬- quad-palindromize with 1 X and 1 Y overlap and without swapping characters
Japt, 22 21 bytes
¬Å£¬qYîÃy w ê y w ê ·
Explanation
The first 8 bytes generate the bottom-right quadrant of the pattern:
¬ Å £ ¬ qYîÃ
Uq s1 mXY{Uq qYî}
Uq : Split the input into characters.
s1 : Slice off the first.
mXY{ } : Map each item X and index Y to
Uq : the input split into chars,
q : joined with
Yî : Y spaces.
At this point we have an array of e.g. ["ABCD", "A B C D", "A B C D"]. Unfortunately, it takes 13 bytes to square this:
y w ê y w ê ·
y w ê y w ê qR
y : Pad each line to the same length with spaces and transpose.
w : Reverse the array, and
ê : palindromize. Now we have the bottom half of the output transposed.
y : Transpose back.
w ê : Reverse and palindromize again, giving the full output.
qR : Join with newlines.
: Implicit: output result of last expression