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 >>> 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)
3 class C:
def __len__(self): ...
def __getitem__(self, i): ...
def __contains__(self, x):
i = bisect.bisect_left(self, x)
return i < len(self) and self[i] == x
At that point it's just syntactic sugar.(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.
You 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.
print("quux" in {"foo", "bar", "baz"})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.
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.
The fact that simple reusable utility functions were/are complicated does make it feel half baked to me too.
Go made multiple decisions that make me face palm. It was so close to being a great systems programming alternative.
A pythonic C++ that doesn’t smell bad.
I wish Go had classes. I like those damn things. Extending a Dog from Animal with all the methods and data in one place is nice.
Obligatory...try Rust?
But actually, try python with mypy and mypyc. It's still a little experimental, but if you stick to simple constructs it works well, and you can compile straight python to fast, static libraries, without using goofy cython syntax.
In your new struct definition, include the desired base type as a member - without giving it a name. That embeds the unnamed member directly, and your new type automatically gets all the interfaces and their underlying implementations. As a python-person I always found this a somewhat odd mechanism, but from a functionality perspective it satisfies what I have wanted to do.
Guess that's what people mean by composability.
Closer to 50.