12
\$\begingroup\$

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
\$\endgroup\$
5
  • 5
    \$\begingroup\$ In the title you say add comments, however in the body you say you only have to prepend some lines. Which is it? \$\endgroup\$
    – Wheat Wizard
    Nov 5, 2016 at 17:18
  • \$\begingroup\$ @WheatWizard The title is a hint. If you can solve this by prepending arbitrary lines (non-comments) I will be puzzled. \$\endgroup\$
    – Leon
    Nov 5, 2016 at 17:37
  • \$\begingroup\$ 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. \$\endgroup\$
    – Linus
    Nov 5, 2016 at 21:40
  • \$\begingroup\$ @Linus I admit that selecting the score in a non standard way was a mistake. Will not repeat it in the future. \$\endgroup\$
    – Leon
    Nov 5, 2016 at 21:56
  • 2
    \$\begingroup\$ Just a note: the provided sed command count the newlines, that are whitespace characters \$\endgroup\$
    – edc65
    Nov 6, 2016 at 7:39

5 Answers 5

11
\$\begingroup\$

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\.

\$\endgroup\$
5
  • \$\begingroup\$ I don't have sed, if someone could verify my score I'd greatly appreciate it. \$\endgroup\$
    – Linus
    Nov 5, 2016 at 20:07
  • \$\begingroup\$ Removing all whitespace, my count (by hand) is 110 (but I was wrong... it's 111) \$\endgroup\$
    – edc65
    Nov 5, 2016 at 20:09
  • 2
    \$\begingroup\$ @Linus Why do you need the X in `#define X\`? \$\endgroup\$
    – Leon
    Nov 5, 2016 at 21:03
  • \$\begingroup\$ @Leon good catch! \$\endgroup\$
    – Linus
    Nov 5, 2016 at 21:11
  • 2
    \$\begingroup\$ For anyone wondering what C++4 is: In this case it is short for "The C++ that gcc 4.3.2 accepts". \$\endgroup\$
    – nwp
    Nov 6, 2016 at 11:06
11
\$\begingroup\$

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

\$\endgroup\$
8
  • \$\begingroup\$ The score must be computed against the full program (including the python code). \$\endgroup\$
    – Leon
    Nov 5, 2016 at 18:35
  • \$\begingroup\$ Since the ternary conditional can have an empty second portion, you can remove the trailing 0 on line 2. Test. \$\endgroup\$ Nov 5, 2016 at 18:37
  • \$\begingroup\$ 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 \$\endgroup\$
    – edc65
    Nov 5, 2016 at 18:37
  • \$\begingroup\$ @ConorO'Brien it really can! I did not know! Thx \$\endgroup\$
    – edc65
    Nov 5, 2016 at 18:40
  • \$\begingroup\$ You can use <cstdio> rather than <stdio.h> \$\endgroup\$
    – PieCot
    Nov 5, 2016 at 19:54
7
\$\begingroup\$

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
λ 
\$\endgroup\$
9
  • \$\begingroup\$ The output of the C++ version is not identical to the python version - it misses a newline. Added that clarification to the question. \$\endgroup\$
    – Leon
    Nov 5, 2016 at 18:03
  • \$\begingroup\$ @Leon This is now fixed. \$\endgroup\$ Nov 5, 2016 at 18:07
  • \$\begingroup\$ #import is not valid C++ \$\endgroup\$
    – Leon
    Nov 5, 2016 at 18:17
  • 1
    \$\begingroup\$ Clever handling of : \$\endgroup\$
    – edc65
    Nov 5, 2016 at 18:19
  • 2
    \$\begingroup\$ @Leon Our site rules say that if it works in one environment, it's a valid submission. \$\endgroup\$ Nov 5, 2016 at 18:35
6
\$\begingroup\$

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

\$\endgroup\$
2
  • \$\begingroup\$ I have get 109... \$\endgroup\$
    – Linus
    Nov 5, 2016 at 21:21
  • 1
    \$\begingroup\$ @Linus I have a new line at the last line \$\endgroup\$
    – Leon
    Nov 5, 2016 at 21:23
5
\$\begingroup\$

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.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ But cstdio should be noted. \$\endgroup\$
    – edc65
    Nov 5, 2016 at 20:07
  • \$\begingroup\$ I think the score for this ends up being 136. You don't count the spaces. \$\endgroup\$
    – Linus
    Nov 5, 2016 at 21:44
  • \$\begingroup\$ @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 \$\endgroup\$
    – PieCot
    Nov 5, 2016 at 21:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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