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"]]}
Related
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 3 years ago.
Improve this question
What is the optimal algorithm to find all the paths between 2 nodes of an adjacency list
Example input:
source = 1
destination = 5
list = {1: [2], 2: [4], 3: [4, 5], 4: [5]}
def paths(adj, st, en)
return [] unless adj.key?(st)
adj[st].each_with_object([]) do |nxt,arr|
nxt == en ? arr << [st, en] :
paths(adj, nxt, en).each { |a| arr << [st, *a] }
end
end
adj = { 1=>[2,3], 2=>[4,7], 3=>[4,8], 4=>[5,6], 5=>[7], 6=>[7] }
Note that I added an isolated node 8.
paths(adj, 1, 7)
#=> [[1, 2, 4, 5, 7],
# [1, 2, 4, 6, 7],
# [1, 2, 7],
# [1, 3, 4, 5, 7],
# [1, 3, 4, 6, 7]]
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 5 years ago.
Improve this question
I want to search through a hash for keys with only one value, and divide that value by 2. How do I go about this?
Example
hash = { a => [4], b => [2, 4, 5], c => [3, 5] }
Result sought
hash = { a => [2], b => [2, 4, 5], c => [3, 5] }
hash = {:a=>[4], :b=>[2, 4, 5], :c=>[3, 5]}
You can use transform_values:
hash.transform_values { |v| v.one? ? [v[0]/2.0] : v }
#=> {:a=>[2.0], :b=>[2, 4, 5], :c=>[3, 5]}
Or map
hash.map { |k,v| v.one? ? [k,[v[0]/2.0]] : [k,v] }.to_h
#=> {:a=>[2.0], :b=>[2, 4, 5], :c=>[3, 5]}
This should do the trick, just iterate over each key/value pair and check the length of the values, if it only has 1 item, set the key in the hash to be equal to half the original value.
hash = { a: [4], b: [2, 4, 5], c: [3, 5] }
# => {:a=>[4], :b=>[2, 4, 5], :c=>[3, 5]}
hash.each do |key, values|
if 1 == values.length
hash[key] = [values.first / 2]
end
end
# => {:a=>[2], :b=>[2, 4, 5], :c=>[3, 5]}
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
what is the cleanest way to turn these two arrays of arrays:
[[1, 'a'], [2, 'b']]
and
[[1, 'c'], [2, 'd']]
into a single array of hashes with arbitrary keys like this:
[{:id => 1, :foo => 'a', :bar => 'c'}, {:id => 2, :foo => 'b', :bar => 'd'}]
to elaborate, position 0 of every internal array should map to the :id key, position 1 of the internal arrays of the first array should map to the :foo key, and position 1 of the internal arrays of the second array should map to the :bar key. further, the :id key of each hash should not repeat, and each hash should have an :id, :foo, and :bar key.
Clarify by decomposing the block variables:
a = [[1, 'a'], [2, 'b']]
b = [[1, 'c'], [2, 'd']]
a.zip(b).map { |(id,foo),(_,bar)| {id: id, foo: foo, bar: bar } }
#=> [{:id=>1, :foo=>"a", :bar=>"c"}, {:id=>2, :foo=>"b", :bar=>"d"}]
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.
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 9 years ago.
Improve this question
I currently have this code
def objects(ids)
array = []
ids.each do |id|
array << object(id) # => #<object[id]>
end
array
end
objects([1, 2, 3])
# => [#<object1>, #<object2>, #<object3>]
It seems like there should be a cleaner way to do this. Can anyone help?
EDIT
This is what works
[1, 2, 3].map do |id|
object(id)
end
ORIGINAL
go this way:
[1, 2, 3].map(&:object_id)
# => [3, 5, 7]
def objects(ids)
ids.map(&:object_id)
end
objects([1, 2, 3])
# => [3, 5, 7]