arrays

Most efficient way to sum up an array of integers

爱⌒轻易说出口 提交于 2021-02-19 03:38:45
问题 I am trying to find a sub- O(n) method to calculate the sum of an integer array ~~~(instead of iterating through 0 - n , I am doing it in n/2 )~~~ I'm still doing it in O(n) . public static int sum(int[] s) { int length = s.length - 1; int half = length/2; int sum = 0; for(int i = 0; i <= half; i++) { System.out.println(i + " " + s[i] + " + " + s[length - i]); sum += s[i] + s[length - i]; } return sum; } My algorithm works for even number of integers, however, when there are odd number of

complementary slicing in a numpy array

[亡魂溺海] 提交于 2021-02-19 03:36:47
问题 If I have a numpy array for example : A = np.array([[3, 2], [2, -1], [2, 3], [5, 6], [7,-1] , [8, 9]]) I would like to separate the part of the array with the subarrays having -1 from the ones who don't. Keep in mind that I'm working on very big data set, so every operation can be very long so I try to have the most effective way memory and CPU-time wise. What I am doing for the moment is : slicing1 = np.where(A[:, 1] == -1) with_ones = A[slicing1] slicing2 = np.setdiff1d(np.arange(A.shape[0]

Is Chrome's JavaScript console lazy about evaluating arrays?

强颜欢笑 提交于 2021-02-19 03:21:12
问题 I'll start with the code: var s = ["hi"]; console.log(s); s[0] = "bye"; console.log(s); Simple, right? In response to this, Firebug says: ["hi"] ["bye"] Wonderful, but Chrome's JavaScript console (7.0.517.41 beta) says: ["bye"] ["bye"] Have I done something wrong, or is Chrome's JavaScript console being exceptionally lazy about evaluating my array? 回答1: Thanks for the comment, tec. I was able to find an existing unconfirmed Webkit bug that explains this issue: https://bugs.webkit.org/show_bug

ES6 conditional if statement to check if arrays are empty [duplicate]

女生的网名这么多〃 提交于 2021-02-19 03:13:41
问题 This question already has an answer here : way to embed if expressions inside JSX code? (1 answer) Closed 4 years ago . I can't seem to get my jsx es6 react if statement to work.. What am I doing wrong? const otherVariables = doesntMatter; return ( ... <div> {if (props.student.length == null && props.teacher.length == null) => ( <p>empty</p> ) : ( <p>not empty</p> )} </div> ... ) How can i check if both arrays are empty? 回答1: There is a syntax error, you are testing an lambda expression. You

optimizing array loop in c

孤街醉人 提交于 2021-02-19 02:38:19
问题 I have looked online and in my books but I can't seem to get this. I was asked to optimize a small part of a program. Specifically to take an array and add its contents within a small amount of time, with vi and gcc, without using the built-in optimizer. I have tried loop unrolling and a couple of other optimizations meant for products. Can you please help? int length = ARRAY_SIZE; int limit = length-4; for (j=0; j < limit; j+=5) { sum += array[j] + array[j+1] + array[j+2] + array[j+3] +

Is there a significant overhead in calling `np.asarray' on a NumPy array?

假如想象 提交于 2021-02-19 02:26:29
问题 I am quite new to the Python world, so please excuse my dumb question. In a number of circumstances, I implement a functions that works on array-like numerical inputs, and it is usually advantageous to make use of NumPy utilities for basic operations on the sequences. To this end, I would write something like this: import numpy as np def f(x): if not isinstance(x, np.ndarray): x = np.asarray(x) # and from now on we know that x is a NumPy array, with all standard methods (Note that I don't

Laravel flatten and pluck from multidimensional collection

我的未来我决定 提交于 2021-02-19 02:00:14
问题 I have to retrieve just an array of id from the given collection, something like [10,54,61,21,etc] . I've tried flatten , pluck , but nothing seems to work apart from a foreach which is something I would like to remove at this step. // Model class Children extends Eloquent { public function directChildrens(){ return $this->hasMany('App\Children','father_id','id')->select('id','father_id'); } public function childrens(){ return $this->directChildrens()->with('childrens'); } } // Controller

How to flatten an array to a string of the values?

橙三吉。 提交于 2021-02-19 01:56:10
问题 I have an array that looks like this. 'keyvals' => array 'key1' => 'value1' 'key2' => 'value2' 'key3' => 'value3' Is there a cool way to flatten it to a string like 'value1 value2 value3' ? I also have access to PHP 5.3 if there's something new there. 回答1: $someArray = array( 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' ); implode(' ', $someArray); // => "value1 value2 value3" 回答2: See implode: $flat = implode(' ', $array['keyvals']); 回答3: If you have to flatten this array to

Removing NaNs in numpy arrays

瘦欲@ 提交于 2021-02-19 01:42:10
问题 I have two numpy arrays that contains NaNs: A = np.array([np.nan, 2, np.nan, 3, 4]) B = np.array([ 1 , 2, 3 , 4, np.nan]) are there any smart way using numpy to remove the NaNs in both arrays, and also remove whats on the corresponding index in the other list? Making it look like this: A = array([ 2, 3, ]) B = array([ 2, 4, ]) 回答1: What you could do is add the 2 arrays together this will overwrite with NaN values where they are none, then use this to generate a boolean mask index and then use

May I treat a 2D array as a contiguous 1D array?

一曲冷凌霜 提交于 2021-02-19 01:35:32
问题 Consider the following code: int a[25][80]; a[0][1234] = 56; int* p = &a[0][0]; p[1234] = 56; Does the second line invoke undefined behavior? How about the fourth line? 回答1: It's up to interpretation. While the contiguity requirements of arrays don't leave much to the imagination in terms of how to layout a multidimensional arrays (this has been pointed out before), notice that when you're doing p[1234] you're indexing the 1234th element of the zeroth row of only 80 columns. Some interpret