Python built-in functions time/space complexity - python-3.x

For python built-in functions such as:
sorted()
min()
max()
what are time/space complexities, what algorithms are used?
Is it always advisable to use the built-in functions of python?

As mentioned in comments, sorted is timsort (see this post) which is O(n log(n)) and a stable sort. max and min will run in Θ(n). But, if you want to find both of them in a solution, you can find them using 3n/2 comparison instead of 2n. (Although in general they are in O(n)). To know more about the method see this post.

Related

What are the upsides of generators in python 3?

I know that you can use generators/list comprehensions as filters. You can do a lot with lists but what can you do with generators? Python would only make such thing as a generator if it is useful.
The biggest benefit of a generator is that it doesn't need to reserve memory for every element of a sequence, it generates each item as needed.
Because of this, a generator doesn't need to have a defined size. It can generate an infinite sequence if needed.

Gurobi: Add log term and exponential term be in objective function?

Is it possible to have exponential terms in objective function in Gurobi?
I want to optimize the following objective function:
min_x L2_norm(x-y) + log(b+ exp(k+x))
Gurobi does provide support for log and exponential functions in constraints, but I couldn't find anything for the objective function.
I am using Gurobi's python API.
The solution to formulate this is to add a new auxiliary variable and assign the value of the expression to this variable. This post in the Gurobi Community explains it pretty well.

Way to use bisect module for sets in python

I was looking for something similar to lower_bound() function for sets in
python, as we have in C++.
Task is to have a ds, which inserts element in sorted manner, storing only single instance of each distinct value, and returns the left neighbor of a given value, both operations in O(logn) worst time in python.
python: something similar to bisect module for lists, with efficient insertion may work.
sets are unordered, and the standard lib does not offer tree structures.
Maybe you could look at sorted containers (3rd party lib): http://www.grantjenks.com/docs/sortedcontainers/ it might offer a good approach to your problem.

Searching an unsorted vector (c++)

What is the fastest method for searching an unsorted vector?
Does it take longer to just search the vector from start to end or use std::sort then use a binary search algorithm?
If you need to check if a vector is sorted then the fastest method would be to use std::is_sorted(). It's linear time.
If you need to sort it, you std::sort() which is the most efficient. O(n * log n)
So, checking is faster then sorting itself.

Efficient boolean reductions `any`, `all` for PySpark RDD?

PySpark supports common reductions like sum, min, count, ... Does it support boolean reductions like all and any?
I can always fold over or_ and and_ but this seems inefficient.
this is very late, but all on a set of boolean values z is the same as min(z) == True and any is the same as max(z) == True
No the underlying Scala API doesn't have it so the Python one definitely won't. I don't think they will add it either as it's very easy to define in terms of filter.
Yes using fold would be inefficient because it won't parallelelize. Do something like .filter(!condition).take(1).isEmpty to mean .forall(condition) and .filter(condition).take(1).nonEmpty to mean .exists(condition)
(General suggestion: the underlying Scala API is generally more flexible than Python API, suggest you move to it - it also makes debugging much easier as you have less layers to dig through. Scala means Scalable Language - it's much better for scalable applications and more robust than dynamically typed languages)

Resources