remove all negative elements from an array buffer of integers in scala - arrays

I have a question about coding. There are similar types of questions in the database which I came across but none of them clears my doubt. I am going thru the book of "Scala for Impatient". The code below removes negative elements from the Array and gives positive elements as output
val a = ArrayBuffer(-1, 1, 0, -2, -1, 2, 5, 6, 7)
val positionsToKeep = for (i <- a.indices if a(i) >= 0) yield i
for (j <- positionsToKeep.indices) a(j) = a(positionsToKeep(j))
a.trimEnd(a.length - positionsToKeep.length)
It gives the output as (1,0,2,5,6,7) removing all negative elements.
I am unable to understand line 3 & 4.
for (j <- positionsToKeep.indices) a(j) = a(positionsToKeep(j))
a.trimEnd(a.length - positionsToKeep.length)
I'm scratching my head since 2 days on these 2 lines but can't give up and I finally posting it here seeking some help.

As a is a bufferArray so we can change the values of the array a.
Line 3:
Line 3 is populating or you can say updating the value of positionToKeep into a.
a(j) = positionToKeep(j)
// which is running like this
// a(0) = positionToKeep(0)
// a(1) = positionToKeep(1) .... and so on
Now what will happen after populating all the values of positionToKeep into a there might be the case some older values remains untouched. Line four is deleting or dropping these elements. In the case when we have all the positive values in array a line four has like no use but when the length of a is greater then positionToKeep then we need line 4.
Line 4: consider the scenario
val a = Array(1, 2, 3, 4, 5, 6)
Then our positionToKeep will have all the element and the length of both the array will be equal.
val positionToKeep = Array(1, 2, 3, 4, 5, 6)
In this case line four trimEnd(0) because length of a and positionToKeep are equal.
val a = Array( 1, 2, 3, 4, -5, -6, 8, 9, -3)
In this case we will have Array(1,2,3,4,8,9) in positionToKeep
In line 3 we will update array a and after updating before line four this is how our array a will look like.
Array(1,2,3,4,8,9,8,9,-3) as we need values only up to length 6 as we have only 6 positive values. We need to drop last 3 element that what is tripEnd doing for us.

Related

How would you split a numpy array where the elements give the partition size?

For an numpy 1d array such as:
In [1]: A = np.array([2,5,1,3,9,0,7,4,1,2,0,11])
In [2]: A
Out[2]: array([2,5,1,3,9,0,7,4,1,2,0,11])
I need to split the array by using the values as a sub-array length.
For the example array:
The first index has a value of 2, so I need the first split to occur at index 0 + 2, so it would result in ([2,5,1]).
Skip to index 3 (since indices 0-2 were gobbled up in step 1).
The value at index 3 = 3, so the second split would occur at index 3 + 3, and result in ([3,9,0,7]).
Skip to index 7
The value at index 7 = 4, so the third and final split would occur at index 7 + 4, and result in ([4,1,2,0,11])
I'm using this simple array as an example, because I think it will help in my actual use case, which is reading data from binary files (either as bytes or unsigned shorts). I'm guessing that numpy will be the fastest way to do it, but I could also use struct/bytearray/lists or whatever would be best.
I hope this makes sense. I had a hard time trying to figure out how best to word the question.
Here is an approach using standard python lists and a while loop:
def custom_partition(arr):
partitions = []
i = 0
while i < len(arr):
pariton_size = arr[i]
next_i = i + pariton_size + 1
partitions.append(arr[i:next_i])
i = next_i
return partitions
a = [2, 5, 1, 3, 9, 0, 7, 4, 1, 2, 0, 11]
b = custom_partition(a)
print(b)
Output:
[[2, 5, 1], [3, 9, 0, 7], [4, 1, 2, 0, 11]]

Rearrange an array A so that A wins maximum number of comparisons with array B when comparison is done one-on-one

Let's say I have an array A = [3, 6, 7, 5, 3, 5, 6, 2, 9, 1] and B = [2, 7, 0, 9, 3, 6, 0, 6, 2, 6]
Rearrange elements of array A so that when we do comparison element-wise like 3 with 2 and 6 with 7 and so on, we have maximum wins (combinations where A[i] > B[i] are maximum (0<=i<len(A))).
I tried below approach:
def optimal_reorder(A,B,N):
tagged_A = [('d',i) for i in A]
tagged_B = [('a',i) for i in B]
merged = tagged_A + tagged_B
merged = sorted(merged,key=lambda x: x[1])
max_wins = 0
for i in range(len(merged)-1):
print (i)
if set((merged[i][0],merged[i+1][0])) == {'a','d'}:
if (merged[i][0] == 'a') and (merged[i+1][0] == 'd'):
if (merged[i][1] < merged[i+1][1]):
print (merged[i][1],merged[i+1][1])
max_wins += 1
return max_wins
as referenced from
here
but this approach doesn't seem to give correct answer for given A and B i,e if A = [3, 6, 7, 5, 3, 5, 6, 2, 9, 1] and B = [2, 7, 0, 9, 3, 6, 0, 6, 2, 6] then maximum wins is 7 but my algorithm is giving 5.
is there something I am missing here.
revised solution as suggested by #chqrlie
def optimal_reorder2(A,B):
arrA = A.copy()
C = [None] * len(B)
for i in range(len(B)):
k = i + 1
all_ele = []
while (k < len(arrA)):
if arrA[k] > B[i]:
all_ele.append(arrA[k])
k += 1
if all_ele:
e = min(all_ele)
else:
e = min(arrA)
C[i] = e
arrA.remove(e)
return C
How about this algorithm:
start with an empty array C.
for each index i in range(len(B)).
if at least one of the remaining elements of A is larger than B[i], choose e as the smallest of these elements, otherwise choose e as the smallest element of A.
set C[i] = e and remove e from A.
C should be a reordering of A that maximises the number of true comparisons C[i] > B[i].
There’s probably a much better algorithm than this, but you can think of this as a maximum bipartite matching problem. Think of the arrays as the two groups of nodes in the bipartite graph, then add an edge from A[i] to B[j] if A[i] > B[j]. Then any matching tells you how to pair elements of A with elements of B such that the A element “wins” against the B element, and a maximum matching tells you how to do this to maximize the number of wins.
I’m sure there’s a better way to do this, and I’m excited to see what other folks come up with. But this at least shows you can solve this in polynomial time.

Vectorizing Matlab replace array values from start to end

I have an array in which I want to replace values at a known set of indices with the value immediately preceding it. As an example, my array might be
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
and the indices of values to be replaced by previous values might be
y = [2, 3, 8];
I want this replacement to occur from left to right, or else start to finish. That is, the value at index 2 should be replaced by the value at index 1, before the value at index 3 is replaced by the value at index 2. The result using the arrays above should be
[1, 1, 1, 4, 5, 6, 7, 7, 9, 0]
However, if I use the obvious method to achieve this in Matlab, my result is
>> x(y) = x(y-1)
x =
1 1 2 4 5 6 7 7 9 0
Hopefully you can see that this operation was performed right to left and the value at index 3 was replaced by the value at index 2, then 2 was replaced by 1.
My question is this: Is there some way of achieving my desired result in a simple way, without brute force looping over the arrays or doing something time consuming like reversing the arrays around?
Well, practically this is a loop but the order is number of consecutive index elements
while ~isequal(x(y),x(y-1))
x(y)=x(y-1)
end
Using nancumsum you can achieve a fully vectorized version. Nevertheless, for most cases the solution karakfa provided is probably one to prefer. Only for extreme cases with long sequences in y this code is faster.
c1=[0,diff(y)==1];
c1(c1==0)=nan;
shift=nancumsum(c1,2,4);
y(~isnan(shift))=y(~isnan(shift))-shift(~isnan(shift));
x(y)=x(y-1)

How Can I Creat Method do This Job?

I want create method that return an array which contains exactly the same numbers as the given array, but rearranged so that every 3 is immediately followed by a 4.
Do not move the 3's, but every other number may move. The array contains the same number of 3's and 4's, every 3 has a number after it that is not a 3 or 4, and a 3 appears in the array before any 4.
Example:
problem({1, 3, 1, 4, 4, 3, 1}) → {1, 3, 4, 1, 1, 3, 4}
problem({3, 2, 2, 4}) → {3, 4, 2, 2}
thanks .
Set i = 0, j = 0. Then you repeat the following:
Find the first 3 at an index ≥ i which is not followed by a 4. If none are found, you succeeded. If the 3 is the last number in the array or followed by a 3, you failed. Now find the first 4 at an index ≥ j which is not preceded by a 3. If none are found, you fail. Otherwise set i = location of the 3, j = location of the 4, exchange the objects at positions i+1 and j, set i = i + 2 and j = j + 1, and repeat.
I don't like writing code that depends on promises about the data that I don't verify myself, so this will work whatever is in the array.

Python2.7 looping through custom sequence

Rookie questions season continues :)
I've got a function that has to be fed with numerical value from certain range. This part of the code will be replicated for each datasource I'm linking in, but which changed numerical parameters.
Example (that works):
for i in [0, 1, 2, 3, 7, 8, 15, 31, 32]:
RowTDE(i)
Question
I would like to avoid typing in all the necessary values, therefore I would like to use something like this:
for i in [:2]+[7:10]+[15:]:
RowTDE(i)
I've tried it and got:
SyntaxError: invalid syntax
Do I need to create a list of integers first to use it? Like
intList = [1, 2, 3, 4, ... 33].
Also, as mentioned previously for each data source this range will differ, but maximum numerical value will be less then 40 (each number represents a column index).
As always I would much appreciate your help with this and just let me know if you need more info.
Happy Monday morning :)
You can add ranges:
>>> for i in range(3) + range(7, 9) + range(15, 16) + range(31, 33):
print i
0
1
2
7
8
15
31
32
or build the range then slice it:
>>> r = range(33)
>>> for i in r[:3] + r[7:9] + r[15:16] + r[31:]:
print i
0
1
2
7
8
15
31
32
But you can't slice nothing, hence [:2] on its own is a SyntaxError.
Slice notation on its own doesn't make sense. It's implemented by objects that support it using the __getitem__ method.
You could (ab)use __getitem__ to create an object that uses that syntax:
import itertools
class SliceAbuse(object):
def __getitem__(self, key):
last = None
for obj in key:
if isinstance(obj, slice):
for n in xrange(obj.start, obj.stop + 1, obj.step or 1):
last = n
yield n
elif obj is Ellipsis:
for n in itertools.count(last + 1):
yield n
else:
last = obj
yield obj
For example:
for n in SliceAbuse()[1:5, 7:9, 11, ...]: # To infinity and beyond
print n
if n == 20:
break
Although since your ranges are rather small, you can use the fact that range() in Python 2 returns a list object, which you can concatenate with other lists:
range(1, 4) + range(10, 15) == [1, 2, 3, 10, 11, 12, 13, 14]
Note that this won't work in Python 3, as range doesn't return a list.

Resources