### Communities

tag:snake search within a tag
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
created:<1w created < 1 week ago
post_type:xxxx type of post
Challenges

# Word Set Square

+7
−0

## 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.

Why does this post require moderator attention?
Why should this post be closed?

+6
−0

# APL (Dyalog Unicode), 23 bytes

Anonymous tacit prefix function. Reuires 0-based indexing (⎕IO←0)

(⊢⍪⍨¯1↓⊢,∘↑-∘⍳∘≢↑¨⊢)⊢,⌽


Try it online!

⌽ 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

Why does this post require moderator attention?

+3
−0

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=' '  Try it online! Why does this post require moderator attention? You might want to add some details to your flag. #### 0 comment threads +3 −0 # Japt-R, 1615 14 bytes pÔ¬Ëú°EDùEÃÆpÔ  Try it 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 Why does this post require moderator attention? You might want to add some details to your flag. #### 1 comment thread General comments (3 comments) +3 −0 # 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}  Try it in Scastie! Why does this post require moderator attention? You might want to add some details to your flag. #### 0 comment threads +3 −0 # V (vim), 23 bytes æw|PòlÄxòjVLkîyllv$r $Ð  Try it online! Hexdump: 00000000: e677 7c50 f26c c478 f26a 564c 6bee 796c .w|P.l.x.jVLk.yl 00000010: 6c76 2472 2024 d0 lv$r $. Why does this post require moderator attention? You might want to add some details to your flag. #### 0 comment threads +2 −0 # VyxalJ, 12 bytes ḣmėƛ÷꘍ṘǏ;⁋?m  Try it Online! ḣ # 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  Why does this post require moderator attention? You might want to add some details to your flag. #### 0 comment threads +2 −0 # JavaScript, 76 bytes Outputs an array of lines. s=>[...s+=[...s].reverse().join].map((c,x)=>s[-~x]?c.padEnd(x)+(x?c:):s)  Try it online! Why does this post require moderator attention? You might want to add some details to your flag. #### 0 comment threads +2 −0 # 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)*'
'}

Why does this post require moderator attention?

+2
−0

# 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.

Why does this post require moderator attention?

+1
−0

# Python 3, 196195145102918987 79 bytes

x=input();x+=x[::-1];i=-1
for c in x[:-1]:print(c*(i>-1)+" "*i+c);i+=1
print(x)


Try it online!

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.

Why does this post require moderator attention?

Sub 80 (1 comment)
bool(i) -> (i>0) (1 comment)
+1
−0

# Ruby, 70 bytes

->s{s+=s.reverse;s[..-2].gsub(/./){t=$&+' '*$.+$/;t[-2]=$&;\$.+=1;t}+s}


Try this online!

Why does this post require moderator attention?