"Interpolate" an array (add new elements between existing) [closed] - ruby

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]

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

Rearrange the negative elements of the array so that they are in non-descending order [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 1 year ago.
Improve this question
Rearrange the negative elements of the array so that they are in non-descending order. Leave the remaining elements in their places.
It will show like that: [-1, 1, -3, 6, -5, -9] -> [-9, 1, -5, 6, -3, -1]
How can I do this without using additional arrays?
There are many ways to do that. Here is one.
arr = [-1, 1, -3, 6, -5, -9]
negs = arr.select { |n| n < 0 }.sort
#=> [-9, -5, -3, -1]
arr.map { |n| n >= 0 ? n : negs.shift }
#=> [-9, 1, -5, 6, -3, -1]

How to do scheduling using dynamic programming? [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 5 years ago.
Improve this question
The Problem
Given N input vector = [x1, x2, ..., xl] and each of them have same length L. Define F = summation of N input vector.
And the aim is to find a set of integer T = [n1, n2, ..., nN] represents delay of each input vector so that the maximum value of F is minimized.
Example
Assume 2 Input vector [0, 3, -1, 1, 0], [0, 2, -1, 0, 0]:
if T = [0, 0], then F = [0, 5, -2, 1, 0] and max(F) = 5 which is obviously not a good result.
if T = [1, 0], so that F = [0, 2, 2, -1, 1, 0], max(F) = 2. This T is we aim to find. The computing process below may help understand this problem:
T = [1, 0]
Vector1 with delay 1 [0, 3, -1, 1, 0]
Vector2 with delay 0 [0, 2, -1, 0, 0]
F = [0, 2, 2, -1, 1, 0]
The Question
Any idea how to delay vector T using dynamic programming?
Any advise would be greatly appreciated.

Ruby: Sum of "first element" in each array [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
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]

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