Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Turn a string into a snake

Write a function or program which writes an input string out as a snake.

The snake starts heading to the right. If a / or \ character is reached in the input, the snake changes direction as if the character represented a wall placed diagonally across the path.

That is it changes direction as follows (before -> after):

  • /: right -> up; up -> right; left -> down; down -> left
  • \: right -> down; down -> right; left -> up; up -> left

Every character other than / or \ is output literally.

So given the input sn/ak\e the output is:

ek 
 a
sn

If the snake crosses its own path, the later characters replace earlier ones:

boa/co\ns/t\rictor

sno
trictor
boa

Input

A string of characters from the set [-a-zA-Z_/\]. There may be consecutive direction change characters (/ and \) including of the same type (////). The first character may be a direction change character. There will be at least one non-direction-change character.

Output

Multiple lines of output as described above, in any reasonable format. Extra whitespace is not allowed:

  • Each line must contain the minimal amount of left-padding such that (at least) one line has zero left padding.
  • There should be either no right-padding, or the minimal amount to produce lines of equal length.
  • No extra blank lines are allowed at the start or end. The final new line character is optional.

Scoring

This is code golf. Standard rules apply.

Sample output

/\backwards

sdrawkcab

_oo/o/oOoo\o\oo/O/_

 ooOoo O_
_oo ooo

TigeR\attlE\gg-eateR/ibboN/ose-horneD\warf-beakeD/esertWomA\nacondA\ddeR/aceR/aT\aipan

TigeR Nose-horneD
 a o w
 t b a
 t n b r
 l a ReddA f
 EggpeateRd -
 i c n b
 a e o e
 TaR c a
 a k
 n e
 AmoWtreseD

\Mamba/Nu\mberF\ive

eviF
 r M
 e a
 b m
 m b
 uNa

There/\and\/back

Tdback

Answer*

Draft saved
Draft discarded
Cancel
3
  • 1
    \$\begingroup\$ 28 bytes: εY"//\"yk©di®>·^Võëˆ]Jā≠>s¯Λ. \$\endgroup\$ Commented Apr 11 at 9:34
  • \$\begingroup\$ @Neil Thanks! The "//\" can be ...//\ for an additional -1. :) \$\endgroup\$ Commented Apr 11 at 14:40
  • \$\begingroup\$ What with compressed strings I I didn't feel like checking to see what the most efficient way of coding the string was... \$\endgroup\$ Commented Apr 11 at 15:34

AltStyle によって変換されたページ (->オリジナル) /