Word Set Square
Challenge
Given a string, e.g. Hello
, do the following:
Mirror it:
Hello -> HelloolleH
and create a right triangle using it as the sides:
H
ee
l l
l l
o o
o o
l l
l l
e e
HelloolleH
Which looks sort of like a set square.
That's it!
Scoring
This is code-golf. Shortest answer in each language wins.
[APL (Dyalog Unicode)], 23 byt …
3y ago
Japt `-R`, 16 15 14 bytes …
2y ago
Scala, 99 bytes ```scala s=> …
3y ago
[V (vim)], 23 bytes æw| …
3y ago
[Haskell], 110 bytes ```has …
3y ago
JavaScript, 76 bytes Output …
3y ago
Vyxal `J`, 12 bytes ``` ḣm …
3y ago
Ruby, 74 bytes Full program …
3y ago
JavaScript, 102 bytes s …
3y ago
Ruby, 70 bytes ```ruby ->s …
3y ago
[Python 3], 196 195 145 102 91 …
3y ago
11 answers
APL (Dyalog Unicode), 23 bytes
Anonymous tacit prefix function. Reuires 0-based indexing (⎕IO←0
)
(⊢⍪⍨¯1↓⊢,∘↑-∘⍳∘≢↑¨⊢)⊢,⌽
⌽
reverse the argument
⊢,
prepend the argument
(
…)
apply the following tacit function to that:
⊢
on the characters of the argument
…↑¨
of each character, take this many characters:
-∘⍳∘≢
the negative indices $0…\text{length}-1$
…∘↑
mix list of strings into character matrix, then:
⊢,
prepend the argument (vertically)
¯1↓
drop the last row
⊢⍪⍨
append the argument below
0 comment threads
V (vim), 23 bytes
æw|PòlÄxòjVLkîyllv$r $Ð
Hexdump:
00000000: e677 7c50 f26c c478 f26a 564c 6bee 796c .w|P.l.x.jVLk.yl
00000010: 6c76 2472 2024 d0 lv$r $.
0 comment threads
Japt -R
, 16 15 14 bytes
pÔ¬Ëú°EDùEÃÆpÔ
pÔ¬Ëú°EDùEÃÆpÔ :Implicit input of string
p :Append
Ô : Reverse
¬ :Split
Ë :Map each D at 0-based index E
ú : Right pad
°E : to length E, prefix incremented
DùE : with D, left padded to length E
à :End map
Æ :Modify the last element, replacing it with
p : Append to input
Ô : Reverse
:Implicit output joined with newlines
Haskell, 110 bytes
f a=s>>=(#)$a++reverse a
c@(a:b)!f=f c:b!f
_!_=[]
f#n=n!((n!).f)
s(p:q)(d:e)(b:c)|e==c||e==[]=b|q==c=d|0<1=' '
0 comment threads
Scala, 99 bytes
s=>{val x=s+s.reverse
s(0)+"\n"+x.tail.init.zipWithIndex.map{case(c,i)=>c+" "*i+c+"\n"}.mkString+x}
0 comment threads
JavaScript, 76 bytes
Outputs an array of lines.
s=>[...s+=[...s].reverse().join``].map((c,x)=>s[-~x]?c.padEnd(x)+(x?c:``):s)
0 comment threads
Vyxal J
, 12 bytes
ḣmėƛ÷꘍ṘǏ;⁋?m
ḣ # Head extract (x[0], x[1:] )
m # Mirror the ToS (x[1:])
ė # Zip with 0...length
ƛ ; # Map...
÷꘍ # Pad by spaces to correct amount
ṘǏ # Append the first character to the end
⁋ # Join by newlines
?m # Input mirrored
# (J flag) Output stack joined by newlines
0 comment threads
JavaScript, 102 bytes
s=>[...x=s.split``].concat(x.reverse()).reduce((l,c,i,a)=>l+`
`+(a[i+1]?c+' '.repeat(i-1)+c:a.join``))
The first part creates the 'mirrored' string in an array. reduce
starts with the first element (which conveniently handles the top vertex of the triangle) and calls the provided function for each later element, with the arguments being the previous value, the element, its index, and the array. a[i+1]
is a way of checking for the last element: it is undefined
(which is falsy) for the last element, and a character (truthy) for the rest.
0 comment threads
Ruby, 74 bytes
Full program:
s=gets+$_.reverse
puts [s[0]]+s[1..-2].chars.zip(0..).map{_1+' '*_2+_1}<<s
Note that this is not fit for interactive use because it looks weird when you input trailing newlines. In bash, try echo -n Hello | ruby wordsetsquare.rb
75 bytes, Proc
->n{n+=n.reverse;([n[0]]+n[1..-2].chars.zip(0..).map{_1+' '*_2+_1}<<n)*'
'}
0 comment threads
Ruby, 70 bytes
->s{s+=s.reverse;s[..-2].gsub(/./){t=$&+' '*$.+$/;t[-2]=$&;$.+=1;t}+s}
0 comment threads
Python 3, 196 195 145 102 91 89 87 79 bytes
x=input();x+=x[::-1];i=-1
for c in x[:-1]:print(c*(i>-1)+" "*i+c);i+=1
print(x)
Golfed 50 bytes thanks to @celtschk's advice. Golfed another 43 bytes thanks to @celtschk's advice. Golfed another 11 bytes thanks to @celtschk's advice. Golfed another 2 bytes thanks to @celtschk's advice. Golfed 2 bytes thanks to @Moshi's advice. Golfed another 8 bytes thanks to @Moshi's advice.
0 comment threads