Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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

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

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

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

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

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.

share|improve this answer
1  
But cstdio should be noted. – edc65 Nov 5 at 20:07
    
I think the score for this ends up being 136. You don't count the spaces. – Linus Nov 5 at 21:44
    
@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 Nov 5 at 21:52

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

share|improve this answer
    
I have get 109... – Linus Nov 5 at 21:21
1  
@Linus I have a new line at the last line – Leon Nov 5 at 21:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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