Related: On scoring imported functions
I knew that if I want to use some imported functions, I had to include "import" statement in the byte count. For example, I want to find out the most common character in a string:
from collections import*
lambda x:Counter(x).most_common(1)[1]
As the Counter
function is imported from collections
, I need to include from collections import*
into my byte count.
But what should I do if I'm only using some method on inputted object, which you need the "import" to construct the input object?
For example, I want to calculate sum of two fractions:
from fractions import*
f = lambda x,y:x+y
f(Fraction(1, 2), Fraction(1, 3))
The function will not cause any error without the import. But you cannot invoke it without the import.
Also, the same thing may happen to third-party libraries too. Another example is, I want to find out all positive number in an array:
import numpy as np
f = lambda x:x[x>0]
f(np.array([1,3,-2,5,-4]))
So, how should I score above codes?
- Is the
fractions
answer valid? - Should I include
import fractions
in byte count? - Is the
numpy
answer valid? - Should I include
import numpy
as in byte count?
Fraction
s, and if they aren't allowed, then even with animport
it wouldn't be valid. But there are other more nuanced cases where this might apply so I won't answer until I can get a more satisfactory complete answer. \$\endgroup\$np.array
since most questions are not targeted only Python language. \$\endgroup\$numpy
" in the title for fairness of scoring, I suppose. But I'll have to think about this more; this is a very good question. \$\endgroup\$numpy
" for the last case. But language name never contributes to byte count... (as long as you are not using something like MetaGolfScript) \$\endgroup\$import
(or similar mechanism), then it's not necessary to include the bytes for theimport
in your byte count, even if theimport
is needed to construct objects of the type that your function expects.' \$\endgroup\$