min

How can I get the minimum and the maximum element of a list in python

情到浓时终转凉″ 提交于 2019-12-04 02:05:52
问题 If a have a list like: l = [1,2,3,4,5] and I want to have at the end min = 1 max = 5 WITHOUT min(l) and max(l) . 回答1: The fastest approach I can think of would be to sort the original list and then pick the first and last elements. This avoids looping multiple times, but it does destroy the original structure of your list. This can be solved by simply copying the list and sorting only the copied list. I was curious if this was slower than just using max() and min() with this quick example

numpy.max or max ? Which one is faster?

三世轮回 提交于 2019-12-03 22:05:09
In python, which one is faster ? numpy.max(), numpy.min() or max(), min() My list/array length varies from 2 to 600. Which one should I use to save some run time ? Well from my timings it follows if you already have numpy array a you should use a.max (the source tells it's the same as np.max if a.max available). But if you have built-in list then most of the time takes converting it into np.ndarray => that's why max is better in your timings. In essense: if np.ndarray then a.max , if list and no need for all the machinery of np.ndarray then standard max . I was also interested in this and

Why are there differing definitions of INT64_MIN? And why do they behave differently?

三世轮回 提交于 2019-12-03 20:26:08
The stdint.h header at my company reads: #define INT64_MIN -9223372036854775808LL But in some code in my project, a programmer wrote: #undef INT64_MIN #define INT64_MIN (-9223372036854775807LL -1) He then uses this definition in the code. The project compiles with no warnings/errors. When I attempted to remove his definition and use the default one, I got: error: integer constant is so large that it is unsigned The two definitions appear to be equivalent. Why does one compile fine and the other fails? -9223372036854775808LL is not a single literal. It's an expression consisting of a unary -

Algorithm splitting array into sub arrays where the maximum sum among all sub arrays is as low as possible

核能气质少年 提交于 2019-12-03 20:18:03
问题 Let's say we have an array of ints: a = {2,4,3,5} And we have k = 3. We can split array a in k (3) sub arrays in which the order of the array cannot be changed. The sum of each sub array has to be as low as possible so that the maximum sum among all sub arrays is as low as possible. For the above solution this would give {2, 4}, {3}, {5} which has a maximum sum of 6 (4 + 2). A wrong answer would be {2}, {4, 3}, {5}, because the maximum sum is 7 (4 + 3) in this case. I've tried creating a

fastest way to get Min from every column in a matrix?

自作多情 提交于 2019-12-03 19:52:39
问题 What is the fastest way to extract the min from each column in a matrix? EDIT: Moved all the benchmarks to the answer below. Using a Tall, Short or Wide Matrix: ## TEST DATA set.seed(1) matrix.inputs <- list( "Square Matrix" = matrix(sample(seq(1e6), 4^2*1e4, T), ncol=400), # 400 x 400 "Tall Matrix" = matrix(sample(seq(1e6), 4^2*1e4, T), nrow=4000), # 4000 x 40 "Wide-short Matrix" = matrix(sample(seq(1e6), 4^2*1e4, T), ncol=4000), # 40 x 4000 "Wide-tall Matrix" = matrix(sample(seq(1e6), 4^2

Python-How to determine largest/smallest int/long/float/complex numbers my system can handle [duplicate]

烈酒焚心 提交于 2019-12-03 14:50:45
This question already has an answer here: Maximum and Minimum values for ints 8 answers Specs: Python 3.3.1 What I was trying to do: "Using Python, determine the largest and smallest ints, longs, floats, and complex numbers that your system can handle." What I did: I went through Python's math modules and all built-in functions relating to math and numbers, but couldn't find a way to do this. I also tried something like max(range(0,)) but it returned ValueError: max() arg is an empty sequence error. Question: How to determine largest/smallest int/long/float/complex numbers my system can handle

which.max ties method in R

百般思念 提交于 2019-12-03 11:41:59
which.max and which.min will return the smallest index of the max or min value if there are ties. Is there a way around this so that the largest index is returned without affecting the efficiency of the function? max.col has this exact functionality, but I am dealing with a vector not a matrix. You could do like this: x<-c(1,2,1,4,3,4) #identical to which.max, except returns all indices with max which(x==max(x)) [1] 4 6 z<-which(x==max(x)) z[length(z)] [1] 6 #or with tail tail(which(x==max(x)),1) [1] 6 edit: Or, you could also use max.col function for vectors like this: max.col(t(x),"last") [1

Manage empty list/invalid input when finding max/min value of list (Python)

会有一股神秘感。 提交于 2019-12-03 09:25:57
I'm finding max value and min value of a list by using max(list) and min(list) in Python. However, I wonder how to manage empty lists. For example if the list is an empty list [] , the program raises 'ValueError: min() arg is an empty sequence' but I would like to know how to make the program just print 'empty list or invalid input' instead of just crashing. How to manage those errors? Catch and handle the exception. try: print(min(l), max(l)) except (ValueError, TypeError): print('empty list or invalid input') ValueError is raised with an empty sequence. TypeError is raised when the sequence

SELECT min and max value from a part of a table in MySQL

社会主义新天地 提交于 2019-12-03 08:05:25
If I want to select min and max values from a whole table I can use this: SELECT min(price) as min_price, max(price) as max_price FROM `prices` But how to select min and max values from just a part of a table? For example, I have 30 rows in a table. I want to select min and max values from first ten rows, then from second ten rows and then form the last 10. I've tried something like SELECT min(price) as min_price, max(price) as max_price FROM `prices` LIMIT 0,10 but this did not work. How can I solve this problem with minimum queries? reggie SELECT MIN(PRICE) AS MinPrice, MAX(PRICE) AS

Minimum steps to set a series of rotating dials to a given sequence

可紊 提交于 2019-12-03 07:54:26
问题 So I have the following problem: There are n rotating dials each set to some number between 0-9 and they need to be matched with another series of n numbers(also between 0-9). One step of rotation is rotating any number of consecutive dials up or down by one step. The dials wrap around 9. i.e. rotating one step up from 9 gives 0 and vice versa. I need to find the minimum number of steps to match the initial configuration with the given configuration. Ex: Initial -> 154 Given -> 562 1. first