Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Make a polyglot generator

A polyglot is a program that runs in several languages.

Your challenge is to:

Choose at least two languages (Different versions count as different languages)

Create a program or function that takes several positive integers (one for each language you chose) and returns a program that:

Is a valid program in all of those languages

When run in language A, outputs the first number; When run in language B, outputs the second number; etcetera. Exiting by error is allowed, as long as the generated program prints the number first

The generator does not need to be in one of your chosen languages.

You can assume the inputted numbers within your chosen languages' limits. For example, if BF is one of your languages, the number for BF will be at most 255.

UPDATE

Explicitly checking the version number or implementation of the language is forbidden. To differentiate, use differences in language features.

Scoring

Your score is [code length] / [number of languages]2.

Example:

Let's say you were using JS/Python 3, in a JS program:

(x,y)=>`1//1;'''
console.log(${x})
//''';print(${y})`

Your score would be \$\frac{53}{2^{2}}\$ = 13.25.

The lower your score, the better.

Answer*

Draft saved
Draft discarded
Cancel
4
  • \$\begingroup\$ Why do I get a "Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW) in [...] on line 2"? \$\endgroup\$ Commented May 22, 2021 at 16:01
  • \$\begingroup\$ @PierrePaquette in which language? \$\endgroup\$ Commented May 24, 2021 at 7:13
  • \$\begingroup\$ Sorry! In PHP 7. \$\endgroup\$ Commented May 24, 2021 at 20:33
  • \$\begingroup\$ @PierrePaquette you mean the generator or the generated code? In TIO the PHP version is 7.4 and it works, I've tried on my local server too.. note that the code may output notices, or even errors from the eval'ed code, but it doesn't stop the execution and you should have the final result \$\endgroup\$ Commented May 25, 2021 at 9:50

AltStyle によって変換されたページ (->オリジナル) /