Ruby: Sum of "first element" in each array [closed] - ruby

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
If I have a hash that looks like this:
C = [[1, 1, 1, 1],
[1, 2, 1, 1],
[1, 3, 1, 7],
[1, 1, 4, 1]]
What is a fast way to sum the columns and produce the following result:
C = [4, 7, 7, 10]
Edit: The way I was doing it coming from a C background was to parse thru the result and summing manually, that's why I asked. didn't know where else to look for.

arr = [[1, 1, 1, 1],
[1, 2, 1, 1],
[1, 3, 1, 7],
[1, 1, 4, 1]]
arr.transpose.map{|e| e.inject(:+)}
# => [4, 7, 7, 10]

Related

How can I solve this codewars kata. (really simple) 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 1 year ago.
Improve this question
The instructions:
You have to write a method, that return the length of the missing array.
Example:
[[1, 2], [4, 5, 1, 1], [1], [5, 6, 7, 8, 9]] --> 3
If the array of arrays is null/nil or empty, the method should return 0.
When an array in the array is null or empty, the method should return 0 too!
There will always be a missing element and its length will be always between the given arrays.
If you do array_of_arrays.sort_by! {|x| x.size} you'll get this:
[[1], [1, 2], [4, 5, 1, 1], [5, 6, 7, 8, 9]] it's missing an array with three elements inside the main array.
Also the def name: def getLengthOfMissingArray(array_of_arrays)
a = [[1, 2], [4, 5, 1, 1], [1], [5, 6, 7, 8, 9]]
new_arr = a.map(&:length)
missing_num_arr = (new_arr.min..new_arr.max).to_a - new_arr
# => [3]
You can map given array by length
Take minimum & maximum from mapped array
Create new array from minimum & maximum and subtract mapped array
You will get result

Finding paths in an adjacency list [closed]

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]]

Adding one element of an array to many elements of second array 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 5 years ago.
Improve this question
I want to add array in this manner
arr1=[1,2,3,4]
arr2=[4,5,6,7]
adding should be like
arr1[0]+arr2[0]
arr1[0]+arr2[1]
arr1[0]+arr2[2]
and so on similarly with other second and other elements of arr1
Try a combination of map over both arrays:
p [1,2,3,4].map { |e| [4,5,6,7].map { |f| f + e } }
# => [[5, 6, 7, 8], [6, 7, 8, 9], [7, 8, 9, 10], [8, 9, 10, 11]]
arr1.product(arr2).map { |a,b| a + b }
#=> [5, 6, 7, 8, 6, 7, 8, 9, 7, 8, 9, 10, 8, 9, 10, 11]
Use sum maybe if you have Ruby > 2.4, otherwise you can use Enumerable#inject.
arr1.product(arr2).map { |a| a.sum }
See Array#product and Array#sum for further information.

"Interpolate" an array (add new elements between existing) [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 can't seem to find a ruby-way to transform this
[0, 1, 2, 3]
into
[0, 0.5, 1, 1.5, 2, 2.5, 3]
e.g, insert new elements based on existing ones applying some rule.
EDIT: I'm asking about a general case, not necessarily a 0.5 increment. Say, when elements are not successive:
[1, 3, 12] => [1, 2, 3, 7.5, 12]
So the accepted answer is just perfect here, thanks.
arr = [0, 1, 2, 3]
def rule(x, y)
(x + y) / 2.0
end
arr.each_cons(2).flat_map{|x, y| [x, rule(x, y), y]} # => [0, 0.5, 1, 1, 1.5, 2, 2, 2.5, 3]

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]

Resources