23
\$\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\$
  • \$\begingroup\$ May we assume the input is rectangular, and has trailing whitespace? \$\endgroup\$ – orlp Jul 17 '15 at 5:09
  • \$\begingroup\$ @orlp No. You can assume a trailing newline, but it's not necessarily rectangular. \$\endgroup\$ – Calvin's Hobbies Jul 17 '15 at 6:52
4
\$\begingroup\$

CJam, 35 bytes

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

Try it online here

\$\endgroup\$
  • \$\begingroup\$ I had almost exactly the same code, but without that clever $ trick, it was still 37 bytes long... \$\endgroup\$ – Dennis Jul 17 '15 at 18:06
  • \$\begingroup\$ That $ trick has saved my life a lot of times already ;) \$\endgroup\$ – Optimizer Jul 17 '15 at 18:16
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\$
2
\$\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\$
  • \$\begingroup\$ +1 This is the solution I saw in my head when I read the question, shouldn't be surprised it was already here :P \$\endgroup\$ – Kade Jul 17 '15 at 19:46
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\$
0
\$\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\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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