10
\$\begingroup\$

There are lots of questions about whether to count the bytes in an import statement, etc. This question is not about that. Instead, this question is more inline with this question but I think it would be considered off topic/distinct.

Again, assume standard library imports are allowed at least, and that the byte count of the whole import is included in the score. The question arose here where many languages have libraries that provide a ready made solution. Now, is importing the solution enough, or does the required function need to be reassigned or evaluated?

When considering the consensus about builtin functions, it seems builtin solutions are acceptable in two situations:

  1. The builtin is evaluated.
  2. The builtin is assigned to a new name.

My gut feeling is that just importing a library is the same as doing neither of the above and claiming the builtin solves it for 0 bytes.

For instance, in Python, the NumPy library provides the function eye which returns the nxn identity matrix.

from numpy import*

seems like cheating, but by the wording of the other discussion, I think it could be argued that it's valid.

My gut feeling is that this should not be allowed, but by the wording of the discussion about builtins it looks like it should be. Look at the wording of these two articles assignments and imports:

Assignment statements are used to (re)bind names to values

  • Assignment

The import statement ... searches for the named module, then it binds the results of that search to a name in the local scope.

So is

from library import func

equivalent to

func=builtin_func

and therefore acceptable as an answer? And if, technically, it is, should we put it on the list of banned loopholes?

\$\endgroup\$

2 Answers 2

14
\$\begingroup\$

Score is the length of the import plus the length of the function name

This seems to be the standard around here. If there exists a built-in function (we'll call it foo) that solves the task, and built-ins are not forbidden, then, for a challenge that accepts functions and not just full programs, the solution would be:

Python, 3 bytes

foo

Similarly, if foo resides in a module named bar, the solution would be:

Python, 20 bytes

from bar import*
foo

(technically it would be shorter to do import bar\nbar.foo, but that's beside the point)

Though this is specifically aimed at Python, it's easy enough to generalize to other languages. Consider a challenge where the goal is to output the number of characters in an ASCII string. A C solution would be:

C, 25 bytes

#include<string.h>
strlen
\$\endgroup\$
10
  • \$\begingroup\$ Yes, I just used Python because I'm not as familiar with other languages' syntaxes. \$\endgroup\$
    – Ogaday
    Feb 4, 2016 at 14:33
  • \$\begingroup\$ @Ogaday Nothing wrong with that. Python makes for some nice, simple examples. \$\endgroup\$
    – user45941
    Feb 4, 2016 at 14:34
  • \$\begingroup\$ Your C code causes a compilation error, surely that shouldn't be a valid answer? The main problem I have in deciding what should be allowed is that as far as I can tell in python there isn't a difference between from foo import bar and def bar.... other than the first way allows a sort of cheaty meta golfscript library idea, and as a reason "it's boring" is not really satisfying, even if it is necessary sometimes. \$\endgroup\$ Feb 4, 2016 at 15:41
  • \$\begingroup\$ foo is considered valid for Python because to can prepend an assignment (f=) to create a user-defined function. You cannot do f=strlen in C. \$\endgroup\$
    – Dennis
    Feb 4, 2016 at 15:42
  • \$\begingroup\$ @Dennis You could work with a function pointer in C. \$\endgroup\$ Feb 4, 2016 at 15:47
  • 1
    \$\begingroup\$ @MartinBüttner Right, something like size_t (*f)(const char *str) = strlen; would work (and be a valid submission). That still leaves the issue that the submitted code doesn't compile though. \$\endgroup\$
    – Dennis
    Feb 4, 2016 at 16:06
  • \$\begingroup\$ Ok I learned something weird today, if you click edit on Ideone you actually change where the link points, you don't create a new one, so my old link is borked now because I fixed it, but I don't want to delete the rest of the comment so here's a link to the error again... Sorry :P \$\endgroup\$ Feb 4, 2016 at 16:13
  • 4
    \$\begingroup\$ @Dennis Well, function submissions normally end in expressions that evaluate to a function which could either be captured in a name or be called directly. \$\endgroup\$ Feb 4, 2016 at 16:13
  • \$\begingroup\$ Isn't the third example (the C one) 25 bytes? \$\endgroup\$
    – Downgoat
    Feb 12, 2016 at 0:48
  • \$\begingroup\$ @Downgoat Err, yeah. I was counting a trailing line feed for some reason. Temporary insanity. \$\endgroup\$
    – user45941
    Feb 12, 2016 at 0:51
0
\$\begingroup\$

It depends. The question is whether the library is part of the language. If it is, merely specifying an import-clause is the most idiomatic solution and we should not penalize that. (Individual challenge authors may penalize it anyway but it’s their decision.)

If the imported library is a third-party plugin, however, i. e. not part of the programming language, you are outsourcing the implementation. Several loopholes forbidden by default try to prevent this.

It is therefore consistent that just importing a third-party symbol is not sufficient, but you will at least need to define your own symbol, too.


For instance, as far as I understand, the numpy library is a third-party library independently developed from the Python language. The existence of numpy is not guaranteed with any Python implementation. Hence

from numpy import *

is an incomplete implementation of the n×n identity matrix function. You will need to declare a user-defined function.

On the other hand the C programming language defines (since C99) that (unless an implementation defines the __STDC_NO_COMPLEX__ symbol) the function carg is available via the complex.h header. Therefore

#include <complex.h>

is a “complete” and in particular idiomatic implementation of the complex argument function.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .