29
\$\begingroup\$

Write a program that takes in a string containing only spaces, newlines, and angle brackets: <, > (chevrons). Output a string of spaces, newlines, and slashes: /, \ (soliduses) whose shapes correspond to the input, but rotated a quarter turn clockwise, with a column of spaces inserted between each row of the original input (for aesthetics).

For example, if the input is this:

<>

The output would be this:

/\
\/

If the input is this:

><<<>
 <><

The output would be this:

   \/
/\ /\
\/ /\
/\ /\
   \/

If the input is this:

>>  <<
<>  <>
  <
  ><

The output would be this:

      /\ \/
      \/ \/
\/ /\    
/\      
      /\ /\
      \/ /\

Notice how there is a single column of spaces between the original input rows in the last two examples.

You may write a full program that takes the input in any usual way (command line, stdin) and prints the output, or you may write a function with a string argument, that prints or returns the output.

Empty leading and trailing rows or columns of whitespace in the input do not need to be present in the output. Additionally, there may be any amount of leading and/or trailing spaces and/or newlines in the output, in any locations, as long as the resulting shapes are correct. In other words, the translation of the ascii art does not matter, only the shapes and their relation to one another do.

You may optionally assume the input has a trailing newline.

The shortest code in bytes wins.

\$\endgroup\$
2
  • \$\begingroup\$ May we assume the input is rectangular, and has trailing whitespace? \$\endgroup\$
    – orlp
    Commented Jul 17, 2015 at 5:09
  • 1
    \$\begingroup\$ @orlp No. You can assume a trailing newline, but it's not necessarily rectangular. \$\endgroup\$ Commented Jul 17, 2015 at 6:52

10 Answers 10

6
\$\begingroup\$

CJam, 35 bytes

qN/_s,S*f+{iD%[S3*" \/"_$]=}f%W%zN*

Try it online here

\$\endgroup\$
2
  • 1
    \$\begingroup\$ I had almost exactly the same code, but without that clever $ trick, it was still 37 bytes long... \$\endgroup\$
    – Dennis
    Commented Jul 17, 2015 at 18:06
  • \$\begingroup\$ That $ trick has saved my life a lot of times already ;) \$\endgroup\$
    – Optimizer
    Commented Jul 17, 2015 at 18:16
6
\$\begingroup\$

Python 2, 105 bytes

def f(s):
 for row in map(None,*s.split("\n")):print" ".join("\/ /\ "[1-cmp(c,"<")::3]for c in row[::-1])

For all the wrong reasons, this has got to be one of the nicest uses of map(None, ...) I've had so far. The output even pads to perfect rectangle.

Let's take the second example:

><<<>
 <><

map(None,*s.split("\n")) performs a poor man's zip_longest, giving:

[('>', ' '), ('<', '<'), ('<', '>'), ('<', '<'), ('>', None)]

Notice how the second line is shorter than the first, so we get a None at the end. Normally this would be a problem, but for some reason almost everything is comparable in Python 2, and in particular

>>> None < ""
True

This means that the expression 1-cmp(c,"<") returns 0, 1, 2 for ">", "<", None respectively, allowing us to use the string slicing trick to extract one of "\/", "/\", " ". Using this, we print the output line by line, joining the 2-char groups with spaces.

\$\endgroup\$
2
5
\$\begingroup\$

Jelly, 19 bytes

Ỵz⁶UµOHØ^ṙaf⁶;`$)€G

Try it online!

While I was writing this, it tormented me to no end that the whole mess with af⁶;`$ could just be «, if the codepoint of backslash were somewhere closer to the ballpark of its forward companion. This can definitely be golfed further.

Ỵ                       Split the input on newlines.
 z⁶                     Transpose with space as filler.
   U                    Reverse each row.
    µ            )€     For each character in each row:
       Ø^ṙ              rotate "/\" by
      H                 half of
     O                  the character's codepoint;
          a             vectorizing logical and with
             ;          concatenate
           f⁶  $        the character filtered to spaces
              `         to itself.
          a             (The slashes are unaffected if the filter removes everything.)
                   G    Grid format: this builtin can do a lot of things,
                        but in this case it joins each row on spaces 
                        then joins the rows on newlines.
\$\endgroup\$
3
\$\begingroup\$

CJam, 37 bytes

qN/_z,S*f{+"< >""/\    \/ "3/er}W%zN*

Try it online in the CJam interpreter.

How it works

qN/                                   e# Read from STDIN and split at linefeeds.
   _z,                                e# Zip a copy and push the results length.
                                      e# This computes the maximum line length.
      S*                              e# Repeat " " that many times.
        f{                     }      e# For each line:
                                      e#   Push the string of spaces.
          +                           e#   Append it to the line.

           "< >""/\    \/ "3/         e#   Push "< >" and ["/\ " "   " "\/ "].
                             er       e#   Perform transliteration.
                                W%z   e# Reverse the lines and zip.
                                      e# This rotates by 90 degrees.
                                   N* e# Join, separating by linefeeds.  
\$\endgroup\$
3
\$\begingroup\$

Vyxal, 15 bytes

øṀð+Ṙ∩øɽ⁋‛<>k/Ŀ

Try it Online!

øṀ              # Mirror each char vertically - "<" -> "<>", ">" -> "><", " " -> "  "
  ð+            # Append a space to each
    Ṙ∩          # Reverse 
      øɽ⁋       # Right align and join by newlines
         ‛<>k/Ŀ # Replace "<>" with "/\"
\$\endgroup\$
4
  • \$\begingroup\$ "You can assume a trailing newline, but it's not necessarily rectangular." \$\endgroup\$ Commented Jun 20, 2021 at 9:23
  • 1
    \$\begingroup\$ @UnrelatedString Ok, the second one then. \$\endgroup\$
    – emanresu A
    Commented Jun 20, 2021 at 9:26
  • 1
    \$\begingroup\$ vyxal jar! vyxal jar! \$\endgroup\$
    – lyxal
    Commented Jun 29, 2021 at 6:04
  • \$\begingroup\$ @lyxal I'm glad you approve. \$\endgroup\$
    – emanresu A
    Commented Jun 29, 2021 at 6:12
2
\$\begingroup\$

Husk, 30 bytes

mȯσ" ""  "σ"<""/¦"σ">""¦/"T' ↔

Try it online!

I made the clunkiest possible solution.

\$\endgroup\$
2
\$\begingroup\$

Stax, 19 bytes

Ǻòî@f┬U☺≥•:Γ_ƒ←⌠Φô

Run and debug it

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

Scala, 201 188 180 characters

(s:String)⇒(Seq("")→0/:s.lines.flatMap(l⇒Seq(l,l))){case((v,i),l)⇒(l.map(c⇒if(Set('>','<')(c))if(c%4==i)'/'else'\\'else c)+:v,2-i)}._1.init.transpose.map(_.mkString).mkString("\n")

note:

this only works if the provided string has all lines with equal length (i.e. padded with spaces)

explanation:

I'm using fold with initial value of tuple of a Seq[String] and an Int (instead of writing Seq.empty[String] im writing the shorter Seq("") and .init after the fold), the fold operates on a collection of strings, each string is a line in the original input, and every line is doubled. the trick here was to test for modulo of the char. since '<' value is 60, and '>' value is 62, testing for modulo 4, will yield 0 or 2. that's why the fold also carry a flipping Int set to 0. and flipped between 0 and 2 with 2-i. every odd line should map '>' to '/' and '<' to '\\', and every even line should map '>' to '\\' and '<' to '/'. this is why i test for c%4==i and hit 2 birds with 1 stone. the fold "rebuilds" the initial sequence of strings in reverse, and then (after dropping the last line), I transpose the sequence (this is why all strings must be of exact same length). because of the implicits involved, I need to _.mkString on each line (previously column), and then mkString("\n") for the final output.

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

Perl - 119

@l=map[/./g],reverse<>;do{print;$_=join(' ',map({'<'=>'/\\','>'=>'\/'}->{$_->[$b]}||'  ',@l))."\n";$b++;}while(/[^ 
]/)

First, @l is assigned as a list of lists representing the characters on each line of input with lines in reverse order. Then it loops through the columns of characters, replacing the angle brackets with the corresponding slashes, joining the elements with spaces, and printing the joined slashes as a line.

\$\endgroup\$
0
\$\begingroup\$

Perl 5 -lF, 99 bytes

push@;,[@F]}{map{//;say map$$_[$']eq'<'?'/\\ ':$$_[$']eq'>'?'\\/ ':'   ',reverse@;}0..max map$#$_,@

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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