Add comments to a Python script and make it a bilingual Python/C++ “program”

12

1

Given the following Python 3 script:

def greet():
    print("Hello, world!")

greet()

Prepend some lines to this text file so that it can be both executed as a Python program as well as compiled and run as a C++ program producing the same output Hello, world! (including the newline at the end):

$ python3 bilingual.py.cpp
Hello, world!
$ g++ bilingual.py.cpp && ./a.out
Hello, world!

The solution will be scored by the count of non-whitespace characters of the entire program, including the Python script:

sed 's/\s//g' bilingual.py.cpp|wc -c

Leon

Posted 2016-11-05T10:58:26.410

Reputation: 307

5In the title you say add comments, however in the body you say you only have to prepend some lines. Which is it? – Post Rock Garf Hunter – 2016-11-05T17:18:18.080

@WheatWizard The title is a hint. If you can solve this by prepending arbitrary lines (non-comments) I will be puzzled. – Leon – 2016-11-05T17:37:18.177

This is a very nice question. My only remark would be to just stick to the byte count for scoring in the future. It's simpler to check for those on different systems. – Linus – 2016-11-05T21:40:05.913

@Linus I admit that selecting the score in a non standard way was a mistake. Will not repeat it in the future. – Leon – 2016-11-05T21:56:25.393

2Just a note: the provided sed command count the newlines, that are whitespace characters – edc65 – 2016-11-06T07:39:35.600

Answers

10

Score 116

Prepend:

#include<cstdio>
#define print(A)main(){puts(A);}
#define greet()
#define \

The preprocessor backslash \ pulls the nasty : containing line into an unused macro. Try it here.

Thanks to edc65's answer for the note about implicit int in C++4.
Thanks to PieCot's answer for suggesting <cstdio> over <stdio.h>.
Thanks to Leon for suggest I remove the X in the original #define X\.

Linus

Posted 2016-11-05T10:58:26.410

Reputation: 1 948

I don't have sed, if someone could verify my score I'd greatly appreciate it. – Linus – 2016-11-05T20:07:08.870

Removing all whitespace, my count (by hand) is 110 (but I was wrong... it's 111) – edc65 – 2016-11-05T20:09:34.893

2@Linus Why do you need the X in #define X\\? – Leon – 2016-11-05T21:03:33.503

@Leon good catch! – Linus – 2016-11-05T21:11:35.923

2For anyone wondering what C++4 is: In this case it is short for "The C++ that gcc 4.3.2 accepts". – nwp – 2016-11-06T11:06:55.310

11

Score 119

(Thx @Linus for the byte count)

(1 byte saved thx @Conor O'Brien) (1 byte saved thx @PieCot)

Counting bytes again by hand, I found 113. Maybe it's right this time. No it's not

#include <cstdio>
#define def main(){0?
#define print(x) puts(x);}
#define greet()

Notes: stdio and puts are still alive and kicking in C++. The missing int type is valid in C++ 4. Test

edc65

Posted 2016-11-05T10:58:26.410

Reputation: 31 086

The score must be computed against the full program (including the python code). – Leon – 2016-11-05T18:35:12.960

Since the ternary conditional can have an empty second portion, you can remove the trailing 0 on line 2. Test.

– Conor O'Brien – 2016-11-05T18:37:04.233

OK I don't see why, as the python code will be the same for every answer, but you are the boss. I did the count by hand, not having sed, I hope it's right – edc65 – 2016-11-05T18:37:29.510

@ConorO'Brien it really can! I did not know! Thx – edc65 – 2016-11-05T18:40:53.543

You can use <cstdio> rather than <stdio.h> – PieCot – 2016-11-05T19:54:41.727

So I managed to get an online terminal working and sed 's/\s//g' leaves the newlines. This ends up being 119 characters.

– Linus – 2016-11-05T21:15:57.013

Well if, sed leaves the newlines, that it's not count of non-whitespace characters, as newlines (like tabs, carriage return and the like) are whitespace characters. I keep my count – edc65 – 2016-11-05T23:35:42.073

@Linus I changed my mind, and I'll use your count, even if it does not feel right. We need consistency – edc65 – 2016-11-06T07:37:23.367

7

Score 131 130 134

The lines to be prepended are:

#import <iostream>
#define def int main(){0?
#define greet()
#define print(A) 0;std::cout<<A"\n";}

And the resulting code:

#import <iostream>
#define def int main(){0?
#define greet()
#define print(A) 0;std::cout<<A"\n";}
def greet():
    print("Hello, world!")

greet()

Testing

C:\Users\Conor O'Brien\Documents\Programming\golf
λ type bilingual.py.cpp

#import <iostream>
#define def int main(){0?
#define greet()
#define print(A) 0;std::cout<<A"\n";}
def greet():
    print("Hello, world!")

greet()
C:\Users\Conor O'Brien\Documents\Programming\golf
λ sed 's/\s//g' bilingual.py.cpp|wc -c
134

C:\Users\Conor O'Brien\Documents\Programming\golf
λ g++ bilingual.py.cpp 2>nul && a
Hello, world!

C:\Users\Conor O'Brien\Documents\Programming\golf
λ python bilingual.py.cpp
Hello, world!

C:\Users\Conor O'Brien\Documents\Programming\golf
λ 

Conor O'Brien

Posted 2016-11-05T10:58:26.410

Reputation: 36 228

The output of the C++ version is not identical to the python version - it misses a newline. Added that clarification to the question. – Leon – 2016-11-05T18:03:55.997

@Leon This is now fixed. – Conor O'Brien – 2016-11-05T18:07:43.003

#import is not valid C++ – Leon – 2016-11-05T18:17:56.103

@Leon It may be deprecated but it is valid. It works in g++. – Conor O'Brien – 2016-11-05T18:18:47.027

1Clever handling of : – edc65 – 2016-11-05T18:19:36.993

It is a deprecated gcc extension. g++ was used just as an example. The problem formulation requires a C++ program. And there is no sense arguing about that 1 char, since everyone will have to have that #include in their solution. – Leon – 2016-11-05T18:21:57.323

2@Leon Our site rules say that if it works in one environment, it's a valid submission. – Conor O'Brien – 2016-11-05T18:35:02.820

@Leon not everyone will have that #include, even if probably everyone will have some #include – edc65 – 2016-11-05T18:43:52.130

@edc65 By that #include I meant the include as opposed to import. – Leon – 2016-11-05T18:46:29.210

5

Score 136

Only for the records:

#include <cstdio>
#define def class a{public
#define greet()
#define print(a) };int main(){puts(a);}

Another (less efficient) way to handle the colon.

PieCot

Posted 2016-11-05T10:58:26.410

Reputation: 1 039

1But cstdio should be noted. – edc65 – 2016-11-05T20:07:53.940

I think the score for this ends up being 136. You don't count the spaces. – Linus – 2016-11-05T21:44:21.117

@Linus: Thanks! I think you are right. If I use this command:

tr -d '[:space:] ' < bilingual.py.cpp | wc -c

I get 128, while this one:

tr -d '[:blank:] ' < bilingual.py.cpp | wc -c

provides 136 – PieCot – 2016-11-05T21:52:07.990

5

Score 110 104

Improving upon Linus' answer:

#include <cstdio>
#define print main(){puts
#define greet() ;}//\
def greet():
    print("Hello, world!")

greet()

Test as C++

Test as Python

Leon

Posted 2016-11-05T10:58:26.410

Reputation: 307

I have get 109... – Linus – 2016-11-05T21:21:32.603

1@Linus I have a new line at the last line – Leon – 2016-11-05T21:23:14.313