Fastest way to split ruby array following a structure [closed] - ruby

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I'm looking for the fastest way to split an array into sub-arrays with different size. The size of every array is driven by a configuration file.
Example:
c = [1,2,3,4,5,6,7,8]
My configuration file:
block
contents: 3
type: ...
scope ...
block
contents: 1
type: ...
scope ...
block
contents: 2
type: ...
scope ...
block
contents: 2
type: ...
scope ...
c.size is equal to the sum of the content number of every block.
I must split my array into 'n' arrays where n is the number of blocks I define in my config file and the size of every array is the number of contents defined in that block.
The result with the given array and config file is:
[1,2,3]
[4]
[5,6]
[7.8]
Any idea with good performance result?

A slight variant of Matt's answer:
If you read the values from the file into:
a = [3,1,2,2]
you can then do this:
a.each_with_object([]) {|e,b| b << c.shift(e)}
#=> [[1, 2, 3], [4], [5, 6], [7, 8]]

c = [1,2,3,4,5,6,7,8]
d = [3,1,2,2]
d.map { |n| c.shift n } # => [[1, 2, 3], [4], [5, 6], [7, 8]]
This destroys the original c.

Related

RAILS - HOW TO COMPARE VALUES BY ARRAY INDEX [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
Improve this question
I'm new to rails and I was wondering how I compare the first value of array "a" if it is greater than the first value of array "b"?
Example:
a = [1, 2, 3]
b = [3, 2, 1]
How do I check if a[0] is greater than b[0].
You can use the first method:
a = [1, 2, 3]
b = [3, 2, 1]
a.first > b.first #=> false

Accumulating data in sorted manner in Ruby [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to store triples of data associated with key. I have triples of this form:
"data1" "data2" "data3"
where data1 is an integer. I have a mechanism to triples to key. For example "key1" is mapped to
["data1", "data2", "data3"]
There can be multiple triples associated with a key. For example, [4, "data2", "data3"], [1, "data5", "data6"] and [3, "data8", "data9"] may be mapped to "key1". I want these triples to be sorted by the "data1" field and mapped. In this case,
"key1" => {[1, "data5", "data6"] [3, "data8", "data9"] [4, "data2", "data3"]}
How do I do this Ruby?
You can do as below :
hsh = {"key1" => [[4,"data2","data3"], [1, "data5","data6"],[3, "data8","data9"]] }
hsh.each{|k,v| hsh[k]=v.sort_by(&:first)}
p hsh
# >> {"key1"=>[[1, "data5", "data6"], [3, "data8", "data9"], [4, "data2", "data3"]]}
If you don't want to update the source hash,then use #dup.
hsh = {"key1" => [[4,"data2","data3"], [1, "data5","data6"],[3, "data8","data9"]] }
hsh1 = hsh.dup
hsh1.each{|k,v| hsh1[k]=v.sort_by(&:first)}
p hsh1
# >> {"key1"=>[[1, "data5", "data6"], [3, "data8", "data9"], [4, "data2", "data3"]]}

checking if 2 numbers of array add up to Input number in ruby [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am new to ruby on rails .
I was programming ruby and want to try checking if 2 numbers of array add up to Input number in ruby.
eg,
array A[]= {3, 1, 8, 11, 5, 7}
given integer say N = 6
answer will be 1,5.
I know how to program it in java,C++ but i am stuck in ruby coding,
Can anyone please help me.Thanks in advance
You can use Array#combination:
ary = [3, 1, 8, 11, 5, 7]
n = 6
ary.combination(2).detect { |a, b| a + b == n }
#=> [1, 5]
combination(2) creates an array of all combinations of length 2, i.e. [3,1], [3,8], [3,11] etc.
detect { |a, b| a + b == n } returns the first pair with sum n
You can use find_all instead of detect to return all pairs with sum n.
a = [3, 1, 8, 11, 4, 5, 7, 2]
> a.combination(2).select {|i| i.inject(:+) == 6 }
#=> [[1, 5], [4, 2]]
a = [3, 1, 8, 11, 5, 7]
p a.combination(2).find{|i| i.inject(:+) == 6}
# >> [1, 5]

Merging 2 Arrays based on one column [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have two arrays:
a = [[1234,1],[2134,0],[4321,0],[2221,3]]
b = [[2134,1],[4321,3]]
I want to merge them based on the first elements of a for the following result:
c = [[1234,1],[2134,1],[4321,3],[2221,3]]
I want to replace 0's in a by the value in b if the first element match. The first element is unique in a and b.
How do I do this?
Thanks in advance,
The Hash#merge function lets you specify a block to define what to do with the values.
a = [[1234,1],[2134,0],[4321,0],[2221,3]]
b = [[2134,1],[4321,3]]
c = Hash[a].merge(Hash[b]) { |key, old, new| old+new }.to_a
# => [[1234, 1], [2134, 1], [4321, 3], [2221, 3]]
See the Hash#merge documentation.
In this case I did the merge through building the sum of the values. You might want to choose the largest value, or some other strategy which fits you.
Disclaimer: This approach does not work, if a (or b) contains Arrays having the same first value. Example [[1, 1], [1, 4], [2, 8]]. It is not specified in your question if that can happen.
Given
a = [[1234,1],[2134,0],[4321,0],[2221,3]]
and
b = [[2134,1],[4321,3]]
You could transform these arrays into hashes, perform the merge, then transform the result into an array again.
Hash[a].merge(Hash[b]).to_a
#=> [[1234, 1], [2134, 1], [4321, 3], [2221, 3]]
Here's one possibility:
a = [[1234,1],[2134,0],[4321,0],[2221,3]]
b = [[2134,1],[4321,3]]
a.zip(b).flatten(1).uniq(&:first)
# => [[1234, 1], [2134, 1], [4321, 3], [2221, 3]]
[*a, *b].group_by(&:first).map{|k, v| [k, v.map(&:last).inject(:+)]}
Just reverse the arrays and call unique:
(b + a).uniq(&:first)
This works because Array#+ is a non-mutating version of Array#concat. It'll create a new ordered array. Whichever array is first, will take precedence in the output. When uniq is called, it will enumerate over the array in order, allowing you to just get the first instance of the first column.
You can do this for n arrays:
[b,d,c,a].reduce(&:+).uniq(&:first)

Get k-1 length subset arrays of k length array [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
ruby array element grouping
Example. Given array a:
a = [1, 2, 3]
Its length is 3 so I want to print all 2-length arrays. These are:
[1, 2]
[1, 3]
[2, 3]
I don't know if there is some method in Ruby to get subset arrays. If there is not such a method what is most efficient way to do achieve this.
That's just a simple combination of 2 elements:
>> xs = [1, 2, 3]
>> xs.combination(xs.size - 1).to_a
=> [[1, 2], [1, 3], [2, 3]]
[EDIT] As #Joshua pointed out in a comment, the docs state that the order is not guaranteed (!). So here is a functional implementation that generates the combinations in the order you asked for. For completeness, I'll make it lazy as the original combination method:
require 'enumerable/lazy'
class Array
def combinations_of(n)
if n == 0
[[]].lazy
else
0.upto(self.size - 1).lazy.flat_map do |idx|
self.drop(idx + 1).combinations_of(n - 1).map do |xs|
[self[idx]] + xs
end
end
end
end
end

Resources