by Thorentis · 4y ago · 0 comments
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.)
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"})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.
The fact that simple reusable utility functions were/are complicated does make it feel half baked to me too.