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*

Break tabs in half

Holy wars have been fought over spaces vs. tabs. (And of course spaces, being objectively superior, won.) —Alex A.

Some people still refuse to accept that which is clearly supreme. You've just received a file using the incorrect, bad, and inferior form of whitespace, and now the contents of the file are tainted and ruined.

You decide you might as well show the person who sent the file to you just how wrong they are—violently.

Description

As the title suggests, your challenge is to take a file that contains one or more tabs:

this is an evil tab onoes

and ruthlessly shatter them into pieces:

this is an evil tab
 o
 n
 o
 e
 s

Note that the Stack Exchange software turns literal tabs into four spaces (because it's right), so tabs within this post will be displayed as four spaces. The input to your program, however, will contain actual tabs.

Challenge

The solution should take a single string as input, which may contain printable ASCII, newlines, and tabs. There will always be at least a single tab in the input.

The output should be the same string, with the following rules applied:

  • Start the cursor at coordinates (0,0) and with a direction of right. The coordinates are (column,row), zero-indexed, and the direction is which way you should move the cursor after printing a character.

  • For each character in the string:

    • If it's a newline, move to coordinates (0,n), where n is the number of newlines in the string so far (including this one), and reset the direction to right.

    • If it's a tab, output two spaces, rotate the cursor direction 90 degrees clockwise, and output two more spaces, effectively "breaking" the tab in half. Here's a visual example, where a tab is represented as ---> and spaces as ·:

      foo--->bar--->baz
      

      becomes

      foo···
       ·
       b
       a
       r
       ·
       ·
       zab··
      
    • Otherwise, simply output the character at the cursor and move the cursor one step in the current direction.

Since you are reading the string from start to end, it is possible that you will have to write "on top" of existing characters—this is okay. For example, the input

foo--->bar
spaces are superior

should result in an output of

foo
 b
spaces are superior
 r

You may choose whether "broken tabs" should overwrite other characters—the original intention was that they do not, but the spec was ambiguous, so this is your decision.

Furthermore, after applying these rules, you may also

  • add or remove as many trailing spaces as you would like.

  • add a maximum of a single trailing newline.

The input will never contain trailing spaces; it will also never contain leading or trailing newlines. You may also always assume that you will never need to write to a column or a row less than 0 (i.e. off the screen).

Test case

Tabs in this test case are represented as ---> because otherwise SE gobbles them up.

Input:

Test case. Here's a tab--->there's a tab--->everywhere a tab--->tab--->this is some more text
blah
blah
blah blah blah blah blah blah--->blaah--->blaah--->blah--->blaaaaah--->blah--->blah--->blah--->blah--->blah

Output:

Test case. Here's a tab
blah
blah t
blah blah blah blah blah blah
 blaablah
 r b
 e l b
 h 'h a l
 a sa a a
 l l h h
 this is some mobe tbxt
 haalhalb
 b a
 a b
 t
 bat a erehwyreve

Fancy animation:

Rules

  • This is , so the shortest code in bytes will win!

Answer*

Draft saved
Draft discarded
Cancel
5
  • \$\begingroup\$ Hint: if !d and if d>2 \$\endgroup\$ Commented Sep 21, 2015 at 21:39
  • \$\begingroup\$ !d is not valid syntax. @sanchises Thanks for the d>2 tip, though. \$\endgroup\$ Commented Sep 21, 2015 at 21:42
  • \$\begingroup\$ Sorry, I don't actually know python :) Just sort of assumed it would work like that. \$\endgroup\$ Commented Sep 21, 2015 at 21:46
  • \$\begingroup\$ I too don't understand python, but if d in 0...3, d==0 -> d<1 \$\endgroup\$ Commented Sep 21, 2015 at 22:06
  • \$\begingroup\$ Yep, you're right. Thanks for the byte. @edc65 \$\endgroup\$ Commented Sep 21, 2015 at 22:10

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