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
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 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.
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]
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]
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to split (chunk) a Ruby array into parts of X elements?
I would like to split an array into an array of sub-arrays.
For example,
big_array = (0...6).to_a
How can we cut this big array into an array of arrays (of a max length of 2 items) such as:
arrays = big_array.split_please(2)
Where...
arrays # => [ [0, 1],
[2, 3],
[4, 5] ]
Note:
I ask this question, 'cause in order to do it, I'm currently coding like this:
arrays = [
big_array[0..1],
big_array[2..3],
big_array[4..5]
]
...which is so ugly. And very unmaintainable code, when big_array.length > 100.
You can use the #each_slice method on the array
big_array = (0..20).to_a
array = big_array.each_slice(2).to_a
puts array # [[0, 1], [2, 3], [4, 5], [6, 7], [8, 9], [10, 11], [12, 13], [14, 15], [16, 17], [18, 19], [20]]
check out the slice:
big_array.each_slice( 2 ).to_a