Your task is to write a program (or function) that will take a string as input and append text to the beginning of every line that satisfy the following rules:
- The text appended must contain the line number. You may use 0- or 1-based indexing.
- The text must be a comment in your language. A comment is defined as a syntacticly valid piece of code that makes no changes to the program's state. Removing the comment should make no change to the program.
- The comment may only contain newlines at the end of it.
- You do not have to put line numbers for lines that contain just whitespace and comments.
Specifications
- You may assume that the input will be a valid program in your language.
- In indentation-sensitive languages like Python, you may place the line number comment after all indentation. You may choose either tabs or spaces to be your indentation character, but you must specify on your post.
- You may assume that every statement in the code will take up at most the entirety of 1 line; i.e. no multi-line strings or backslash-continuation.
Test Cases
Python:
#0
a = int(input())
#1
while a:
#2
print(a)
C: (Honestly, I'm surprised this compiles)
/*0 */#include <stdio.h>
/*1 */int main()
/*2 */{
/*3 */ char c;
/*4 */ c = getchar();
/*5 */ do
/*6 */ {
/*7 */ putchar(c);
/*8 */ }
/*9 */ while (c);
/*10*/}
This is code-golf, so the shortest answer (in bytes) wins.
AssertionError
unless the stack trace contained the phrasefoo.py
, which was expected to come from a file name but could also appear as a line comment in the context. \$\endgroup\$ – wchargin Nov 21 '16 at 3:09