Combining two arrays to create a two dimensional array in ruby - arrays

a = [1, 2, 3]
b = [4, 5, 6]
How would I combine the two arrays in a 2D array?:
[[1, 4], [2, 5], [3, 6]]

Try Array#zip
a.zip(b)
=> [[1,4],[2,5],[3,6]]

While zip is obviously the most straightforward answer, this also works:
[a, b].transpose
=> [[1, 4], [2, 5], [3, 6]]

Related

Find arrays from an array of arrays of numbers in which one or more elements are equal

Exists array with arrays those contains numbers.
How to find and concatenate arrays where one or more elements are equal with preserving the sequence.
For example:
Input: [[1, 2], [4, 3], [2, 7], [3, 5], [5, 9]]
Output: [[[1, 2], [2, 7]], [[4, 3], [3, 5], [5, 9]]]

numpy shape inconsistent with array structure

I am going mad over this thing.
I have 2 lists
A = [ [[1,2,3],[1,2,3],[1,2,3]], [[1,2,3],[1,2,3],[1,2,3]]]
B = [ [[1,2,3],[1,2,3],[1,2,3]], [[1,2,3],[1,2,3]]]
When I call the shape of A and B as numpy array I get this:
In [33]: np.asarray(A).shape
Out[33]: (2, 3, 3)
In [31]: np.asarray(B).shape
Out[31]: (2,)
How do I shape A in the same way as B, that is (2,)?
I think I understand why it's happening but I don't know how to prevent this to happening.
Anyone any help/idea please?
thanks!
Your 2 lists:
In [232]: A
Out[232]: [[[1, 2, 3], [1, 2, 3], [1, 2, 3]], [[1, 2, 3], [1, 2, 3], [1, 2, 3]]]
In [233]: B
Out[233]: [[[1, 2, 3], [1, 2, 3], [1, 2, 3]], [[1, 2, 3], [1, 2, 3]]]
Now, explain why the B result is better than the A one?
In [234]: np.array(A)
Out[234]:
array([[[1, 2, 3],
[1, 2, 3],
[1, 2, 3]],
[[1, 2, 3],
[1, 2, 3],
[1, 2, 3]]])
In [235]: np.array(B)
<ipython-input-235-c938532b77c1>:1: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
np.array(B)
Out[235]:
array([list([[1, 2, 3], [1, 2, 3], [1, 2, 3]]),
list([[1, 2, 3], [1, 2, 3]])], dtype=object)

Acessing values in nested array

I'm learning Rust and I wanted to write program that would take a (randomly generated) bowling sheet and generate the score. I now know that I will have to use Rusts Vecs instead of arrays but I got stuck at accessing the value from the nested arrays so I would like to find the solution before I re-write it.
What I wanted to do is access the individual values and run some logic on them, but I got stuck at the "accessing values" part, this is what I came up with (but it doesn't work):
let sheet: [[u32; 2]; 10] = [[1, 3], [0, 6], [9, 0], [0, 5], [5, 3], [4, 2], [1, 4], [2, 3], [3, 0], [4, 4]];
for frame in 0..sheet.len() {
for score in 0..sheet[frame].len() {
println!("{}", sheet[frame[score]]);
}
}
You should clarify what exactly do you mean by 'accessing individual values', but from your code i'd assume that you just want to iterate over every score. Here's how you do it with for loops:
let sheet = [[1, 3], [0, 6], [9, 0], [0, 5], [5, 3], [4, 2], [1, 4], [2, 3], [3, 0], [4, 4]];
for frame in sheet {
// On the first iteration frame will be == [1, 3], then [0, 6], etc
for score in frame {
// on first iteration score will be == 1, then 3, then 0, etc
println!("{}", score);
}
}

Combine arrays in Ruby

I have two arrays as in the listing given below.
a = [1, 2, 3, 4, 5]
b = [1.360, 0.085, -1.190, -0.340, 3.698]
I need to merge the values at each index so that I get a structure resembling Resultant Array.
Resultant Array = [[1, 1.360], [2, 0.085], [3, -1.190], [4, -0.340], [5, 3.698]]
How do I go about doing it?
You can use Array#zip
a.zip(b)
# => [[1, 1.36], [2, 0.085], [3, -1.19], [4, -0.34], [5, 3.698]]
a = [1, 2, 3, 4, 5]
b = [1.360, 0.085, -1.190, -0.340, 3.698]
You can also try an alternative:
[a,b].transpose
Note: Use this when length of your array is same
You can do:
a.zip(b) #=> [[1,1.360],[2,0.085],[3,-1.190],[4,-0.340],[5,3.698]]
I didn't try it.
Source: apidoc.com

Getting all combinations of pairs from a list in Ruby

I have a list of elements (e.g. numbers) and I want to retrieve a list of all possible pairs. How can I do that using Ruby?
Example:
l1 = [1, 2, 3, 4, 5]
Result:
l2 #=> [[1,2], [1,3], [1,4], [1,5], [2,3], [2,4], [2,5], [3,4], [3,5], [4,5]]
In Ruby 1.8.6, you can use Facets:
require 'facets/array/combination'
i1 = [1,2,3,4,5]
i2 = []
i1.combination(2).to_a # => [[1, 2], [1, 3], [1, 4], [1, 5], [2, 3], [2, 4], [2, 5], [3, 4], [3, 5], [4, 5]]
In 1.8.7 and later, combination is built-in:
i1 = [1,2,3,4,5]
i2 = i1.combination(2).to_a
Or, if you really want a non-library answer:
i1 = [1,2,3,4,5]
i2 = (0...(i1.size-1)).inject([]) {|pairs,x| pairs += ((x+1)...i1.size).map {|y| [i1[x],i1[y]]}}

Resources