26
3
Space indentation users, unite! We must fight against all the lowly tab users!
Your mission (should you choose to accept it) is to write a program or function that takes in two arguments:
- A string: This is the input.
- A positive integer: This the number of spaces per tab.
You must go through every line of the string and replace every tab used for indentation with the given number of spaces, and every tab not used for indentation (e.g. in the middle of a line) with one space.
Note that lines such as \t \tabc
are undefined behavior; they were inserted by the evil tab users to complicate your programs.
According to the Tabs Must Die Society, your program must be as short as possible to avoid detection by the evil tab users.
Example
\t
is used to represent tabs here.
Input string:
a
\t\tb\tc
d
Input number:
4
Output:
a
b c
d
The middle line was indented by 8 spaces, 4 per tab (since the given number was 4).
Input string:
\ta\t\tb
Input number:
4
Output:
a b
NOTE: This is not a duplicate of the tab expansion challenge; it requires a very different input format and slightly different requirements.
The given output only has 4 spaces instead of 8. Typo? – Conor O'Brien – 2015-09-09T21:21:24.980
@CONORO'BRIEN Yep. Fixed. – kirbyfan64sos – 2015-09-09T21:22:49.450
Can input number be unary for retina ? – TheNumberOne – 2015-09-09T21:26:59.097
1Yes, as long as the question doesn't explicitly ask for decimal numbers (which it doesn't). – Martin Ender – 2015-09-09T21:34:18.430
As said a long time ago on some deleted comments, on one of my questions, different input doesn't make enough difference. The answers on the other question can be slightly modified to work on this one. I'm sorry, but I'm voting to close this one as duplicate. Like or hate it. – Ismael Miguel – 2015-09-09T22:43:25.743
2
possible duplicate of Expand tabs (implement expand(1))
– Ismael Miguel – 2015-09-09T22:43:44.273Suggested alternate title: My God, it's full of tabs! – Nate Eldredge – 2015-09-10T01:53:26.200
1Can we assume that the input contains only printable ASCII, tabs and newlines? – Dennis – 2015-09-10T02:27:17.577
1@Dennis Sure!!! – kirbyfan64sos – 2015-09-10T02:27:36.723
2Proposed test case:
\ta\t\tb
,4
(my previous revision was failing that one) – Dennis – 2015-09-10T04:52:48.940Can a line only consists of tabs? Or is this an invalid input? – Jakube – 2015-09-10T09:34:07.683
Does the input string contain actual
\t
(two characters), or the ASCII code 9 (horizontal tab), or at our convenience? – Luis Mendo – 2015-09-10T11:25:32.7071@IsmaelMiguel This is very different from that challenge. The linked challenge involves calculating tabstops; this is simply "tab = 4 spaces." – Doorknob – 2015-09-10T11:36:26.733
@Doorknob Did the rules changed? – Ismael Miguel – 2015-09-10T12:04:29.497
1@LuisMendo The literal ASCII code 9. – kirbyfan64sos – 2015-09-10T13:55:38.383
@Jakube That's valid input. – kirbyfan64sos – 2015-09-10T13:56:07.303
How come in the first test case, the output of the second line is
(8 spaces)b c
and not(8 spaces)b c
(since "b" and "c" had a tab in between them?) – ASCIIThenANSI – 2015-09-10T14:00:33.233@ASCIIThenANSI If you mean why there weren't 4 spaces in between
b
andc
, that's because the rules say tabs that aren't indentation are always replaced by 1 space. – kirbyfan64sos – 2015-09-10T14:02:22.0771@kirbyfan64sos Such a strange rule, since that's not how tabs work. They should be replaced by a number of spaces such that the first character after the tabs lands on a multiple of the input number. – mbomb007 – 2015-09-10T15:57:28.830
@mbomb007 But then this challenge would be too similar to the other tabstop one. Besides, I'm thinking of the scenarios like the way Plan 9 uses tabs (
#ifdef\tX
instead of#ifdef X
). – kirbyfan64sos – 2015-09-10T16:18:11.240@kirbyfan64sos What should the result be in this example:
\t \tHello
? Should the second tab be replaced with a single space? – mbomb007 – 2015-09-10T17:37:44.967@mbomb007 I would say no, since that's technically indentation, but then I'd probably invalidate half the answers here, so I'll count is as "undefined behavior". :) – kirbyfan64sos – 2015-09-10T17:45:26.057
1-1 because tab indentation is better :P (no downvote was actually made) – HyperNeutrino – 2015-09-11T02:12:04.600
@JamesSmith The words
-1 because
are all that appeared in my Stack Exchange Android notifications, so I had momentarily freaked out. :) – kirbyfan64sos – 2015-09-11T02:44:28.8532We need an answer in Whitespace. – Kaz Wolfe – 2015-09-11T03:09:35.693
1This is truly evil. – user – 2015-09-14T14:20:14.080
2 space indent master race – Stan Strum – 2018-06-12T18:31:37.247