5
\$\begingroup\$

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.

\$\endgroup\$
  • 3
    \$\begingroup\$ You haven't explained how you decide if code is pythonic or not. \$\endgroup\$ – Blue Sep 12 '15 at 13:59
  • 1
    \$\begingroup\$ All python code is not Pythonic. Edited my question. \$\endgroup\$ – marsh Sep 12 '15 at 14:03
  • 5
    \$\begingroup\$ So, 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. \$\endgroup\$ – Level River St Sep 12 '15 at 14:08
  • 1
    \$\begingroup\$ I 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? \$\endgroup\$ – Reto Koradi Sep 12 '15 at 15:05
  • 2
    \$\begingroup\$ Couple more questions: Can the first line be empty? Can there be sequences of multiple empty lines? \$\endgroup\$ – Reto Koradi Sep 12 '15 at 15:15
3
\$\begingroup\$

awk, 64 56 54 bytes

/^$/{i=0}/./{if(!i++)print"# This is not pythonic!"}1

Test:

echo '/^$/{i=0}/./{if(!i++)print"# This is not pythonic!"}1' > solution.awk
awk -f solution.awk < pyth.py
awk -f solution.awk < pyth.py

Explanation:

/^$/ - matches empty line,

/./ - matches non-empty line,

1 - matches each line. No body is equivalent to {print;} - each line of input is printed.

\$\endgroup\$
2
\$\begingroup\$

AutoIt - 139

$1='# This is not pythonic!'
ConsoleWrite($1&@CRLF&StringRegExpReplace(FileRead($CmdLine[1]),'(*BSR_ANYCRLF)(^\R|\R(?=\R))',"\0"&@CRLF&$1))

I'm assuming the first line cannot be empty as by your specification:

The data will contain at least 3 lines with one blank one in between them.

This program is called like this:

compiled.exe pycode.py > output.txt

The RegEx can be optimized for sure. Works for CRLF only.

\$\endgroup\$
2
\$\begingroup\$

Pyth, 39 38 37 bytes

tjbm|d+b"# This is not pythonic!"+k.z

Live demo.

Explanation

tjbm|d+b"# This is not pythonic!"+k.z
                                 +k.z    Prepend the empty line to the string
   m                                     For each line
    |d                                   If it's non-empty, return the same line
      +b"# This is not pythonic!"        Else, return a line break followed by the required text
 jb                                      Join via line breaks
t                                        Remove the unneeded preceding line break

38-byte version

tjbm?dd+b"# This is not pythonic!"+k.z

39-byte version

jbtsm?dd,d"# This is not pythonic!"+k.z

No doubt there's still golfing left to do.

\$\endgroup\$
1
\$\begingroup\$

CJam, 41 bytes

"# This is not pythonic!":TNqN/{_{T}|}%N*

Try it online

Alternate solution that handles multiple empty lines in succession, as well as empty lines at the start. This is 44 bytes, and will become my solution depending on how the pending clarifications turn out:

qN/{_L!e&{"# This is not pythonic!"\}&:L}%N*
\$\endgroup\$
1
\$\begingroup\$

JavaScript (ES6), 70

Test running the snippet below.

f=p=>alert((r='# This is not pythonic!\n')+p.replace(/\n\n+/g,x=>x+r))

//// TEST CASES /////

t1=`print "Hello World!"

Print "Bye World!"`
f(t1)

t2=`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);`
f(t2)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.