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
4  
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 14 hours ago
    
@WheatWizard The title is a hint. If you can solve this by prepending arbitrary lines (non-comments) I will be puzzled. – Leon 13 hours ago
    
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 9 hours ago
    
@Linus I admit that selecting the score in a non standard way was a mistake. Will not repeat it in the future. – Leon 9 hours ago

Score 113

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

#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 13 hours ago
    
Since the ternary conditional can have an empty second portion, you can remove the trailing 0 on line 2. Test. – Conor O'Brien 12 hours ago
    
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 12 hours ago
    
@ConorO'Brien it really can! I did not know! Thx – edc65 12 hours ago
    
You can use <cstdio> rather than <stdio.h> – PieCot 11 hours ago

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 11 hours ago
    
Removing all whitespace, my count (by hand) is 110 (but I was wrong... it's 111) – edc65 11 hours ago
2  
@Linus Why do you need the X in `#define X\`? – Leon 10 hours ago
    
@Leon good catch! – Linus 10 hours ago

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 13 hours ago
    
@Leon This is now fixed. – Conor O'Brien 13 hours ago
    
#import is not valid C++ – Leon 13 hours ago
1  
Clever handling of : – edc65 13 hours ago
2  
@Leon Our site rules say that if it works in one environment, it's a valid submission. – Conor O'Brien 13 hours ago

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 11 hours ago
    
I think the score for this ends up being 136. You don't count the spaces. – Linus 9 hours ago
    
@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 9 hours ago

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 10 hours ago
    
@Linus I have a new line at the last line – Leon 10 hours ago

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.