6
Task
Your mission is to create a program that reviews Python code. The solution can be written in any language!
It must take input that contains python code. The program must add # This is not pythonic!
before each block of non-whitespace lines.
Input
- The data can be entered via stdin.
- The data will contain at least 3 lines with one blank one in between them.
Minimum input example.
print "Hello World!"
Print "Bye World!"
Sample Input
from os.path import abspath
lines = []
with open(__file__, 'r') as f:
for line in f:
lines.append(line)
if lines is not None:
newLines = []
hasReviewedBlock = False
for line in lines:
if len(line) > 1: # \n
if hasReviewedBlock == False:
newLines.append("# This is not pythonic!\n")
hasReviewedBlock = True
else:
hasReviewedBlock = False
newLines.append(line)
with open("d:/test.py", 'w') as f:
for line in newLines:
f.write(line);
Output
Output has to be written to STDOUT or closest alternative.
Sample Output
# This is not pythonic!
from os.path import abspath
# This is not pythonic!
lines = []
with open(__file__, 'r') as f:
for line in f:
lines.append(line)
# This is not pythonic!
if lines is not None:
# This is not pythonic!
newLines = []
hasReviewedBlock = False
# This is not pythonic!
for line in lines:
if len(line) > 1: # \n
if hasReviewedBlock == False:
newLines.append("# This is not pythonic!\n")
hasReviewedBlock = True
else:
hasReviewedBlock = False
# This is not pythonic!
newLines.append(line)
with open("d:/test.py", 'w') as f:
for line in newLines:
f.write(line);
Scoring:
Lowest number of bytes wins. -5 bytes if your program can be used as valid input.
3You haven't explained how you decide if code is pythonic or not. – Blue – 2015-09-12T13:59:20.390
2All python code is not Pythonic. Edited my question. – marsh – 2015-09-12T14:03:26.167
So what you're saying is that python itself is not pythonic? – Blue – 2015-09-12T14:04:02.093
5So, simply put, we have to insert "# this is not pythonic" before each block of non-whitespace lines? We're all for a joke here, but please make your spec crystal clear. – Level River St – 2015-09-12T14:08:15.403
Yes, sorry I am not sure how to make it more clear. I have not made a code golf question before. – marsh – 2015-09-12T14:12:39.690
1I don't quite understand the bonus. What does "can be used as valid input" mean? I figure pretty much any program in any language can be used as an input. What makes it valid? – Reto Koradi – 2015-09-12T15:05:57.773
2Couple more questions: Can the first line be empty? Can there be sequences of multiple empty lines? – Reto Koradi – 2015-09-12T15:15:40.210
@RetoKoradi You can only put a blank line at the second line of code when the minimum is a 3LOC input with a line in "between". In this case, the first line can never be empty. – mınxomaτ – 2015-09-12T16:06:49.533
Are functions allowed? – Downgoat – 2015-09-12T16:20:47.510
Functions are allowed, the first line can not be empty. Valid input is python code. So basically to get the bonus it has to be in python, and have 2 code blocks. – marsh – 2015-09-12T23:54:01.017
whitespace
\t
is\t
EVIL! – Max – 2015-09-17T08:06:05.610