Determine your language's version

52

7

Your challenge is to write a polyglot that works in different versions of your language. When run, it will always output the language version.

Rules

  • Your program should work in at least two versions of your language.
  • Your program's output should only be the version number. No extraneous data.
  • Your program may use whatever method you like to determine the version number. However, the output must follow rule 2; however you determine the version number, the output must only be the number.
  • Your program only needs to output the major version of the language. For example, in FooBar 12.3.456789-beta, your program would only need to output 12.
  • If your language puts words or symbols before or after the version number, you do not need to output those, and only the number. For example, in C89, your program only needs to print 89, and in C++0x, your program only needs to print 0.
  • If you choose to print the full name or minor version numbers, e.g. C89 as opposed to C99, it must only print the name. C89 build 32 is valid, while error in C89 build 32: foo bar is not.
  • Your program may not use a builtin, macro, or custom compiler flags to determine the language version.

Scoring

Your score will be the code length divided by the number of versions it works in. Lowest score wins, good luck!

MD XF

Posted 2017-08-16T04:26:47.137

Reputation: 11 605

Sandbox post. Related. – MD XF – 2017-08-16T04:27:36.660

Language version built-in allowed? – user202729 – 2017-08-16T04:30:47.950

@user202729 Your program may not use a builtin macro or custom compiler flags to determine the language version. – MD XF – 2017-08-16T04:31:28.577

So you must use some function that behave differently in versions to determine version number? LogoVersion in LOGO (11 byte) fail. --- Is usage of bugs in previous versions which is fixed in newer versions allowed? – user202729 – 2017-08-16T04:34:23.717

Can we output 3.0 for Python 3? – fireflame241 – 2017-08-16T04:34:33.640

@fireflame241 on Python 3.0, not on Python 3.1 or anything else. – MD XF – 2017-08-16T04:34:54.807

4What is a language version number? Who determines it? – Post Rock Garf Hunter – 2017-08-16T04:34:58.403

@WheatWizard The author of the language determines this. It should be marked on some corresponding webpage or document. – MD XF – 2017-08-16T04:35:33.330

@user202729 Yes, you must use some functionality that differs between language versions. Usage of bugs in previous versions is allowed, but if they don't work in newer versions they don't count toward your score. – MD XF – 2017-08-16T04:36:09.340

9I think that inverse-linear in the number of version does not welcome answers with high number of versions. – user202729 – 2017-08-16T05:23:27.387

6

@user202729 I agree. Versatile Integer Printer did it well - score was (number of languages)^3 / (byte count).

– Mego – 2017-08-16T05:37:26.927

Related. – Dom Hastings – 2017-08-16T06:31:40.920

Related – Peter Taylor – 2017-08-16T07:14:42.307

6What is the version for a language? Isn't we define a language as its interpreters / compilers here? Let we say, there is a version of gcc which has a bug that with certain C89 codes it produce an executable which behavior violate the C89 specification, and it was fixed on the next version of gcc. Should this count a valid solution if we write a piece of code base on this bug behavior to tell which gcc version is using? It targeting on different version of compiler, but NOT different version of language. – tsh – 2017-08-16T07:21:39.350

6I don't get this. First you say "Your program's output should only be the version number.". Then you say "If you choose to print the full name or minor version numbers, e.g. C89 as opposed to C99, it must only print the name." So the first rule is not actually a requirement? – pipe – 2017-08-16T09:13:17.633

1Your program may not use a builtin, macro, or custom compiler flags to determine the language version. So... no way to complete this challenge in C++ ? In my knowledge, the only way to programmatically detect the C++ language version is by using the __cplusplus macro... – HatsuPointerKun – 2017-08-16T11:35:16.173

3@HatsuPointerKun Try using differences between versions. – Erik the Outgolfer – 2017-08-16T14:02:13.887

This was a fun question, I really enjoyed reading the answers. Thanks for asking! – user6014 – 2017-08-22T16:09:34.817

1Your program may use whatever method you like to determine the version number. Then: Your program may not use a builtin, macro, or custom compiler flags to determine the language version. Please don't condradict yourself in the spec. – Grimmy – 2019-05-21T16:36:20.840

@HatsuPointerKun That's to prevent "Python 2 --version and Python 3 --version, 0 bytes", which is no fun at all.

– Khuldraeseth na'Barya – 2019-08-27T15:47:57.943

hmm... what's correct output if the program is written in x86 machine code? – 640KB – 2019-08-28T14:32:08.910

Answers

17

Seriously and Actually, 3 bytes, score 1.5

'1u

Try it online: Actually, Seriously

Explanation:

'1u
'1   both versions: push "1"
  u  Actually: increment character to "2"; Seriously: NOP
     (both versions: implicit print)

u and D having functionality on strings was only added in Actually (which is Seriously v2).

Mego

Posted 2017-08-16T04:26:47.137

Reputation: 32 998

3Actually's README.md says that Actually is the spiritual successor to Seriously. Doesn't sound like a mere version change to me. – Adám – 2017-08-18T07:49:02.910

7

@Adám If you look at the branches in the repository, Seriously resides in the v1 branch. Prior to Seriously being deprecated, Actually resided in the v2 branch. Additionally, Seriously used 1.x version numbers in releases, while Actually uses 2.x (both there and on PyPI).

– Mego – 2017-08-18T07:51:55.453

117

Python 3.0 and Python 2, score 6

(12 bytes, 2 versions)

print(3/2*2)

Try it Online:

Relies on the fact that Python 3+ uses float division by default, unlike Python 2, which uses floor division.

fireflame241

Posted 2017-08-16T04:26:47.137

Reputation: 7 021

@MaltySen Your program should work in at least two versions of your language. It works in the at-least-two versions 2.7 and 3.0. I chose to print the full name or minor version numbers. – fireflame241 – 2017-08-16T05:02:05.007

Oh I see, my bad. – Maltysen – 2017-08-16T05:02:48.877

4OMG! Poor python developers – Regis Portalez – 2017-08-21T07:01:33.680

4@RegisPortalez from __future__ import division, problem solved :) – Łukasz Rogalski – 2017-08-21T12:39:17.240

62

Java, 189 bytes, 10 versions, score = 18.9

Supported versions: 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8 and 9

(For previous scores, check the history!)

Object v(){int i=0;try{for(String[]s={"Locale","Map","Timer","Currency","UUID","Deque","Objects","Base64","zip.CRC32C"};;i++)Class.forName("java.util."+s[i]);}finally{return i<9?"1."+i:i;}}

Run it on Java 8
Run it on Java 9 or later

Ungolfed

Object v(){
  int v=0;
  try {
    for(
      String[] s={
        "Locale",          // 1.1
        "Map",             // 1.2
        "Timer",           // 1.3
        "Currency",        // 1.4
        "UUID",            // 1.5
        "Deque",           // 1.6
        "Objects",         // 1.7
        "Base64",          // 1.8
        "zip.CRC32C"       // 9
      };;v++)
      Class.forName("java.util."+s[v]);
  } finally {
    // Swallowing ClassNotFoundException when the version is not the last one
    // Swallowing ArrayIndexOutOfBoundsException that occurs after reaching the last version.
    return v < 9 ? "1." + v : v; // Return either an int or a String
  }
}

Please note that the code part return v<9?"1."+v:v; (previously return(v<9?"1.":"")+v;) needs to be checked against any version between Java 1.0 and Java 1.3 included. I don't have any Java 1.3 or earlier installation at my disposal to actually test this syntax.

Introduction

The Java versioning has a special history. All versions have historically been 1.x including 1.0. But... from Java 9 onwards and the JEP223, the version scheming has changed from using 1.x to x. That is the version as internally known. So we have the following table (put together with the Javadoc and Wikipedia):

 java.version | Rel. name | Product name
   property   |           |
--------------+-----------+-----------------
          1.0 | JDK 1.0   | Java 1
          1.1 | JDK 1.1   |
          1.2 | J2SE 1.2  | Java 2
          1.3 | J2SE 1.3  |
          1.4 | J2SE 1.4  |
          1.5 | J2SE 5.0  | Java 5
          1.6 | Java SE 6 | Java 6
          1.7 | Java SE 7 | Java 7
          1.8 | Java SE 8 | Java 8
          9   | Java SE 9 | Java 9

This challenge entry matches the version column in the table above, which is what is contained in the system property "java.version".

Explanation

The goal is to check from which version a class starts to exist, because Java deprecates code but never removes it. The code has been specifically written in Java 1.0 to be compatible with all the versions, again, because the JDK is (mostly) source forward compatible.

The implementation tries to find the shortest class names that each version introduced. Though, to gain bytes, it's needed to try and pick a common subpackage. So far I found the most efficient package is java.util because it contains several really short-named classes spread across all versions of Java.

Now, to find the actual version number, the class names are sorted by introducing version. Then I try to instanciate each class sequentially, and increment the array index. If the class exists, we skip to the next, otherwise we let the exception be caught by the try-block. When done, another exception is thrown because there are no more classes whose existence we need to check.

In any case, the thread will leave the try-block with an exception. That exception is not caught, but simply put on hold thanks to the finally-block, which in turn overrides the on-hold exception by actually returning a value which is "1."+v where v is the index used before. It also happens we made this index match the minor version number of Java.

An important part of the golf was to find the shortest new class name in the package java.util (or any children package) for each version. Here is the table I used to compute that cost.

Base cost: `java.util.` (10 chars)

 Version | Class name (cost in chars)     | Reduced name (cost in chars)
---------+--------------------------------+---------------------------
 9       | java.util.zip.CRC32C (20)      | zip.CRC32C (10)
 1.8     | java.util.Base64 (16)          | Base64 (6)
 1.7     | java.util.Objects (17)         | Objects (7)
 1.6     | java.util.Deque (15)           | Deque (5)
 1.5     | java.util.UUID (14)            | UUID (4)
 1.4     | java.util.Currency (18)        | Currency (8)
 1.3     | java.util.Timer (15)           | Timer (5)
 1.2     | java.util.Map (13)             | Map (3)
 1.1     | java.util.Locale (16)          | Locale (6)
 1.0     | <default>                      | <default>
---------+--------------------------------+---------------------------
Subtotal |                      144 chars |                  54 chars
    Base |                                |                  10 chars
   Total |                      144 chars |                  64 chars

Credits

  • 30 bytes saved thanks to Kevin Cruijssen (although I was doing it before I read his comment, I promise!).
  • 26 further bytes saved thanks to Neil (nope, I wasn't thinking about doing that)
  • 12 bytes thanks to Nevay and the nice out-of-the-box-try-catch thinking!
  • 11 more bytes by Neil again and the nice portable finally trick.
  • 2 more bytes thanks to Kevin Cruijssen by replacing return(i<9?"1.":"")+i; with return i<9?"1."+i:i; (this needs to be validated against 1.0 or at most 1.3 since no syntax changes happened before 1.4)

With builtins

If builtins were allowed:

String v(){return System.getProperty("java.version");}

54 bytes for 13 versions (from 1.0 to 12), so the score would be 4.1538.

Olivier Grégoire

Posted 2017-08-16T04:26:47.137

Reputation: 10 647

Would you not score better with just 2 versions? – Shaggy – 2017-08-16T08:49:17.023

Ah, you beat me to it. Was currently writing this exact answer, and was looking at which classes were new in which versions. :) +1 from me. Also, you can golf some bytes by doing return"1."+(...); – Kevin Cruijssen – 2017-08-16T08:49:53.517

@Shaggy I'm running for the most versions. Be my guest for a 2 versions solution ;) – Olivier Grégoire – 2017-08-16T08:51:12.940

Out of curiosity, where did you find which classes were new in which versions? Was trying to find an overview site for it, but couldn't find it 1-2-3. – Kevin Cruijssen – 2017-08-16T08:52:38.840

1

@KevinCruijssen I opened the javadoc and ran classes with short names 1 by 1. But... I was helped a bit by this page: http://docs.oracle.com/javase/8/docs/technotes/guides/lang/enhancements.html

– Olivier Grégoire – 2017-08-16T08:53:36.867

1260 bytes Or maybe 1 more, don't know if return"... without space is possible in all versions tbh.) – Kevin Cruijssen – 2017-08-16T08:56:28.047

@KevinCruijssen I was just doing it! Stop reading my mind X( Really, thanks anyways. ;) Also, I don't know if return"... works everywhere as well. I can test on 1.3 at the lowest and it's ok there. – Olivier Grégoire – 2017-08-16T09:02:21.483

@OlivierGrégoire We Java golfers always read each others mind. ;p – Kevin Cruijssen – 2017-08-16T09:10:13.717

1235 bytes: String v(){return "1."+(e("time.Year")+e("nio.file.Path")+e("io.Console")+e("util.UUID")+e("text.Bidi")+e("util.Timer")+e("sql.Ref")+e("lang.Void"));}int e(String c){try{Class.forName("java."+c);return 1;}catch(Exception e){return 0;}} – Neil – 2017-08-16T09:19:02.570

3216 bytes: String v(){int i=0;try{for(String[]s={"lang.Void","sql.Ref","util.Timer","net.URI","util.UUID","net.IDN","nio.file.Path","time.Year","lang.Module"};;i++)Class.forName("java."+s[i]);}catch(Exception e){}return"1."+i;} – Nevay – 2017-08-16T13:17:59.547

@Nevay When I just finished incorporating your first changes, I saw you edited it... and made it even shorter than what I improved on your try! Good job to you, golfing so much! :) – Olivier Grégoire – 2017-08-16T13:37:01.527

1Ooh, I did wonder about iterating an array and catching an exception, but you can go one better with finally{return"1."+i;}. – Neil – 2017-08-17T09:59:38.037

Seems to work even on 1.3, so I'll take it. Thanks @Neil ! :) – Olivier Grégoire – 2017-08-17T10:29:10.443

Doesn't javax.lang.model.SourceVersion work? – Magic Octopus Urn – 2017-09-21T20:13:29.523

@MagicOctopusUrn it shows the version with which a class was compiled, not run. Also, it's only available since 1.6. – Olivier Grégoire – 2017-09-22T08:44:01.360

1Not sure if it works in all versions, but I think you can save a byte by having Object instead of String as return-type, and change the ternary to return i<9?"1."+i:i;. Also, 2 (almost 3) more Java versions are available now. ;D – Kevin Cruijssen – 2019-08-27T13:24:15.200

@KevinCruijssen I'm checking this answer from time to time and actually, I've found out, strangely, that none of Java 10, 11 or 12 added any new classes in java.util and its subpackages, and so far there are still no new classes planned in Java 13's java.util package. If I must support more versions, I'll probably fall back on java.lang. Until then, I stick to this answer. Regarding the golf, I don't have any versions < 7 available anymore so I cannot test it, unfortunately... So I'll adapt, with a warning. – Olivier Grégoire – 2019-08-27T14:00:19.703

22

Python, 606 bytes / 15 versions = score 40.4

-67 bytes (lol) thanks to NoOneIsHere.

The versions are 0.9.1, 2(.0), 2.2, 2.2.2, 2.5.0, 2,5.1, 3(.0), 3.1, 3.1.3, 3.2.1, 3.3, 3.4, 3.5 aaand 3.6.

try:eval('1&2')
except:print('0.9.1');1/0
if`'\n'`<'\'\\n\'':print(2);1/0
try:from email import _Parser;print(2.2);1/0
except:0
try:eval('"go"in""')
except:print('2.2.2');1/0
try:int('2\x00',10);print(2.5);1/0
except:0
if pow(2,100)<1:print('2.5.1');1/0
if str(round(1,0))>'1':print(3);1/0
if format(complex(-0.0,2.0),'-')<'(-':print(3.1);1/0
if str(1.0/7)<repr(1.0/7):print('3.1.3');1/0
try:eval('u"abc"')
except:print('3.2.1');1/0
try:int(base=10);print(3.3);1/0
except:0
try:import enum
except:print('3.3.3');1/0
try:eval('[*[1]]')
except:print(3.4);1/0
try:eval('f""')
except:print(3.5);1/0
print(3.6)

All credit to Sp3000's amazing answer. The trailing newline is necessary.

Whee, that was fun to golf. This should work (yes, I installed every one of these versions), but I might've accidentally borked something. If anybody finds a bug, please let me know.

totallyhuman

Posted 2017-08-16T04:26:47.137

Reputation: 15 378

...Oh, no wonder. I was wondering why Sp3000 has put parentheses in every print call... Thanks for letting me know! – totallyhuman – 2017-08-16T14:02:23.530

2You can save 68 bytes by removing the specific types of errors (replace all excepts with except:). – NoOneIsHere – 2017-08-16T18:10:06.213

Would this still work if you did x=<string inside eval> instead of just evaling the code manually? – Blue – 2017-08-17T14:08:31.613

@NoOneIsHere I thought, at first, that you couldn't because of all the 1/0's, but then I realized. Thanks! – totallyhuman – 2017-08-17T15:48:42.983

@Blue Ah, no. SyntaxErrors thrown by regular code cannot be caught. They can only be caught when using eval or exec. – totallyhuman – 2017-08-17T15:49:37.900

Can you take the whole code, swap out all those "except"s for "x", then call eval on that string.replace('x','except')? E.g. instead of try:from email import _Parser;print(2.2);1/0 except:0 do eval('try:from email import _Parser;print(2.2);1/0 xt:0'.replace('x','except') – Acccumulation – 2018-06-21T21:18:09.853

I thought I was amazing for differentiating python 1 and 2 but wow, this is much more amazing. – Want – 2020-01-26T02:57:26.740

21

C++ 11/14/17, score = 147/3 = 49

To distinguish between C++11 and C++14/17, it uses the change in the default constness of constexpr member functions in C++14 (with credit to the example at https://stackoverflow.com/questions/23980929/what-changes-introduced-in-c14-can-potentially-break-a-program-written-in-c1). To distinguish between C++14 and C++17, it uses the fact that C++17 disabled trigraphs.

#include<iostream>
#define c constexpr int v
struct A{c(int){return 0;}c(float)const{return*"??="/10;}};int main(){const A a;std::cout<<11+a.v(0);}

Ungolfed:

struct A {
    constexpr int v(int) { return 0; }
    constexpr int v(float) const {
        // with trigraphs, *"??=" == '#' == 35, v() returns 3
        // without trigraphs, *"??" == '?' == 63, v() returns 6
        return *("??=") / 10;
    }
};

int main() {
    const A a;
    std::cout << 11 + a.v(0);
}

(Tested with Debian gcc 7.1.0 using -std=c++{11,14,17}.)

Daniel Schepler

Posted 2017-08-16T04:26:47.137

Reputation: 1 001

Cool answer! Welcome to the site :) – James – 2017-08-16T18:37:02.497

1Great first answer! Note that you can golf the spaces between the include and the < in the include statements, for example #include<iostream>. – MD XF – 2017-08-16T19:17:15.987

@MDXF Thanks, edited accordingly. – Daniel Schepler – 2017-08-16T19:30:34.737

1Hmm... if the rules were revised to forbid using standard library differences (which in this case indirectly uses the __cplusplus macro) - then to distinguish C++17 from C++14 I'd lean toward using the change in range-based for semantics. Maybe create minimal iterator/sentinel classes along the lines of boost::integer_iterator such that converting sentinel to iterator has "surprising" behavior. – Daniel Schepler – 2017-08-16T19:39:40.213

4return 0; is implicit for main so you can save 9 bytes there. Also according to wc -c your solution is using 251 bytes and not 252 (your editor may have inserted a newline at the end). – nwp – 2017-08-17T10:25:34.217

@nwp Thanks, I wasn't aware of the implicit return 0. And you were also right about the newline at the end (I wonder when Emacs started doing that). Edited accordingly. – Daniel Schepler – 2017-08-17T16:45:23.587

1It is probably shorter to use the lack of trigraphs to separate c++17 from c++14 – Potato44 – 2017-08-18T09:33:26.047

@Potato44 Yes, that did indeed shorten the code drastically, thanks! – Daniel Schepler – 2017-08-18T16:54:52.857

1Would this work? return *=>return* – Zacharý – 2017-08-18T19:17:32.843

@Zachary Ah yes, of course, how silly of me. :) Will edit, thanks. – Daniel Schepler – 2017-08-18T19:34:05.610

If you want to go for C++03, A<B<C>>::D>::E>::F can be valid in C++03 and C++11 with different meanings. Probably not easy to reduce score with this, though. – aschepler – 2017-08-19T05:06:26.390

@aschepler Also probably not easy to keep the code using constexpr either. :) – Daniel Schepler – 2017-08-19T18:10:17.133

You could write this as a function that returns the version as an int, instead of a program that prints it as a string. That lets you drop iostream. You can also move the struct A declaration inside your function like int V(){struct A {...}const a; return 11 + a.v(0);}. You can use long instead of float, because integer literals are int unless they're too large for an int. (I tested that this works even when int and long are both 32-bit types, on gcc/clang with -m32). Anyway, see https://godbolt.org/g/zRjHxt for my golfed version, with asm output showing that it works.

– Peter Cordes – 2017-08-20T13:16:43.203

1You don't need to specify the return type of main - it will default to int. This will save another 4 bytes. – CSM – 2017-08-20T15:25:27.247

20

EcmaScript 3 / 5 / 2015 / 2016 / 2017 in Browser, 59 bytes / 5 versions = 11.8 points

alert(2017-2*![].map-2010*![].fill-![].includes-!"".padEnd)

NetScape 7 report 3, and Opera 12 report 5

Save 1 byte thanks to GOTO 0

tsh

Posted 2017-08-16T04:26:47.137

Reputation: 13 072

1Ninjaed! ;) – Shaggy – 2017-08-16T08:42:29.577

Netscape 7 only supported ES3? Wow, it's older than I thought... – Neil – 2017-08-16T09:22:46.430

1You could save a few bytes using -! instead of +!! where it makes sense (and change the numeric constants accordingly). – GOTO 0 – 2017-08-16T14:14:19.107

3Maybe some explanation? :) – Derek 朕會功夫 – 2017-08-18T20:55:19.727

@Derek: see my solution (linked above) for an explanation. – Shaggy – 2017-08-22T20:31:32.937

18

JavaScript (ES5 & ES6), 14 bytes / 2 versions = 7

alert(5^"0o3")

0o-style octal constants are new in ES6; ES5 casts the string to NaN which doesn't affect the result of the bitwise XOR.

Neil

Posted 2017-08-16T04:26:47.137

Reputation: 95 035

13

JavaScript (ES 2, 3 & 5 - 8 9), 59 / 6 = 9.833 75 / 7 = 10.714

May as well submit the solution with more versions, even if it does score slightly higher than the 2-version solution.

alert(9-(/./.dotAll!=0)-!"".padEnd-![].includes-![].keys-2*![].map-![].pop)

Try it online

Checks for the presence of various methods in the Array, RegExp & String prototypes, negates them, giving a boolean, and subtracts that boolean from an initial value of 9. The multiplication of ![].map accounts for the fact that ES4 was abandoned.

  • The dotAll property (and related s flag) for Regular Expressions was introduced in ES2018 (v9).
  • The padEnd String method was introduced in ES2017 (v8).
  • The includes Array method was introduced in ES2016 (v7).
  • The keys Array method was introduced in ES2015 (v6).
  • The map Array method was introduced in ES5.1 (v5).
  • The pop Array method was introduced in ES3 (v3).

Shaggy

Posted 2017-08-16T04:26:47.137

Reputation: 24 623

Is ES 7 or ES 8 a valid version number? Maybe it should be called as ES 201x? – tsh – 2017-08-16T08:54:36.063

1@tsh: Yes, they still use version numbers; they just use years for realese names. – Shaggy – 2017-08-16T08:58:25.123

9

PHP 5/7, score 5.5

<?=7-"0x2";

3V4L it online!

PHP 5.3.9/5.3.11, score 10

<?='5.3.'.(9-0x0+2);

3V4L it online!

The online version is longer because old PHP versions on the sandbox don't have short tags enabled.

Sisyphus

Posted 2017-08-16T04:26:47.137

Reputation: 1 521

9

Befunge : 15 11 bytes / 2 versions = 5.5

4 bytes shaved off by @Pietu1998

"89",;5-;,@  

Try it online :
Befunge 93
Befunge 98
Uses the Befunge 98-exclusive semicolon operator ("skip to next semicolon") to differentiate versions. Both will print "9". Befunge 93 will ignore the semicolons, subtract 5 from "8" (value left on top of the stack), print the resulting "3" and terminate. Befunge 98 on the other hand, will skip over, print "8" and terminate.

karhell

Posted 2017-08-16T04:26:47.137

Reputation: 411

"89",;5-;,@ for 11 bytes – PurkkaKoodari – 2017-08-16T14:02:30.947

@Pietu1998 Nice! If you want to post that as an answer, I'll gladly upvote :) – karhell – 2017-08-16T14:04:45.510

Go ahead and take it if you'd like, you figured out the ; part. – PurkkaKoodari – 2017-08-16T14:07:27.230

@Pietu1998 Edited in. Many thanks! – karhell – 2017-08-16T14:20:03.090

FYI, I managed to get it down to 7 bytes, taking a different approach. Link

– James Holderness – 2017-11-09T20:30:08.133

8

Cubically, 4 bytes, score 4/∞

B3%0

Works in every version your system has enough memory to run. Non-competing because it's lame. Valid per this meta post.

Basically, B3 rotates one row from the left face into the top face. F3 would work just as well, as would F₁3 or B₁3. As one row in Cubically 3x3x3 is three cubelets by one cubelet, this puts three 1's into the top face, giving it a face sum of 3. %0 prints that top face sum, printing 3 for Cubically 3x3x3.

In Cubically 4x4x4, rows are 4x1 cubies. Puts 4 1's into the top face, yielding a sum of 4.

MD XF

Posted 2017-08-16T04:26:47.137

Reputation: 11 605

9Shouldn't the score be 4/∞? – nwp – 2017-08-17T10:08:44.213

8

x86 16/32/64-bit machine code: 11 bytes, score= 3.66

This function returns the current mode (default operand-size) as an integer in AL. Call it from C with signature uint8_t modedetect(void);

NASM machine-code + source listing (showing how it works in 16-bit mode, since BITS 16 tells NASM to assemble the source mnemonics for 16-bit mode.)

 1          machine      global modedetect
 2          code         modedetect:
 3 addr     hex          BITS 16

 5 00000000 B040             mov    al, 64
 6 00000002 B90000           mov    cx, 0       ; 3B in 16-bit.  5B in 32/64, consuming 2 more bytes as the immediate
 7 00000005 FEC1             inc    cl          ; always 2 bytes.  The 2B encoding of inc cx would work, too.
 8                       
 9                           ; want: 16-bit cl=1.   32-bit: cl=0
10 00000007 41               inc    cx       ; 64-bit: REX prefix
11 00000008 D2E8             shr    al, cl   ; 64-bit: shr r8b, cl doesn't affect AL at all.  32-bit cl=1.  16-bit cl=2
12 0000000A C3               ret
# end-of-function address is 0xB, length = 0xB = 11

Justification:

x86 machine code doesn't officially have version numbers, but I think this satisfies the intent of the question by having to produce specific numbers, rather than choosing what's most convenient (that only takes 7 bytes, see below).

The original x86 CPU, Intel's 8086, only supported 16-bit machine code. 80386 introduced 32-bit machine code (usable in 32-bit protected mode, and later in compat mode under a 64-bit OS). AMD introduced 64-bit machine code, usable in long mode. These are versions of x86 machine language in the same sense that Python2 and Python3 are different language versions. They're mostly compatible, but with intentional changes. You can run 32 or 64-bit executables directly under a 64-bit OS kernel the same way you could run Python2 and Python3 programs.

How it works:

Start with al=64. Shift it right by 1 (32-bit mode) or 2 (16-bit mode).

  • 16/32 vs. 64-bit: The 1-byte inc/dec encodings are REX prefixes in 64-bit (http://wiki.osdev.org/X86-64_Instruction_Encoding#REX_prefix). REX.W doesn't affect some instructions at all (e.g. a jmp or jcc), but in this case to get 16/32/64 I wanted to inc or dec ecx rather than eax. That also sets REX.B, which changes the destination register. But fortunately we can make that work but setting things up so 64-bit doesn't need to shift al.

    The instruction(s) that run only in 16-bit mode could include a ret, but I didn't find that necessary or helpful. (And would make it impossible to inline as a code-fragment, in case you wanted to do that). It could also be a jmp within the function.

  • 16-bit vs. 32/64: immediates are 16-bit instead of 32-bit. Changing modes can change the length of an instruction, so 32/64 bit modes decode the next two bytes as part of the immediate, rather than a separate instruction. I kept things simple by using a 2-byte instruction here, instead of getting decode out of sync so 16-bit mode would decode from different instruction boundaries than 32/64.

    Related: The operand-size prefix changes the length of the immediate (unless it's a sign-extended 8-bit immediate), just like the difference between 16-bit and 32/64-bit modes. This makes instruction-length decoding difficult to do in parallel; Intel CPUs have LCP decoding stalls.


Most calling conventions (including the x86-32 and x86-64 System V psABIs) allow narrow return values to have garbage in the high bits of the register. They also allow clobbering CX/ECX/RCX (and R8 for 64-bit). IDK if that was common in 16-bit calling conventions, but this is code golf so I can always just say it's a custom calling convention anyway.

32-bit disassembly:

08048070 <modedetect>:
 8048070:       b0 40                   mov    al,0x40
 8048072:       b9 00 00 fe c1          mov    ecx,0xc1fe0000   # fe c1 is the inc cl
 8048077:       41                      inc    ecx         # cl=1
 8048078:       d2 e8                   shr    al,cl
 804807a:       c3                      ret    

64-bit disassembly (Try it online!):

0000000000400090 <modedetect>:
  400090:       b0 40                   mov    al,0x40
  400092:       b9 00 00 fe c1          mov    ecx,0xc1fe0000
  400097:       41 d2 e8                shr    r8b,cl      # cl=0, and doesn't affect al anyway!
  40009a:       c3                      ret    

Related: my x86-32 / x86-64 polyglot machine-code Q&A on SO.

Another difference between 16-bit and 32/64 is that addressing modes are encoded differently. e.g. lea eax, [rax+2] (8D 40 02) decodes as lea ax, [bx+si+0x2] in 16-bit mode. This is obviously difficult to use for code-golf, especially since e/rbx and e/rsi are call-preserved in many calling conventions.

I also considered using the 10-byte mov r64, imm64, which is REX + mov r32,imm32. But since I already had an 11 byte solution, this would be at best equal (10 bytes + 1 for ret).


Test code for 32 and 64-bit mode. (I haven't actually executed it in 16-bit mode, but the disassembly tells you how it will decode. I don't have a 16-bit emulator set up.)

; CPU p6   ;  YASM directive to make the ALIGN padding tidier
global _start
_start:
    call   modedetect
    movzx  ebx, al
    mov    eax, 1
    int    0x80        ; sys_exit(modedetect());

align 16
modedetect:
BITS 16
    mov    al, 64
    mov    cx, 0       ; 3B in 16-bit.  5B in 32/64, consuming 2 more bytes as the immediate
    inc    cl          ; always 2 bytes.  The 2B encoding of inc cx would work, too.

    ; want: 16-bit cl=1.   32-bit: cl=0
    inc    cx       ; 64-bit: REX prefix
    shr    al, cl   ; 64-bit: shr r8b, cl doesn't affect AL at all.  32-bit cl=1.  16-bit cl=2
    ret

This Linux program exits with exit-status = modedetect(), so run it as ./a.out; echo $?. Assemble and link it into a static binary, e.g.

$ asm-link -m32 x86-modedetect-polyglot.asm && ./x86-modedetect-polyglot; echo $?
+ yasm -felf32 -Worphan-labels -gdwarf2 x86-modedetect-polyglot.asm
+ ld -melf_i386 -o x86-modedetect-polyglot x86-modedetect-polyglot.o
32
$ asm-link -m64 x86-modedetect-polyglot.asm && ./x86-modedetect-polyglot; echo $?
+ yasm -felf64 -Worphan-labels -gdwarf2 x86-modedetect-polyglot.asm
+ ld -o x86-modedetect-polyglot x86-modedetect-polyglot.o
64

## maybe test 16-bit with BOCHS somehow if you really want to.

7 bytes (score=2.33) if I can number the versions 1, 2, 3

There are no official version numbers for different x86 modes. I just like writing asm answers. I think it would violate the question's intent if I just called the modes 1,2,3, or 0,1,2, because the point is to force you to generate an inconvenient number. But if that was allowed:

 # 16-bit mode:
42                                  detect123:
43 00000020 B80300                      mov ax,3
44 00000023 FEC8                        dec al
45                                  
46 00000025 48                          dec ax
47 00000026 C3                          ret

Which decodes in 32-bit mode as

08048080 <detect123>:
 8048080:       b8 03 00 fe c8          mov    eax,0xc8fe0003
 8048085:       48                      dec    eax
 8048086:       c3                      ret    

and 64-bit as

00000000004000a0 <detect123>:
  4000a0:       b8 03 00 fe c8          mov    eax,0xc8fe0003
  4000a5:       48 c3                   rex.W ret 

Peter Cordes

Posted 2017-08-16T04:26:47.137

Reputation: 2 810

I'm not sure these counts as different versions. Don't they just correlate to different system configurations.? – Uriel – 2017-08-19T23:47:52.417

1@Uriel: Running a block of machine code with the CPU in 16-bit mode, 32-bit mode, or 64-bit mode is the machine-code equivalent of running python2 vs. python3 interpreters on the same Python program. New x86 CPUs always include a mode that's compatible with older CPUs (this is their only excuse for using such a convoluted hard-to-decode machine-code format!), but 386's 32-bit protected mode and x86-64's long mode really are new versions of x86 machine code. Long mode even removed some opcodes, making them invalid. – Peter Cordes – 2019-05-18T21:06:49.423

7

Pyth 4/5 - 6 bytes/2 versions = 3

  5 ;4

In Pyth 5, an even amount of spaces at the start of the line is ignored for use in indenting, while in Pyth 4, it just acts like a single space and prevents printing the 5. In Pyth 4, semicolons just finish statements, which allows the 4 to be printed, while in Pyth 5, a space and semicolon makes the rest of the line a comment.

Maltysen

Posted 2017-08-16T04:26:47.137

Reputation: 25 023

11Who knew Pyth had versions? – Erik the Outgolfer – 2017-08-16T13:58:51.460

7

Python 3 and Python 2.0, 18 bytes, score 18 / 2 = 9

print(3-round(.5))

Banker's rounding in Python 3, standard rounding in Python 2.

Try it online - Python 3!

Try it online - Python 2!

Stephen

Posted 2017-08-16T04:26:47.137

Reputation: 12 293

wow I've always seen people differentiating python 2 and 3 by division – phuclv – 2017-08-25T09:00:40.170

@LưuVĩnhPhúc well division is golfier, so that's why :P – Stephen – 2017-08-25T11:24:21.723

5

Brachylog / Brachylog v1, 5 / 2 = 2.5

2,1hw

Try it online! (Brachylog)

Try it online! (Brachylog v1)

Explanation for Brachylog:

?2,1hw.
?2      Unify ? (input) with 2 (no input so it succeeds)
  ,1    Append 1 (21)
    h   First element/head (2)
     w. Write to STDOUT and unify with output (not displayed)

Explanation for Brachylog v1:

?2,1hw.
?2      Unify ? (input) with 2 (no input so it succeeds)
  ,     Break implicit unification/logical AND
   1h   Take first element/head of 1 (1)
     w. Write to STDOUT and unify with output (not displayed)

Erik the Outgolfer

Posted 2017-08-16T04:26:47.137

Reputation: 38 134

Great! As a sidenote, 2,1 in Brachylog v2 does not construct the list [2,1] (2;1 would), but rather the number 21 (which doesn't change the way you intended your answer to work). – Fatalize – 2017-08-17T13:55:57.133

@Fatalize Ooh thanks I confused that with Jelly... – Erik the Outgolfer – 2017-08-17T13:56:40.597

@Fatalize BTW 2;1 wouldn't have worked in Brachylog v1 as ; means logical OR there. – Erik the Outgolfer – 2017-08-17T14:03:09.773

5

Perl 5 and Perl 6, 23 bytes 19 bytes, score 9.5

print 6-grep '.',''

Perl 5 grep first op is always treated as a regex, not so in Perl 6.

Joshua

Posted 2017-08-16T04:26:47.137

Reputation: 261

score is 19/2 = 9.5 – Daniel Vestøl – 2017-08-18T08:42:42.503

5

Bash, all 4 versions, 72 71 32 bytes ⇒ score = 8

s=$'\ua\xa\n';expr 5 - ${#s} / 2

This piece of code makes use of different interpretations of $'...' strings in each version of Bash.
Outputs the major version number -- and that's it.

Doc found here.

Ungolfed:

s=$'\ua\xa\n';
expr 5 - ${#s} / 2
# Bash v4 sees three linefeeds => length of 3 => 5 - 3 / 2 = 4
# Bash v3 sees the literal '\ua' + two linefeeds: 5 chars in length
#    => 5 - 5 / 2 = 3
# Bash v2 sees '\ua\xa' + linefeed, 7 chars: 5 - 7 / 2 = 2
# Bash v1 does not even interpret $'..' strings, and sees literally '$\ua\xa\n' of length 9 => 5 - 9 / 2 = 1

This answer is halfly a guess; I only tested it in bash 4 and 3, but it should work on other versions too.

Let me know if it does / does not, I will try with other versions as soon as I have them available.

-1 char thanks to Jens.
-29 bytes thanks to Digital Trauma (the whole expr idea)!

joH1

Posted 2017-08-16T04:26:47.137

Reputation: 391

The shell grammar does not require a ;; in the last alternative. Use ; to shave off a byte. – Jens – 2017-08-18T07:23:02.023

1I've just tried this on bash-2.05a (compiled just now for Cygwin), and it incorrectly reports "3", not "2" :( – Jason Musgrove – 2017-08-21T09:51:45.670

1the interpret-$'\xN feature seems to have been added in 2.01.1... I will have to update my answer. Working on it – joH1 – 2017-08-21T10:12:51.217

can I ask you to try this? s="$'\ua\xa\n'";case ${#s} in 3)echo 4;;5)echo 3;;7)echo 2;;9)echo 1;esac – joH1 – 2017-08-21T10:16:53.570

Sorry for delay. Nothing is returned. The length of $s appears to be 11 - the literal content of the quoted string. This also appears to be the case in bash 4.4.12 – Jason Musgrove – 2017-08-23T10:21:41.670

1You might be able to golf this to something like s=$'\ua\xa\n';expr 5 - ${#s} / 2. This works on v3 and v4. I don't have working older versions to try right now. – Digital Trauma – 2018-06-20T21:27:01.650

@DigitalTrauma nice! the only new mechanism here is expr, so unless it was upgraded between Bash versions, this should work as well as mine (which I still haven't tested on v1 and v2) – joH1 – 2018-06-21T09:10:31.797

5

C89/C99, 25 bytes, 2 versions, score = 12.5

#include <stdio.h>

int main() {
    int v = 11 //**/ 11
            + 88;
    printf("C%d\n", v);
    return 0;
}

// style comments aren't recognized in C89.

Golfed version:

v(){return 20//**/2
+79;}

Try it online: C89, C99

nwellnhof

Posted 2017-08-16T04:26:47.137

Reputation: 10 037

replace int v() with main(), it's shorter and will actually compile as a complete program! – Andrea – 2017-08-17T16:14:19.290

@Andrea Thanks. AFAIK, it's allowed to post either functions or whole programs. – nwellnhof – 2017-08-17T16:16:40.567

You're correct. – Andrea – 2017-08-17T16:19:36.260

4

R, versions 2 and 3, score: 10.5 points

cat(exists("cite")+2)

This command returns 2 for R 2.x.x and 3 for R 3.x.x. The function cite was added in R version 3.0.0. Therefore, the command exists("cite") returns FALSE for R 2.x.x and TRUE for R 3.x.x.

R, all versions (1, 2, and 3), score: 12⅓ points

e=exists;cat(e("cite")+e("eapply")+1)

The function eapply was introduced in R 2.0.0.

Sven Hohenstein

Posted 2017-08-16T04:26:47.137

Reputation: 2 464

R.version$major. 15 characters. I don't since when it exists. – Rui Barradas – 2017-08-17T17:52:43.950

@RuiBarradas Let me cite the OP: "Your program may not use a builtin, macro, or custom compiler flags to determine the language version." – Sven Hohenstein – 2017-08-17T17:54:17.013

Ok, sorry, I've missed that part. Should I delete the comment? – Rui Barradas – 2017-08-17T17:55:07.057

@RuiBarradas No problem. You don't need to delete the comment. – Sven Hohenstein – 2017-08-17T18:07:38.803

You should handle printing the result. Currently, when run as a full program this doesn't print anything. – JAD – 2017-08-18T09:21:35.633

Also e=exists;cat(e("cite")+e("eapply")+1) is 1 byte shorter (after you handle printing). – JAD – 2017-08-18T09:22:53.437

@JarkoDubbeldam Thanks for pointing out and the improvement hint. – Sven Hohenstein – 2017-08-18T09:49:44.817

No problem. Also take a look at the scoring, its not supposed to be in bytes. These would be 10.5 and 12.333 points respectively. – JAD – 2017-08-18T10:01:46.350

@JarkoDubbeldam Thanks for pointing this out too. – Sven Hohenstein – 2017-08-18T10:20:15.477

4

Python, 196 bytes / 16 versions = score 12.25

The versions are 1.5, 1.6, 2.0, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 3.0, 3.1, 3.2, 3.3, 3.4, 3.5, and 3.6
Unfortunately I had to leave out 2.7 because there aren't any modules in it (as far as I can tell) that aren't in 2.6 but are in 3.0.

i=15
try:
 for m in'os.atexit.os.os.os.warnings.cgitb.heapq.collections._ast.abc.queue.os.os.os.importlib.argparse.lzma.asyncio.zipapp.secrets.'.split('.'):__import__(m);i=i+1
except:print(i/10.)

We loop through a bunch of modules that were introduced in different versions of python, and at the first error we quit and return the version. The gaps between major versions are filled in by repeatedly importing os. The test for python 1.5 relies on string.split not being present until 1.6.

Credit to Olivier Grégoire's answer for the idea of testing for new classes/modules in a loop.

I've now finally tested on all relevant versions of python...which required editing the 1.5 source code to get it to compile...

Gavin S. Yancey

Posted 2017-08-16T04:26:47.137

Reputation: 617

4

Windows' Batch File, 35 bytes / 2 versions = score 17.5

@if /i Z==z @echo NT&exit
@echo DOS

Prints DOS on MS-DOS (duh) and NT on Windows NT. (duh)

Now, for some explanation.

Windows has had batch scripting since MS-DOS times and it hasn't changed much since then. However, when Windows NT came along, Microsoft changed the default interpreter for batch scripts, from COMMAND.COM to cmd.exe (now also allowing the extension .cmd as an alternative to the original .bat).

With that, they also implemented a few changes, such as the /i flag for ignoring string case on conditionals. That is, while Z==z is false, /i Z==z is true.

We exploit that DOS didn't have case insensitivy and compare uppercase Z with lowercase z. By using the /i flag, we end up with a Z==z (false) conditional on DOS and z==z (true) on NT.

Now, I realize that the challenge specifies that a version number should be printed. But, as far as I know, there is no 'version number' to batch scripting, so this is the closest I could get.


Tested on Windows 10, DOSBox and vDos:

Windows 10:

Windows 10

(run with cmd /k to prevend window closing on exit)

DOSBox:

DOSBox

vDos:

vDos

Matheus Avellar

Posted 2017-08-16T04:26:47.137

Reputation: 273

Windows 7 is shorter than Windows NT. – user202729 – 2017-08-21T06:56:20.450

2@user202729 I suppose, but then again, 7 isn't really a language version, it has been the same on all Windows' since 3.1. So I didn't think it would be very fair to call it a 7 when it should maybe even be 3.1 – Matheus Avellar – 2017-08-21T09:20:21.400

3

Wolfram Language/Mathematica 10/11, 37 bytes / 2 versions = 18.5

Consider (Length@DateRange[{1},{1}][[1]]+27)/3, at 37 bytes and working with 2 versions, gives me a score of 18.5.

In[1]:= $Version

Out[1]= "10.4.1 for Microsoft Windows (64-bit) (April 11, 2016)"

In[2]:= (Length@DateRange[{1}, {1}][[1]] + 27)/3

Out[2]= 10

and

In[1]:= $Version

Out[1]= "11.1.1 for Microsoft Windows (64-bit) (April 18, 2017)"

In[2]:= (Length@DateRange[{1}, {1}][[1]] + 27)/3

Out[2]= 11

I'm sure there is a more efficient way, but the discrepancy between the DateRange output bit me in the butt recently, so I was set on using that.

As a follow up, someone could probably take advantage of Length@DateRange[{1}, {1}][[1]] evaluating to 1 in Mathematica versions 1-~8, but I didn't have time to incorporate that.

user6014

Posted 2017-08-16T04:26:47.137

Reputation: 288

2Fairly sure that your answer doesn't meet the requirements of the prompt, namely the last rule due to you using $Version: Your program may not use a builtin, macro, or custom compiler flags to determine the language version. – Amndeep7 – 2017-08-16T14:06:47.123

7I'm only using $Version to demonstrate that it outputs the correct result in the correct version, $Version is not part of my answer... – user6014 – 2017-08-16T14:07:56.940

All good friend - the thing is, you are using something like $VersionNumber, but instead you're calling it $Version. To my mind, while the meat of your program is the Length@DateRange stuff, that wouldn't work without $Version just providing the full version information that you then process, which therefore violates the rules. – Amndeep7 – 2017-08-16T14:12:45.930

@Amndeep7 I see. I think there was some confusion, I edited my previous comment to try and clarify. $Version is not in any way required for my actual line of code to operate. – user6014 – 2017-08-16T14:15:06.940

4@Amndeep7 The submission is the 37 byte code inlined in the first paragraph. The code blocks are only output demonstrations. – PurkkaKoodari – 2017-08-16T14:16:02.760

Ahh I see, my bad - it's been a while since I've looked at Matlab code. I guess I mistakenly thought the {1} stuff was how Matlab allowed one to access the results of a previous line in its REPL. – Amndeep7 – 2017-08-16T14:20:22.873

@Amndeep7 No problem! This isn't even Matlab though, which may have been your first mistake ;) – user6014 – 2017-08-16T14:20:55.153

Now I feel like an even bigger dunce haha – Amndeep7 – 2017-08-16T14:21:45.083

3Explanation: Using different format of time in different versions. That can be golfed more to {1} Tr[1^#&@@%~DateRange~%]/3+9 (31 bytes), or even 7+Length@Now (12 bytes) – user202729 – 2017-08-17T08:34:47.527

3

Ruby 1.x and 2.x, 20 bytes, score 10

p [].to_h&&2rescue 1

Based on the to_h method which was introduced on the Array class in Ruby 2.

Philipp Frank

Posted 2017-08-16T04:26:47.137

Reputation: 131

Nice first answer. I have no 1.x handy to test, but p [].to_h&&2rescue 1 is a bit shorter. – manatwork – 2017-08-17T10:23:04.040

@manatwork Great, saves 3 bytes and works like a charm – Philipp Frank – 2017-08-17T10:31:38.260

3

Julia 0.4, 0.5, 46 bytes, score 22

f(::ASCIIString)=.4
f(::String)=.5
f()=f("")

Julia has changed the type name of the concrete and abstract String types in many versions.

This code in particular take advantage of:

Julia 0.4:

  • Concrete is ASCIIString,
  • Abstract is officially AbstractString,
  • Abstract has deprecated alias to String.
  • Concrete is most specific than abstract so it wins dispatch

Julia 0.5:

  • Concrete is officially String,
  • Concrete has deprecated alias to ASCIIString,
  • Abstract is AbstractString, (though that doesn't matter here)
  • As two methods have been defined for the concrete string type, the latter over-writes the former.

See also my newer more effective solution based on different principles

Lyndon White

Posted 2017-08-16T04:26:47.137

Reputation: 1 021

3

Erlang, 180 bytes, 11 versions, score 16.36

20-length([A||A<-[schedulers,c_compiler_used,cpu_topology,snifs,dynamic_trace,port_count,nif_version,end_time,max_heap_size,atom_count],{'EXIT',_}<-[catch erlang:system_info(A)]]).

With indentation and line breaks:

20-length([A||A<-
                  [schedulers,
                   c_compiler_used,
                   cpu_topology,
                   snifs,
                   dynamic_trace,
                   port_count,
                   nif_version,
                   end_time,
                   max_heap_size,
                   atom_count],
              {'EXIT',_}<-[catch erlang:system_info(A)]]).

Tested on one minor release of each major version since 10:

  • R10B-9
  • R11B-5
  • R12B-5
  • R13B04
  • R14B04
  • R15B03
  • R16B03
  • 17.5.6.2
  • 18.2.1
  • 19.2
  • 20.0

The idea is that every major release has added at least one new allowable argument for the function erlang:system_info, so let's try the ones in the list, count how many of them fail, and subtract the number of failures from 20, which is the current version.

legoscia

Posted 2017-08-16T04:26:47.137

Reputation: 131

3

Japt (1 & 2), 8 6 / 2 = 4 3

'1r\S2

Test v1  |  Test v2

  • 2 bytes saved thanks to Oliver

Explanation

Prior to v2, Japt used a customised RegEx syntax, so we can take advantage of that.

'1

The number 1 as a string.

 r  2

Replace (r) the below with a 2.

\S

Japt 2 sees this as the RegEx /\S/g, which matches the 1. Japt 1 ignores the \ escape character and just sees the S, which is the Japt constant for a space character and, obviously, doesn't match the 1.

Shaggy

Posted 2017-08-16T04:26:47.137

Reputation: 24 623

3

Befunge, score = 3.5

7 bytes, 2 versions

"]"'b.@

Try it online in Befunge-93
Try it online in Befunge-98

"]" is a string literal in both versions, pushing 93 (the ASCII value of [) onto the stack. 'b is a character literal in Befunge-98, pushing 98 (the ASCII value of b), but those are invalid instructions in Befunge-93, so they are simply ignored. We thus end with 93 on the top of the stack in Befunge-93 and 98 in Befunge-98. .@ writes out the value at the top of the stack and then exits.

James Holderness

Posted 2017-08-16T04:26:47.137

Reputation: 8 298

]".@.b' or ]g.@.b' also work – MildlyMilquetoast – 2017-11-09T22:24:41.297

3

Ruby 1.x (<1.9) and 2.x, 10 8 bytes, score = 4

$><<?2%7

Try it:

This works by exploiting the different behaviors of ?x between Ruby 1.x and 2.x. In Ruby 1.x, ?A (for example) returns 65 (the ASCII value of the character A), but in Ruby 2.0 it returns the one-character string "A".

The code above is equivalent to this:

val = ?2
$> << val % 7

In Ruby 1.x (<1.9), the value of val is 50 (the ASCII value of the character 2), a Fixnum. Fixnum#% is the modulo operator, so 50 % 7 returns 1.

In Ruby 2.x, val is the string "2". String#% is an infix version of sprintf, so "2" % 7 is equivalent to sprintf("2", 7), where "2" is the format string. Since the format string doesn't contain any format sequences (e.g. %d), subsequent arguments are discarded and "2" is returned.

Finally, $> is an alias for $stdout, so $> << ... prints the result.

Jordan

Posted 2017-08-16T04:26:47.137

Reputation: 5 001

1Ooh, nice! I was trying to do something like ?A==66?1:2 before I came across your answer – Piccolo – 2018-08-08T01:39:09.013

3

Python 2 and Python 3, 36 34 bytes, score 18 17

print(str(hash(float('-inf')))[1])

In Python 2, the hash of negative infinity is -271828 but in Python 3 it's -314159. Edit: Saved 2 bytes, 1 point of score, thanks to @ArBo.

Neil

Posted 2017-08-16T04:26:47.137

Reputation: 95 035

squints Is this a deliberate e vs pi thing? – Jo King – 2019-05-21T12:37:11.697

@JoKing Yes; apparently when hash was first fixed to work on floating-point infinities the relevant developer used pi1e5 and e-1e5 as the hash values. At some point in Python 3 the has value for negative infinity got changed to be the negation of the hash value for infinity. – Neil – 2019-05-21T12:48:36.353

2

Python 3, Python 2, score 17.5

(35 bytes, 2 versions)

try:exec("print 2")
except:print(3)

Python 2, 35 bytes

Try it online!

Python 3, 35 bytes

Try it online!

Saved 5 bytes thanks to ETHproductions

Not a good code golf answer, but a massive change!

jferard

Posted 2017-08-16T04:26:47.137

Reputation: 1 764

Hmm, can you put each statement on the previous line? I.e try:exec("print 2")\nexcept:print(3) – ETHproductions – 2017-08-16T14:52:19.200

@ETHproductions thanks! I didn't expect to win, thus I was a bit distracted. I mainly wanted to focus on the massive change between Python 2 and 3. – jferard – 2017-08-16T14:59:21.553

2

PHP 4, 5, & 7: 59 bytes, score 19.67

<?list($a[],$a[])=array(5-strrpos('112','11'),7);echo$a[1];

It's way long but it does 3 versions!

list() assigns in backwards order in PHP 4 & 5, strrpos() only searches for a single character in PHP 4.

CJ Dennis

Posted 2017-08-16T04:26:47.137

Reputation: 4 104

2

C++11 and C++14, 96 bytes, score 48

#include<iostream>
#define M(x,...)__VA_ARGS__
int main(){std::cout<<(int[]){M(1'2,1'4,11)}[0];}

The code is based on the digit separator proposal which has very similar example code at the end.

online version

nwp

Posted 2017-08-16T04:26:47.137

Reputation: 241

2

Braingolf, 4 bytes, 2 versions, score = 2

0n6+

Try it online!

n (negate) was added in braingolf v0.7, meaning this will output 7 in v0.7 and newer, and 6 in v0.6 and older

In v0.7, n "negates" the 0 to a 1, then adds it to 6 to make 7, but in v0.6, the n does nothing, and so 0 is added to 6, resulting in 6. Both versions have implicit output.

This is technically the minor version, however there is only 1 major version of Braingolf, as Braingolf v1.0 isn't a thing yet, so this is about as major as the version numbers get.

Skidsdev

Posted 2017-08-16T04:26:47.137

Reputation: 9 656

2

Idea of the solution

Each version hava added a new syntax features, so code vith them will cause a syntax error in smaller versions of the language.

  • ES5: Octal constants 03 are disallowd in strict mode (unused in actual solutions)
  • ES6: Octal constants 0o6 are introduced
  • ES7: Pow operator is introduced: 7**1
  • ES8: Trailing comma in function call is allowed: f(8,)
  • ES9: Tagging of invalid strings is allowed: f`\u9`

So evaluating

eval('9;'+s)

on one of the examples will either return the the value, or throw a SyntaxError. But what value would it return? For all ES6-ES8 snippets the last calculated value will be the version. In ES9 snippet the 9 from 9; is return. So if we place a minimal supported version into catch block like

eval(`try{eval('9;'+s)}catch(e){6}`)

we'll get the behaviour we want.

Ecmascript 5+ (for 5, 6, 7, 8, 9); 124 / 5 = 24.8 points

alert(Math.max.apply(0,'0o6~7**1~f(8,)~f`\\u9`'.split('~').map(function f(s){return eval("try{eval('9;'+s)}catch(e){5}")})))

Tested in:

  • IE8: Error (Object doesn't support property or method 'map')
  • IE 11: 5
  • Edge 14: 8
  • Chrome 60: 8
  • Firefox 55: 9

Ecmascript 6+ (for 6, 7, 8, 9); 96 / 4 = 24 points

alert(Math.max(...'7**1~f(8,)~f`\\u9`'.split`~`.map(f=s=>eval(`try{eval('9;'+s)}catch(e){6}`))))

Tested in:

  • Edge 14: 8
  • Chrome 60: 8
  • Firefox 55: 9

Qwertiy

Posted 2017-08-16T04:26:47.137

Reputation: 2 697

Oh, now this is very nice :) I'm not sure about the inclusion of ES9, though, seeing as the official spec doesn't exist yet. I'd suggest including an explanation to help those unfamiliar with JS to understand what's going on here. Also, could you save any bytes using reduce instead of max? Oh, and this outputs 8 for both in Edge 14, if you want to include it. – Shaggy – 2017-08-18T12:49:08.593

@Shaggy, here is a single feature for ES 2018 which already got out of ES next and I used it.

– Qwertiy – 2017-08-18T12:51:16.700

Ah, I wasn't aware that that had made it out. – Shaggy – 2017-08-18T13:02:21.973

1@Shaggy, added a description. – Qwertiy – 2017-08-18T13:04:24.790

2

APL (Dyalog), score 7.3 or 3

51 bytes for 7 versions or 6 bytes for 2 versions

For the lowest score, distinguish versions 12 and 13 with:

12+≡⍳0

Twelve plus the depth of the first zero integers. Until version 12, ⍳0 (wrongly) returned the index origin as a scalar, i.e. depth 0. In version 13 and up, ⍳0 returns an empty list, i.e. depth 1.


Much more interesting is the following anonymous function which distinguishes all major versions 10 through 16 (the current). It needs a dummy argument (which is ignored) to run.

{2::10⋄⌷2::11⋄_←⎕XML⋄0::+/12⎕ML,≡⍳0⋄≡,/⍬::15⋄16@~0}

{} an anonymous function which allows setting error guards :: with a numeric error code (determining which type of error to catch) on the left, and on the right, the result to be returned in case of error.

2::10 if SYNTAX ERROR, return 10

⌷2::11 materialise (introduced in version 11) 2 (SYNTAX ERROR) which if happens, yields 11

⋄_←⎕XML try: assign XML system function (introduced in version 12) to a dummy name

0:: if any error happens;

  ⍳0 first zero integers (gives 1 until version 12 and empty list from version 13)

   depth of that (0 until 12, 1 from 13)

  12⎕ML, prepend 12 and the Migration Level (0 until 13, 1 from 14)

  +/ sum (12 until 12, 13 in 13, 14 from 14)

≡,/⍬::15 try: concatenate the elements of an empty list (introduced in version 15) and get its depth, 2 (SYNTAX ERROR) which if happens, yields 15

⋄16@~0 try: amend with a 16 at (introduced in version 16) positions where logical NOT yields truth, applied to 0

Try it on TryAPL (slightly modified to pass security) and Try it Online!

Adám

Posted 2017-08-16T04:26:47.137

Reputation: 37 779

2

Lua 5.3.3 / 5.2.4, 32 bytes / 2 = 16 points

print(54-#("a"):gsub("a?$","a"))

This actually abuses a bug in Lua 5.2's string.gsub function: In Lua 5.2, "a?$" would first match "a{endOfString}" and then "{endOfString}" a second time, which is obviously incorrect, because gsub may only match each character/anchor in the string once. This results in "a" being substituted into the string twice, giving a final string length of 2.

In Lua 5.3, the bug is fixed, so "a?$" only matches "a{endOfString}", thus matching the end of the string only once, which is the correct behavior. Therefore, "a" is substituted into the string just once, resulting in a string length of 1.

The difference between the string length and 54 then gives the major/minor version number of the Lua interpreter, which is printed as follows:

Lua 5.2.4: 52
Lua 5.3.3: 53

(I could only test on these two specific versions, but it should work on more)

Jonathan S.

Posted 2017-08-16T04:26:47.137

Reputation: 423

2

Python 1, 2, and 3, 43 bytes, score 14.333

if str(1<2)=="1":print(1)
else:print(3/2*2)

Yes, there are already submission for Python, but this one also differentiates Python 1 and 2.

The difference between Python 3 and 2 are pretty well known, but with Python 1? It's very similar to Python 2.

But I found something.

In Python 1, a true statement evaluates to 1. In Python 2 and 3, it evaluates to True. By turning a true statement into a string, you can differentiate Python 1 and the later versions.

Want

Posted 2017-08-16T04:26:47.137

Reputation: 407

1

VBA, 51 Bytes / 2 Codes = 25.5

Anonymous VBE immediate window function that outputs the VBA program version - works only in Excel 2011 and later. Outputs Version 7 for windows and Version 6 for Mac, as mac has yet to get version 7 of VBA

Uses conditional compilation constant Mac to determine the VBA language version

Sub a()
o=7
#If Mac Then
o=6
#End If
Debug.?o
End Sub

Taylor Scott

Posted 2017-08-16T04:26:47.137

Reputation: 6 709

@Shaggy, missed that, corrected – Taylor Scott – 2017-08-16T17:02:23.527

1

Lua 5.0, 5.1, 5.2, 5.3, score: 17.75

print("5."..(table.getn or load'return #...'){math.fmod,_ENV,math.ult})

math.fmod was math.mod before 5.1
_ENV was introduced in 5.2 for new environment system
math.ult was introduced in 5.3

There was a problem: lua 5.0 don't know # for tables, but lua >= 5.2 don't have function table.getn, so I keep # isolated in string for lua 5.0, and loads in instead of table.getn for lua >= 5.2.

val says Reinstate Monica

Posted 2017-08-16T04:26:47.137

Reputation: 409

1

Julia 0.2-0.7, bytes = 59, versions = 6, score = 9.833

f()=[4,0,3,0,5,2,6,7][endof(subtypes(AbstractArray))-16]/10

Turns out each version of julia has had a different number of abstract array subtypes. So we use the number of subtypes to index into an array that contains the version number. The current nightly (0.7), currently has 25, but it is the fallback case anyway.

Julia 0.1 also has a different number of AbstractArray subtypes I would guess. However, julia 0.1 does not have the subtypes function. So I can't trivially retrieve them.

A significant improvement over my previous answer.

(Thanks @one-minute-more for almost halving the bytecount)

Lyndon White

Posted 2017-08-16T04:26:47.137

Reputation: 1 021

Golfing this to 52 bytes, for a score of 8.7: [4,0,3,0,5,2,6,7][endof(subtypes(AbstractArray))-16]. – one-more-minute – 2017-08-18T17:08:00.713

@one-more-minute thanks. I do need to add some boilerplate, but that is still quiet a saving. – Lyndon White – 2017-08-19T03:29:26.893

1

K/Kona, 6 bytes, 2 versions - score 3

-4+@,0

Version 3 defaults to long numeric types, whereas 2 defaults to integer numerics. We enlist (,) to get a positively typed number (atoms are negative), we get its type (@), and we add that to -4.

Simon Major

Posted 2017-08-16T04:26:47.137

Reputation: 401

1

RProgN 1/2, 4 bytes / 2 versions. Score = 2

1 1+

RProgN1 requires commands to be split into words, where as RProgN2 reads byte by byte by default. As such, RProgN1 can't do anything with 1+, so it just outputs 1, but RProgN2 can execute it, so it adds 1 to the 1, giving 2.

Try RProgN1

Try RProgn2

ATaco

Posted 2017-08-16T04:26:47.137

Reputation: 7 898

1

T-SQL, 41 bytes / 11 versions = 3.7 score

SELECT MAX(cmptlevel)/10FROM sysdatabases

Note that this does not use a SQL "builtin, macro, or custom compiler flag"; that would be something like SELECT @@VERSION or SELECT SERVERPROPERTY('productversion').

Instead, this looks up the highest "compatibility level" for all databases on the server. Existing databases on a server might have older compatibility levels, but the system database tempdb is recreated each time the server is restarted, so will always have the compatibility level of the actual current version. (Using MAX is shorter than including WHERE name='tempdb').

I divide by 10 so it returns the actual SQL version number.

I've successfully tested this on all versions between SQL 2005 and 2017, but I believe this will ultimately work in 11 distinct versions:

Version Number    Release Name
6                 SQL Server 6.0
6.5               SQL Server 6.5
7                 SQL Server 7.0
8                 SQL Server 2000
9                 SQL Server 2005
10                SQL Server 2008 (or SQL Server 2008 R2)
11                SQL Server 2012
12                SQL Server 2014
13                SQL Server 2016
14                SQL Server 2017
15                SQL Server 2019 (Pre-release)

Let me know if anyone has SQL 2000 or older still running to see if this works as expected.

Note that my code uses a deprecated system table for widest compatibility, I'd normally use the newer sys.databases system view introduced in SQL 2005.

BradC

Posted 2017-08-16T04:26:47.137

Reputation: 6 099

1

Japt, 4 bytes / 2 versions = 2

J\+2

Japt | Japt 2.0

Alternative:

2\/2

Japt | Japt 2.0

Oliver

Posted 2017-08-16T04:26:47.137

Reputation: 7 160

1

Wolfram Language (Mathematica), 30 bytes (5<= Score <=10)

Floor[.8+Length@Names@"*"/615]

I tested this on Mathematica 10.2, which has 5683 named symbols and 11.3, which has 6280 names symbols. I used cloud.wolfram.com to test it on Mathematica 12.0 which has 6914 named symbols. With possibly some slight tweaking to the numbers, this could work for as many as 6 versions (see the graph at https://blog.wolfram.com/2018/06/21/weve-come-a-long-way-in-30-years-but-you-havent-seen-anything-yet/) which shows that the function count has grown pretty much linearly since v6.)

Try it online!

Kelly Lowder

Posted 2017-08-16T04:26:47.137

Reputation: 3 225

27 bytes – attinat – 2019-05-22T23:56:39.593

1

Whitespace, 43 bytes, 2 versions, score 21.5

Version 0.3 of Whitespace added the copy and slide instructions. Of those two, slide is exploited here. Slide removes the top n elements of the stack, keeping the top element. The code for version 0.3 slides 2 and version 0.2 ignores the slide and keeps the 2.

SS
T
STSSSTT
SSSTS
SSSTSTTTS
ST
ST
T
SST
ST 

Written as more readable Whitespace assembly:

push 0   # Push 0 to the stack
printi   # Print 0 as an integer
push 3   # Push 3 to the stack
push 2   # Push 2 to the stack
push '.' # Push 46 ('.') to the stack
slide 1  # Pop 2 if version 0.3 or skip instruction
printc   # Print 46 as character '.'
printi   # Print 2 or 3 as integer

andrewarchi

Posted 2017-08-16T04:26:47.137

Reputation: 408

0

Julia: 21 bytes, score: 10.5

(length([1:2])+V)/10   # where V takes value critical version minus 1

I cannot quite remember which version of julia this changed in (stuff gets deprecated very often), but there was some point in the language where this:

julia> [1:2]
2-element Array{Int64,1}:
 1
 2

became this:

julia> [1:2]
1-element Array{UnitRange{Int64},1}:
 1:2

from one version* to the next. I think it was probably from v0.3 to v0.4? (in which case V = 2)


* julia is still in major version 0

Tasos Papastylianou

Posted 2017-08-16T04:26:47.137

Reputation: 233

0

Mathematica 10/11 20 Bytes, Score=10

Since Echo was introduced in version 11

10+Echo@1/.Echo@1->0

Can extend to version 9 as well (since AbsArg was intro'd in V10) with

10-Last@AbsArg@1+Echo@1/.Echo@1:>0

but the score goes up to 34/3 or 11.33

Kelly Lowder

Posted 2017-08-16T04:26:47.137

Reputation: 3 225

0

SmileBASIC (2 and 3), 9 bytes

Thank you 12Me21 for helping out.

?3+CANCEL

In SB, variable declaration rules are lazy by default; if a variable name is not declared or assigned anywhere in scope, it is assumed to be 0. There's also a handful of reserved system variables and constants which varies between version and platform.

In SB v2, CANCEL was a reserved variable that represented -1; this was intended for use with RESULT to check the state of a dialog box or other action. In SB v3, this variable is not present.

Thus, when this program is run on a standard SB v2 environment it prints 2, and on a v3 environment it prints 3.

snail_

Posted 2017-08-16T04:26:47.137

Reputation: 1 982

0

05AB1E/05AB1E (legacy)/2sable, score 9 8⅔ (26 bytes with 3 versions)

•äƵí•hR®di’ÿ (»Š)’ë’2sˆ¢’r

Try it online in 2sable
Try it online in 05AB1E (legacy)
Try it online in 05AB1E

Explanation:

Let's start with a bit of history of these three versions. The development of 05AB1E started at the start of 2016 (or actually, the very first git-commit was on December 21st, 2015). This new codegolf language was being built in Python as backend. Mid 2016 2sable as branched of that current 05AB1E version (July 7th, 2016 to be exact), and the strength of 2sable in comparison to that old 05AB1E version was added back then: implicit inputs. Later on implicit input was also added to 05AB1E, and 2sable was basically a forgotten version right after it was created on that day July 7th, 2016.
Then in mid-2018, a new 05AB1E version was being started, this time completely rewritten in Elixir instead of Python, with loads of new builtins added and some builtins changed or even removed.

So, let's go over the code and see what it does in each of the three versions:

•äƵí•              # Legacy 05AB1E / new 05AB1E: push compressed integer 14793296
                   # 2sable: no-op (compressed integers didn't exist yet), 
                   #         so nothing is pushed to the stack
     h             # Convert this from an integer to a hexadecimal string: "E1BA50"
      R            # Reverse this string: "05AB1E"
                   # (2sable: the stack remains empty for `h` and `R`)
       ®           # Push -1
        d          # 2sable: check if -1 only consist of digits (falsey)
                   # 05AB1E (legacy): check if -1 is an integer (truthy)
                   # New 05AB1E: check if -1 is a non-negative integer ≥0 (falsey)
         i         # If it is truthy:
          ’ÿ (»Š)’ #  Push dictionary string "ÿ (legacy)",
                   #  where the `ÿ` is automatically filled with the top of the stack
         ë         # Else:
          ’2sˆ¢’   #  Push dictionary string "2sable"
                r  #  And reverse the stack
                   #  (05AB1E: stack changes from "05AB1E","2sable" to "2sable","05AB1E"
                   #   2sable: stack stays the same "2sable")
                   # (after which the top of the stack is output implicitly as result)

See this 05AB1E tip of mine (sections How to use the dictionary? and How to compress large integers?) to understand why ’ÿ (»Š)’ is "ÿ (legacy)", ’2sˆ¢’ is "2sable", and •äƵí• is 14793296.

Kevin Cruijssen

Posted 2017-08-16T04:26:47.137

Reputation: 67 575

Out of curiosity, is there a name for this sort of table/explanation method? Where the lines are ordered by English logic, but commands are padded to their place in the code. – Geza Kerecsenyi – 2019-11-20T20:34:22.870

@GezaKerecsenyi I to be honest have no idea. I kinda made it up for my answers tbh. I know some other people do their comments similarly, but no idea if it's a known convention and it has a name. – Kevin Cruijssen – 2019-11-20T21:29:57.030

OK, thanks anyway! – Geza Kerecsenyi – 2019-11-20T21:30:36.723

0

Red, 14 bytes all versions

system/version

LeakySpoon

Posted 2017-08-16T04:26:47.137

Reputation: 1

4Welcome to the site! I'm not familiar with Red, but does this violate the rule "Your program may not use a builtin, macro, or custom compiler flags to determine the language version."? – caird coinheringaahing – 2020-01-25T20:50:31.203