But then yea, 1 line of python using an inbuilt function solves a 10 line go routine in a hackerrank.
Second: it's not a good function to use. Checking if something is present in an array is not something you should do often and thoughtlessly. It has its uses, I agree, but in general a "dict", or if you really need an array, binary search, should be used. Where's the one liner for that in python? O wait, there isn't one. Go does have it, though, since a long time.
But it's a good thing we no longer use LoC as a measure.
Here it is:
>>> import bisect
>>> sorted_fruits = ['apple', 'banana', 'orange', 'plum']
>>> bisect.bisect_left(sorted_fruits, 'banana')
1(Of course, for a custom class you can just wrap it in a __contains__ method and then just use the “in” operator.)
True. However, the hardest part of the binary search algorithm is implemented through bisect, so it still saves a lot of developer time.
>>> class C:
... def __len__(self):
... return 10
... def __getitem__(self, i):
... if i >= 10:
... raise IndexError('out of range')
... return i+1
...
>>> c = C()
>>> import bisect
>>> bisect.bisect_left(c, 1)
0
>>> bisect.bisect_left(c, 4)
3You could maybe count the import, but you'd have to do that once and could do multiple bisections, so it's amortized.
Setting the variable doesn't count because you do, of course, need a variable to perform an operation on a variable. Sorting the array also wouldn't count because you can't use binary search if the array isn't sorted.
For dict/set hash lookup, O(1):
x in s
For binary search on a sorted python list, it takes a standard library import (bisect), and the containment test is: (i:=bisect.bisect_left(a, x)) < len(a) and a[i] == xSo, yes, let's have a contains function on (unsorted) arrays.
And, contains is a perfectly cromulent function to use unless there's a reason not to. At a million items it would be a bad use of contains if you were to lookup up multiple items, but modern day programming requires both knowledge of the code structure and the data.
Yes, python's type hinting is pants.
Go's goals are simplicity, python wants to be the working man's language.
print("quux" in {"foo", "bar", "baz"})