14
\$\begingroup\$

I am not sure if this question has been asked before, but I was unable to find the answer. My question focuses primarily on Python, but it might be applicable to other languages too.

Consider the following function which takes 5 arguments and prints them:

def f(a,b,c,d,e):
 for i in a,b,c,d,e:print(i)          # 46 bytes

This function, if called as such:

f('1','2','3','4','5')

Outputs:

1
2
3
4
5

The same functionalty can also be achieved with a lower byte count:

def f(**x):
 for i in 'abcde':print(x[i])         # 41 bytes

However, if I want to generate the same output, I must now call the function as follows:

f(a='1',b='2',c='3',d='4',e='5')

My question now: Is the requirement for keyworded arguments an appropriate way to reduce the byte count?

I am asking this, because it helped me to save some significant bytes in the challenge Hidden sentences in the license plates.

\$\endgroup\$

1 Answer 1

11
\$\begingroup\$

Using kwargs is fine if it is not encoding extra information

I think there are two reasons that using this feature of Python seems fishy. The first is that this feature is exclusive to relatively newer programming languages, which might seem unfair in comparison. The other reason is that it seems to use more information than other methods of handling information. I don't think either of these are actually problematic, however.

The first reason is fairly easy to dismiss: Python answers shouldn't be compared to C answers, etc. This is just a clever way of accessing information, as if the keys you require uniquely identify an argument they aren't any different from looking for that same argument in a particular position in an array, for example.

The second reason brings up an important abuse case, which would be using the argument to contain additional information. For example, this program tests if the input is prime. Obviously here it doesn't save bytes, but almost any code golf challenge with a single argument could have the same score. This is clearly problematic, but I think the usual rider of "contains no extra information" applied to flexible input formats handles this case just as well.

\$\endgroup\$
1
  • \$\begingroup\$ I was mostly concerned about the first point and I had not considered the second one yet. This seems like a very reasonable approach. Thanks for your answer! \$\endgroup\$
    – Jitse
    Commented Aug 22, 2019 at 8:24

You must log in to answer this question.

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