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 6 months ago.
Improve this question
I have the following array:
[1,1,3,3,3,3,1023,1023,1,3,3,3, ...]
Is there a way to sort the array in a way where the items can appear more mixed up?
Like:
[1, 3, 1023, 3, 1, 3, 1023, ...]
You could shuffle
a=[1,1,3,3,3,3,1023,1023,1,3,3,3]
p a.shuffle
Output
[1023, 1, 1, 1, 3, 3, 1023, 3, 3, 3, 3, 3]
Related
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
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 4 years ago.
Improve this question
How can I remove elements from a given array of integers that are evenly divisible by 3? A new array should be returned and the original array should not be altered.
For example, if
array = [1,2,3,4,1,5,2,6,7,8,9,10,7,11,12,13,14,15]
the array
[1, 2, 4, 1, 5, 2, 7, 8, 10, 7, 11, 13, 14]
should be returned.
Just found it:
array.reject{|a| a % 3 == 0}
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 6 years ago.
Improve this question
I had 10 items in total. I lost all of them in 3 days: 5 items on the 1st day, 3 items on the 2nd day, and 2 items on the last day. I need to get an array [5, 2, 0] of remaining items at the end of each day. How can I get the array, given total 10 and the array of lost items [5, 3, 2]?
[5, 3, 2].each_with_object([10]){|e, a| a.push(a.last - e)}.drop(1)
# => [5, 2, 0]
Know why you need the complication drop(1)? It is because, without it, the answer makes more logical sense. Your requirement is what was complicated.
[5, 3, 2].each_with_object([10]){|e, a| a.push(a.last - e)}
# => [10, 5, 2, 0]
The initial 10 represents the initial state.
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 8 years ago.
Improve this question
Can't understand the difference between select and collect methods. Also want to know when to use each.
Enumerable#collect (or Enumerable#map) returns a result of applying block to each items.
[1, 2, 3, 4].collect { |x| x > 2 }
# => [false, false, true, true]
While Enumerable#select returns an array of filtered items:
[1, 2, 3, 4].select { |x| x > 2 }
# => [3, 4]
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]