12
2
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.
1Good luck doing this is Retina! – Esolanging Fruit – 2016-11-20T22:20:45.270
Just to be clear, we must generate the type of comment our language uses? What if our language doesn't have comments? – James – 2016-11-20T22:25:14.253
7Making the comments language-dependent makes things complicated. You already had to create a special rule for Python. What about all the other languages that don't have multi-line comments? What about languages that do not have comments at all. What about multi-line strings where comments cannot be placed without side effects? – Dennis – 2016-11-20T22:26:03.580
@Dennis You don't strictly need "comments". For example in befunge I consider
"text"!!!!
to be a comment because it makes no changes to the program's state. The thing about multi-line strings is true; I'll mention that. – Esolanging Fruit – 2016-11-20T22:33:20.467@DrMcMoylex See my reply to Dennis about comments. – Esolanging Fruit – 2016-11-20T22:35:16.090
4Fun fact: the rule "removing the comment should make no change to the program" immediately disqualifies any Python answer, because the code—including comments—can be introspected at runtime. I have seen this in play in a production system: a piece of code would raise an
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. – wchargin – 2016-11-21T03:09:02.800@wchargin Hmmm... Let's just say that the program does not directly access its source code or runtime state like that. Also, does that mean that comments are not removed during parsing? Seriously, Python? – Esolanging Fruit – 2016-11-21T03:11:37.297
1
@Challenger5 Correct.
– wchargin – 2016-11-21T03:18:13.3702"The comment can only contain newlines at the end of it" is inconsistent with the C "test case". – Peter Taylor – 2016-11-21T06:41:28.227
1@PeterTaylor Can, not necessarily will. – Esolanging Fruit – 2016-11-21T07:10:08.880
2What about langs without comments? – NoOneIsHere – 2016-11-21T07:58:34.460
"Can only" generally means "cannot not", hence "must". But if it should be "may only" that's still inconsistent with the C test case, which includes padding whitespace. – Peter Taylor – 2016-11-21T08:23:19.220
@PeterTaylor That's true. I edited it. – Esolanging Fruit – 2016-11-21T09:48:37.890
@wchargin I just don't see why that would ever be useful. Is it ever necessary to detect if a comment exists or contains some text? – Esolanging Fruit – 2016-11-21T09:52:12.590
@Challenger5 In Python, variables can be given a type hint with a comment. You could read the comment to work out the type of he variable and enforce type hints – Blue – 2016-11-21T12:11:11.687
Are we allowed to comment blank lines? – anonymous2 – 2016-11-21T12:49:23.107
4The edit doesn't fix the inconsistency. – Peter Taylor – 2016-11-21T13:38:18.927
@NoOneIsHere A comment is defined here as a block of code that has no effect. For example
"comment"!!!!!!!
is considered a comment if Befunge. – Esolanging Fruit – 2016-11-22T15:08:43.540@Challenger5 So
123
is a comment? What about BF?<>
(from my memory) is a comment? – NoOneIsHere – 2016-11-22T16:36:46.757@NoOneIsHere If your language does not do anything with
123
, it is a comment.<>
is also considered a comment in BF. – Esolanging Fruit – 2016-11-22T23:00:22.273Test case needed for rule You do not have to put line numbers for lines that contain just whitespace and comments. – Denis Ibaev – 2016-11-28T07:13:50.797