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 yesterday
    
@WheatWizard The title is a hint. If you can solve this by prepending arbitrary lines (non-comments) I will be puzzled. – Leon yesterday
    
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 yesterday
    
@Linus I admit that selecting the score in a non standard way was a mistake. Will not repeat it in the future. – Leon yesterday
1  
Just a note: the provided sed command count the newlines, that are whitespace characters – edc65 18 hours ago

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

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 yesterday
    
Removing all whitespace, my count (by hand) is 110 (but I was wrong... it's 111) – edc65 yesterday
2  
@Linus Why do you need the X in `#define X\`? – Leon yesterday
    
@Leon good catch! – Linus yesterday
2  
For anyone wondering what C++4 is: In this case it is short for "The C++ that gcc 4.3.2 accepts". – nwp 14 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 yesterday
    
@Leon This is now fixed. – Conor O'Brien yesterday
    
#import is not valid C++ – Leon yesterday
1  
Clever handling of : – edc65 yesterday
2  
@Leon Our site rules say that if it works in one environment, it's a valid submission. – Conor O'Brien yesterday

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

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

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.