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
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
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.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

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

+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)*'
'}
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+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

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+3
−0

Japt -R, 16 15 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
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

General comments (3 comments)
+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!

History
Why does this post require attention from curators or moderators?
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!

History
Why does this post require attention from curators or moderators?
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 $.
History
Why does this post require attention from curators or moderators?
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!

History
Why does this post require attention from curators or moderators?
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
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

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

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−0

Ruby, 70 bytes

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

Try this online!

History
Why does this post require attention from curators or moderators?
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.

History
Why does this post require attention from curators or moderators?
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 »