i want to convert in ruby
[[1, 1], [2, 3], [3, 5], [4, 1], [1, 2], [2, 3], [3, 5], [4, 1]]
into
[{1=>1}, {2=>3}, {3=>5}, {4=>1}, {1=>2}, {2=>3}, {3=>5}, {4=>1}]
and after this to obtain sum of all different keys:
{1=>3,2=>6,3=>10,4=>2}
For the second question
sum = Hash.new(0)
original_array.each{|x, y| sum[x] += y}
sum # => {1 => 3, 2 => 6, 3 => 10, 4 => 2}
Functional approach:
xs = [[1, 1], [2, 3], [3, 5], [4, 1], [1, 2], [2, 3], [3, 5], [4, 1]]
Hash[xs.group_by(&:first).map do |k, pairs|
[k, pairs.map { |x, y| y }.inject(:+)]
end]
#=> {1=>3, 2=>6, 3=>10, 4=>2}
Using Facets is much simpler thanks to the abstractions map_by (a variation of group_by) and mash (map + Hash):
require 'facets'
xs.map_by { |k, v| [k, v] }.mash { |k, vs| [k, vs.inject(:+)] }
#=> {1=>3, 2=>6, 3=>10, 4=>2}
You don't need the intermediate form.
arrays = [[1, 1], [2, 3], [3, 5], [4, 1], [1, 2], [2, 3], [3, 5], [4, 1]]
aggregate = arrays.each_with_object Hash.new do |(key, value), hash|
hash[key] = hash.fetch(key, 0) + value
end
aggregate # => {1=>3, 2=>6, 3=>10, 4=>2}
arr= [[1, 1], [2, 3], [3, 5], [4, 1], [1, 2], [2, 3], [3, 5], [4, 1]]
final = Hash.new(0)
second_step = arr.inject([]) do |arr,inner|
arr << Hash[*inner]
final[inner.first] += inner.last
arr
end
second_step
#=> [{1=>1}, {2=>3}, {3=>5}, {4=>1}, {1=>2}, {2=>3}, {3=>5}, {4=>1}]
final
#=> {1=>3, 2=>6, 3=>10, 4=>2}
if you directly only need the last step
arr.inject(Hash.new(0)){|hash,inner| hash[inner.first] += inner.last;hash}
=> {1=>3, 2=>6, 3=>10, 4=>2}
Related
I have an n-depth array where n is a variable greater than or equal to 2:
[[1,1],[[1,1],[1,1]]]
I want to flatten this array to have exactly 2 depth, like this:
[[1,1],[1,1],[1,1]]
Can anyone think of a good way to achieve that?
This should do it.
def flatten_after_first(arr)
arr.flat_map { |a| a.first.is_a?(Array) ? a.map(&:flatten) : [a] }
end
flatten_after_first [[1,1],[[1,1],[1,1]]]
#=> [[1, 1], [1, 1], [1, 1]]
flatten_after_first [[1,1], [[2,2], [2,2]], [[[3,3], [3,3]], [[3,3], [3,3]]]]
#=> [[1, 1], [2, 2], [2, 2], [3, 3, 3, 3], [3, 3, 3, 3]]
May be this will help
def flat(array)
array.each do |item|
if item.is_a?(Array) && item.flatten.count != item.count
flat(item)
else
$arr << item
end
end
end
###
$arr = []
collection = [[1, 1], [[1, 1], [1, 1], [[1, 2], [1, 2, 3]]]]
flat(collection)
puts $arr.inspect
=> [[1, 1], [1, 1], [1, 1], [1, 2], [1, 2, 3]]
$arr = []
collection = [[1,1],[[[1,1],[1,1]],[1,1]]]
flat(collection)
$arr
=> [[1, 1], [1, 1], [1, 1], [1, 1]]
Try this:
def depth_first_flatten array
result = []
array.each do |element|
if element.first.is_a? Array
result += deph(element)
else
result << element
end
end
result
end
# array = [[1,2],[[3,4],[5,6]]]
# depth_first_flatten(array)
#
# OUTPUT: [[1, 2], [3, 4], [5, 6]]
I have four 2d arrays of varying lengths. Is there a way to compare them to make sure none of the arrays share a value? Is there a simple way to code it or a gem I could use?
To back my question up with an example:
array1 = [[2,3],[2,4]]
array2 = [[1,3],[2,3],[3,3]]
array3 = [[5,3],[6,3],[7,3],[8,3],[9,3]]
I want a checker that would let me know that array1 and array2 have an element the same.
Is this doable?
You might do it as follows.
Code
def shared_values?(*arr)
a = arr.map(&:uniq).reduce(:+)
a.size > a.uniq.size
end
Examples
array1 = [[2, 3], [2, 4]]
array2 = [[1, 3], [2, 3], [3, 3]]
array3 = [[5, 3], [6, 3], [7, 3],[8, 3],[9, 3]]
shared_values? array1, array2, array3 #=> true
array1 = [[2, 4]]
shared_values? array1, array2, array3 #=> false
array1 = [[2, 4], [2, 4]]
shared_values? array1, array2, array3 #=> false
array1 = [[2, 3], [2, 4], [2, 4]]
shared_values? array1, array2, array3 #=> false
Explanation
For the last example above:
arr = [array1, array2, array3]
#=> [ [[2, 3], [2, 4], [2, 4]],
# [[1, 3], [2, 3], [3, 3]],
# [[5, 3], [6, 3], [7, 3], [8, 3], [9, 3]] ]
a = arr.map(&:uniq)
#=> [ [[2, 3], [2 ,4]],
# [[1, 3], [2, 3], [3, 3]],
# [[5, 3], [6, 3], [7, 3], [8, 3], [9, 3]] ]
b = a.reduce(:+)
#=> [[2, 3], [2, 4], [1, 3], [2, 3], [3, 3],
# [5, 3], [6, 3], [7, 3], [8, 3], [9, 3]]
c = b.uniq
#=> [[2, 3], [2, 4], [1, 3], [3, 3],
# [5, 3], [6, 3], [7, 3], [8, 3], [9, 3]]
b.size > c.size
# 10 > 9 #=> true
arr.map(&:uniq) is the same as:
arr.map { |a| a.uniq }
a.reduce(:+) uses the form of Enumerable#reduce that takes an argument that is a symbol, naming a method to be applied to each element of arr, the result being the sum of the three arrays that comprise the elements of b.
If the arrays are in a consistent format, like all numbers and not a mix of floating point and strings, you can do this:
array1 & array2
# => [[2, 3]]
That means to test if they overlap:
(array1 & array2).any?
If you can be assured each of the elements in all of the arrays are unique, then you can test quickly if there are any duplicates:
sum = array1 + array2 + array3 + array4
sum.length == sum.uniq.length
If each array may contain duplicates then you'd need to pair them off and compare A vs. B for all possible pairs.
I have arrays and nested arrays, such that:
a = [1,2]
b = [[3,4],[5,6]]
c = [7,8]
What's the best way to create
d = [[1,2],[3,4],[5,6],[7,8]]
in Ruby?
UPDATE:
The goal is to create a method below:
def foo([a,b,c])
--some logic that iterates through each array--
end
def foo(xss)
xss.flat_map { |xs| xs.first.is_a?(Array) ? xs : [xs] }
en
foo([a, b, c]) #=> [[1, 2], [3, 4], [5, 6], [7, 8]]
This will work assuming inputs are always arrays. Might have some side effects if any input is formatted differently.
def foo(*args)
args.map {|a| a.first.is_a?(Array) ? a : [a]}.inject([], :+)
end
foo([1,2], [[3,4],[5,6]], [7,8]) #=> [[1, 2], [3, 4], [5, 6], [7, 8]]
def foo(a,b,c)
final = []
a.first.is_a?(Array) ? a.each { |x| final << x } : final << a
b.first.is_a?(Array) ? b.each { |x| final << x } : final << b
c.first.is_a?(Array) ? c.each { |x| final << x } : final << c
final
end
I'd do :
a = [1,2]
b = [[3,4],[5,6]]
c = [7,8]
final_ary = [a,b,c].inject([]) do |out_ary,ary|
if ary.first.is_a?(Array)
out_ary.concat(ary)
else
out_ary.push(ary)
end
end
final_ary
# => [[1, 2], [3, 4], [5, 6], [7, 8]]
def foo (*args)
args.flatten.each_slice(2).to_a
end
foo(a,b,c) # => [[1, 2], [3, 4], [5, 6], [7, 8]]
A general solution:
def extract(arr, result=[])
return result if arr.empty?
arr.each {|e| (e.first.is_a? Array) ? result=extract(e,result) : result << e}
end
p extract( [ [1,2],[3,4,5],[6,7] ] )
#=> [[1, 2], [3, 4, 5], [6, 7]]
p extract( [ [1,2],[ [3,4],[5,6] ],[7,8]] )
#=> [[1, 2], [3, 4], [5, 6], [7, 8]]
p extract( [ [1,2],[ [3,4],[ [5,6],[7,8] ] ],[9,10,11] ] )
#=> [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10, 11]]
p extract( [ [1,2],[ [3,4],[ [5,6],[ [7,8],[9,10] ] ] ],[11,12] ])
#=> [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]]
I want to find unique elements from an array of arrays by the first element in the inner arrays.
for example
a = [[1,2],[2,3],[1,5]
I want something like
[[1,2],[2,3]]
The uniq method takes a block:
uniq_a = a.uniq(&:first)
Or if you want to do it in-place:
a.uniq!(&:first)
For example:
>> a = [[1,2],[2,3],[1,5]]
=> [[1, 2], [2, 3], [1, 5]]
>> a.uniq(&:first)
=> [[1, 2], [2, 3]]
>> a
=> [[1, 2], [2, 3], [1, 5]]
Or
>> a = [[1,2],[2,3],[1,5]]
=> [[1, 2], [2, 3], [1, 5]]
>> a.uniq!(&:first)
=> [[1, 2], [2, 3]]
>> a
=> [[1, 2], [2, 3]]
If you're stuck back in 1.8.7 land where uniq doesn't take a block, then you can do it this way:
a.group_by(&:first).values.map(&:first)
For example:
>> a = [[1,2],[2,3],[1,5]]
=> [[1, 2], [2, 3], [1, 5]]
>> a.group_by(&:first).values.map(&:first)
=> [[1, 2], [2, 3]]
Thanks for the extra prodding Jin.
Here's a ruby 1.8.7 solution
irb> [[1,2],[2,3],[1,5]].inject([]) { |memo,x| memo << x unless memo.detect { |item| item.first == x.first }; memo }
=> [[1, 2], [2, 3]]
You could also take the shorthand over a hash and be a bit lazy and take the last item instead
irb> [[1,2],[2,3],[1,5]].inject({}) { |memo,x| memo[x.first] = x; memo }.map { |x| x.last }
=> [[1, 5], [2, 3]]
I have a sparse array, for example:
rare = [[0,1], [2,3], [4,5], [7,8]]
I want to plot a chart with these data, each pair are point coordinates.
As you can see I don't have points for x=1, x=3 , x=5, x=6
I want to fill the array with the previous values, so for the above example I will get:
filled = [[0,1], [1,1], [2,3], [3,3], [4,5], [5,5], [6,5], [7,8]
As you can see, for calculating the y value, I simply take the last y value I used.
What is the best aproach to accomplish this ?
Range.new(*rare.transpose.first.sort.values_at(0,-1)).inject([]){|a,i|
a<<[i, Hash[rare][i] || a.last.last]
}
Step-by-step explanation:
rare.transpose.first.sort.values_at(0,-1) finds min and max x ([0,7] in your example)
Range.new() makes a range out of it (0..7)
inject iterates through the range and for every x returns pair [x,y], where y is:
y from input array, where defined
y from previously evaluated pair, where not
Note: here are some other ways of finding min and max x:
[:min,:max].map{|m| Hash[rare].keys.send m}
rare.map{|el| el.first}.minmax # Ruby 1.9, by steenslag
rare = [[0,1], [2,3], [4,5], [7,8]]
filled = rare.inject([]) do |filled, point|
extras = if filled.empty?
[]
else
(filled.last[0] + 1 ... point[0]).collect do |x|
[x, filled.last[1]]
end
end
filled + extras + [point]
end
p filled
# => [[0, 1], [1, 1], [2, 3], [3, 3], [4, 5], [5, 5], [6, 5], [7, 8]]
An inject solution:
filled = rare.inject([]) do |filled_acc, (pair_x, pair_y)|
padded_pairs = unless filled_acc.empty?
last_x, last_y = filled_acc.last
(last_x+1...pair_x).map { |x| [x, last_y] }
end || []
filled_acc + padded_pairs + [[pair_x, pair_y]]
end
More about Enumerable#inject and functional programming with Ruby here.
irb(main):001:0> rare = [[0,1], [2,3], [4,5], [7,8]]
=> [[0, 1], [2, 3], [4, 5], [7, 8]]
irb(main):002:0> r=rare.transpose
=> [[0, 2, 4, 7], [1, 3, 5, 8]]
irb(main):003:0> iv = (r[0][0]..r[0][-1]).to_a.select {|w| !r[0].include?(w) }
=> [1, 3, 5, 6]
irb(main):004:0> r[1][-1]=r[1][-2]
=> 5
irb(main):005:0> p (iv.zip(r[1]) + rare).sort
[[0, 1], [1, 1], [2, 3], [3, 3], [4, 5], [5, 5], [6, 5], [7, 8]]
=> [[0, 1], [1, 1], [2, 3], [3, 3], [4, 5], [5, 5], [6, 5], [7, 8]]