Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs

Dashboard
Notifications
Mark all as read
Challenges

Word Set Square

+6
−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?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

10 answers

+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?
You might want to add some details to your flag.

0 comment threads

+3
−0

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=' '

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

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

Vyxal J, 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

Japt -R, 16 15 bytes

ê1
¬ËúEÄ hJDÃÆU

Try it

ê1\n¬ËúEÄ hJDÃÆU     :Implicit input of string U
ê1                   :Mirror
  \n                 :Reassign to variable U
    ¬                :Split
     Ë               :Map each D at 0-based index E
      ú              :  Left pad with spaces to length
       EÄ            :    E+1
          h          :  Replace the character at index ...
           J         :  -1 (i.e., the last) with ...
            D        :  D
             Ã       :End map
              ÆU     :Modify the last element and return U
                     :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)
+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?
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?
You might want to add some details to your flag.

0 comment threads

+1
−0

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)

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?
You might want to add some details to your flag.

4 comment threads

Sub 80 (1 comment)
bool(i) -> (i>0) (1 comment)
Further golfing opportunity (5 comments)
You can save quite a few bytes here. First, you can replace `print(" ",end='')` with `print(end=" ... (1 comment)

Sign up to answer this question »

This community is part of the Codidact network. We have other communities too — take a look!

You can also join us in chat!

Want to advertise this community? Use our templates!