4
\$\begingroup\$

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?

  1. Is the fractions answer valid?
  2. Should I include import fractions in byte count?
  3. Is the numpy answer valid?
  4. Should I include import numpy as in byte count?
\$\endgroup\$
6
  • 1
    \$\begingroup\$ IMO if the question allows taking input as a native or built-in rational type you can probably just assume the function parameters are Fractions, and if they aren't allowed, then even with an import 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\$
    – hyper-neutrino Mod
    May 19, 2021 at 2:45
  • 1
    \$\begingroup\$ I'm discussing that the question only says "input as in any reasonable format" or something like it. And I believe if the question require an 1d collection as input (typically Array, LinkedList, Iterable, Iterator, Generator, ...), it will not specially ban np.array since most questions are not targeted only Python language. \$\endgroup\$
    – tsh
    May 19, 2021 at 2:53
  • 2
    \$\begingroup\$ I guess it might make sense to include "Python + 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\$
    – hyper-neutrino Mod
    May 19, 2021 at 2:59
  • 1
    \$\begingroup\$ @hyper-neutrino Yes, language name should be "Python + numpy" for the last case. But language name never contributes to byte count... (as long as you are not using something like MetaGolfScript) \$\endgroup\$
    – tsh
    May 19, 2021 at 3:02
  • \$\begingroup\$ Does this answer your question? 'If your function is syntactically valid without the import (or similar mechanism), then it's not necessary to include the bytes for the import in your byte count, even if the import is needed to construct objects of the type that your function expects.' \$\endgroup\$
    – Dingus
    May 19, 2021 at 23:27
  • 1
    \$\begingroup\$ @Dingus didn’t find out it. I’m going to close this as a dupe. As the two answer under it are both related here. \$\endgroup\$
    – tsh
    May 20, 2021 at 0:05

1 Answer 1

1
\$\begingroup\$

Count imports only if the function definitely doesn't work without directly importing it

Using the OP's Fraction code as an example:

from fractions import*
f = lambda x,y:x+y
f(Fraction(1, 2), Fraction(1, 3))

What if I change the example function call to the following? It imports a hypothetical third-party library frac_consts which contains definitions of one_half = Fraction(1,2); one_third = Fraction(1,3), and does not import Fraction directly, but it works totally fine (and adds two fractions as intended):

from frac_consts import*
f = lambda x,y:x+y
f(one_half, one_third)

Therefore, from fractions import* is not necessary to add two Fractions. This conclusion is consistent with this answer on a previous discussion; an import that is necessary to construct the arguments but not required to define the function may be excluded from the byte count.

Under this reasoning, both answers in the OP's code are valid, and can be scored as lambda x,y:x+y and lambda x:x[x>0], respectively.

\$\endgroup\$
1
  • \$\begingroup\$ Sorry for 1 minutes later... I had closed this question for a dupe of previous one. Feel free to add your thought to answer under that question or vote under that one. \$\endgroup\$
    – tsh
    May 20, 2021 at 0:07

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