33
\$\begingroup\$

This question already has an answer here:

Sometimes when you're lying in bed and reading a message, your phone screen will pop into landscape mode right in the middle of a sentence. Only being able to read left to right, you find yourself incapacitated, unable to process the text in front of you.

To ensure that this won't happen again, you decide to make every message readable from any angle, whether your phone screen is rotated or mirrored. To make this happen, each message is printed as a square, with each side of the square containing the message, either in the original order or in reverse.

  • For backwards compatibility, the top side of the square should be the original message.

  • To make each message square as compact as possible, the first and last character of the message should be a part of two sides of the square. This means that the top side reads normally, the bottom side is in reverse, the left side reads top-bottom, and the right side reads bottom-top.

Input

A single string, with 2 or more characters. You should not assume that the string only contains alphanumerical characters or similar.

Output

The Squarification™ of the string. It is permissible to leave some whitespace at the end of each line, and a single newline at the end of the output.

Examples

Input: 'ab'
ab
ba

Input: 'abc'

abc
b b
cba

Input: 'Hello, world!'
Hello, world!
e           d
l           l
l           r
o           o
,           w

w           ,
o           o
r           l
l           l
d           e
!dlrow ,olleH

This challenge looks like A cube of text, but I'm hoping that it's different enough that there will be some clever answers.

As this is code-golf, get ready to trim some bytes!

\$\endgroup\$

marked as duplicate by Mr. Xcoder code-golf Oct 19 '18 at 22:08

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • \$\begingroup\$ Can the horizontal lines contain spaces between letters, as long as they align with margins? Example: the first line of output for abc would be a b c. Alternatively, can we output a list of lines? Nice challenge, btw \$\endgroup\$ – Mr. Xcoder Oct 8 '18 at 6:30
  • \$\begingroup\$ @Mr.Xcoder You may not have spaces in the middle of a line, so abc is always abc. EDIT: outputting a list of lines is allowed, just make sure that you have the joining in the footer so that answers can be verified easily. \$\endgroup\$ – maxb Oct 8 '18 at 6:52
  • \$\begingroup\$ Can we return a list of lines? \$\endgroup\$ – Jo King Oct 8 '18 at 6:56
  • \$\begingroup\$ @JoKing Yes, a list of lines is allowed. \$\endgroup\$ – maxb Oct 8 '18 at 8:02
  • 1
    \$\begingroup\$ "To ensure that this won't happen again, you decide to make every message readable from any angle, whether your phone screen is rotated or mirrored." - Or you could just, you know, enable the orientation lock for your phone... \$\endgroup\$ – Arnav Borborah Oct 8 '18 at 11:13

37 Answers 37

1
\$\begingroup\$

APL (Dyalog Classic), 29 bytes

⊢⍪⌽⍪⍨1⌽⊢∘≢↑⍤1(↑⌽⍪¨⊢)∘(1↓¯1↓⊢)

Try it online!

Uses the same algorithm as my J solution.

\$\endgroup\$
1
\$\begingroup\$

C++, 232 231 bytes

Edit: included the include statements in byte count, removed all white spaces, and removed include string.h, Thanks Dennis

Try it online!

#include <iostream>
#include <algorithm>
using namespace std;main(int c,char **a){string s=a[1];int x=s.length()-1,i(1);cout<<s<<endl;for(;i<x;++i)cout<<s[i]<<string(x-1,' ')<<s[x-i]<<endl;reverse(s.begin(),s.end());cout<<s<<endl;}
\$\endgroup\$
  • 1
    \$\begingroup\$ Welcome to the site. Indentation and most newlines are not necessary for C++, you can remove them to save bytes. \$\endgroup\$ – Sriotchilism O'Zaic Oct 8 '18 at 15:51
  • \$\begingroup\$ Welcome to PPCG! You can save a lot of bytes by eliminating whitespace. You should be able to save more byte by submitting a function instead of a full program. However, you do have to add #include<iostream> and #include<algorithm> to your byte count. #include<string.h> doesn't seem to be required. \$\endgroup\$ – Dennis Oct 8 '18 at 15:59
  • \$\begingroup\$ Just a few more (and correct TIO link): Try it online! \$\endgroup\$ – Erik the Outgolfer Oct 9 '18 at 11:02
  • 1
    \$\begingroup\$ 222 bytes \$\endgroup\$ – ceilingcat Oct 10 '18 at 1:30
  • 1
    \$\begingroup\$ You can most likely drop the last <<endl. \$\endgroup\$ – Jonathan Frech Oct 10 '18 at 5:53
1
\$\begingroup\$

T-SQL, 179 bytes

DECLARE @ varchar(99),@n INT=2SELECT @=v FROM i PRINT @
a:PRINT SUBSTRING(@,@n,1)+SPACE(LEN(@)-2)+SUBSTRING(REVERSE(@),@n,1)SET @n+=1IF @n<len(@)GOTO a
IF LEN(@)>2PRINT REVERSE(@)

Input is taken via pre-existing table i with varchar field v, per our IO standards.

\$\endgroup\$
1
\$\begingroup\$

Japt, 19 18 bytes

Returns an array of lines.

¬íU¬w)mq¢ç)hJUw)hU

Try it

Saved a byte thanks to Oliver.

\$\endgroup\$
  • \$\begingroup\$ You can replace with ¢ \$\endgroup\$ – Oliver Oct 9 '18 at 18:35
  • \$\begingroup\$ Oh, yeah. Thanks, @Oliver; I'd forgotten about ¢, can't remember the last time I used it. \$\endgroup\$ – Shaggy Oct 9 '18 at 19:03
1
\$\begingroup\$

VBA (Excel), 86 bytes

Using Immediate Window and Cell [a1] as input.

a=[a1]:b=StrReverse(a):c=Len(a):?a:For i=2To c-1:?Mid(a,i,1)Spc(c-2)Mid(b,i,1):Next:?b
\$\endgroup\$
1
\$\begingroup\$

J, 34, 31 30 bytes

,(1|:@|.(2+#){.|.,:])@}.@}:,|.

Try it online!

Explanation:

 ,(1|:@|.(2+#){.|.,:])@}.@}:,|. 
                            ,   - append
                             |. - the reversed input to
  (                  )@}.@}:    - the input with the first and the last chars dropped and
                |.              - its reverse
                  ,.            - laminated to
                    ]           - the trimmed input (results in a matrix 2 x length )
              {.                - take as many rows (pad with empty lines)
         (2+#)                  - as is length of the input (2+length of the trimmed input)
       |.                       - rotate
   1                            - one position to the top
    |:@                         - and transpose
 '                              - prepend with the input 
\$\endgroup\$
  • 1
    \$\begingroup\$ (1j1&#"1,(1|."1(3++:@#){."1|.,.])@}.@}:,1j1&#"1@|.) prints in squared format \$\endgroup\$ – user58120 Oct 9 '18 at 7:24
1
\$\begingroup\$

V, 51 bytes

Ù:se ri
y$2o"mm2kÓ./&ò
dk2GddG:&&
dd'mdk$ëp'mdG

Try it online!

00000000: d93a 7365 2072 690a 7924 326f 1222 1b6d  .:se ri.y$2o.".m
00000010: 6d32 6bd3 2e2f 26f2 0a64 6b32 4764 6447  m2k../&..dk2GddG
00000020: 3a26 260a 6464 1627 6d64 6b24 16eb 7027  :&&.dd.'mdk$..p'
00000030: 6d64 47                                  mdG
Ù                                Duplicate line
 :se ri                          Enable reverse input.
y$                               Copy one line without line-break
  2o<C-r>"<Esc>                  With reverse input enabled, insert copied string in a new line twice
               mm                Set a mark in the last line
                 2kÓ./&ò         Go to second line, add a line break between each character
                                 This created the left edge of the square
dk2Gdd                           Delete first and last character of left edge
      G:&&                       Go to last line, repeat last substitution; this creates the right edge
dd                               Delete the final empty line that was just created
  <C-v>'md                       The right edge is currently below the square; delete it there
          k$<C-v>ëp              And put it in the right place
                   'mdG          Finally, remove excess new lines
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.