Iterate +1 over array two indices two at a time - arrays

In Ruby, given this array
[0,1,2,3,4,5]
how do i produce
> 0, 1
> 1, 2
> 2, 3
> 3, 4
> 4, 5

[0,1,2,3,4,5].each_cons(2){|a| puts a.join(", ")}

each_cons for sure, but another way:
enum = [0,1,2,3,4,5].to_enum
loop do
puts "#{enum.next}, #{enum.peek}"
end
0, 1
1, 2
2, 3
3, 4
4, 5
See Kernel#to_enum and Kernel#loop. Note the docs for all Kernel instance methods are shown at Object, whereas Kernel module methods are documented at Kernel.

Related

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

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.

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.

Adding ints from array

Im taking CompArch for the first time ever. I have to print out an array
list: .word 3, 0, 1, 2, 6, -2, 4, 7, 3, 7
I managed to print it out in two rows
R1 3 0 1 2 6
R2 -2 4 7 3 7
How can i create a loop or code to add all the numbers in the first row. Or in other words the first 5 ints from the array. Thanks for your help in advance.

interpolation of array inside a array

When I was trying to print a specific element in array, I have mistakenly typed the name of the same array inside [ ] as element and got some output as shown. I thought that it is taking the size of the array and printing that number of characters, which was proven to be wrong by the output.
#array = (0..10, 12);
print "#array[#array]";
prints
Use of uninitialized value in join or string at
/home/VAR121/Program/Practise_Perl/Arrays.pl line 9.
0 1 2 3 4 5 6 7 8 9 10
I went one step ahead and edited code as shown below
print "#array[#array[#array]]";
output as: `0 1 2 3 4 5 6 7 8 9 10 0` Use of uninitialized value in join or string at
/home/VAR121/Program/Practise_Perl/Arrays.pl line 9.
Now I tried to put a number inside the second array instead of again giving array name as below.
print "#array[#array[1,2,3]]";
output as
1 2 3
But no warning message this time.
What it is trying to print? and What is the reason behind this behavior.
You're creating an array slice. Start out by understanding that your array contains elements 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12.
That's a total of 12 elements (there is no value 11, but at index 11 you are storing the value 12).
Now when you use this construct: #array[#array] you're taking a slice, and requesting the values stored in indices 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, and 12. There is no element with an index of 12 in your array. The eleventh element has the value "12".
#array[] is array slice notation. For example:
my #array = (qw/apple banana cucumber date/)
#array[1,2] will return a list of (banana, cucumber) (index 1 and index 2 of #array).
Now, #array[#array] tries to take the values of the inner #array and use them as indexes of the outer #array. In your example, #array[#array] is equivalent to #array[0..10, 12]. Since one of the values of the inner array is 12, and the outer array has no index of 12, you get an undefined value warning.

Resources