It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
a = [[24, 18.0], [24, 16.0], [25, 15.0]]
I need b = [[24, 17.0], [25, 15.0]]
The are 2 rules I need calculate average value of second item in sub-array i.e. [24, 18.0], [24, 16.0] #=> [24, 17.0] - this I can do separately myself, but I can't understand how to combine reduce with average like "ruby way" solutions in a.collect{...}
You can use group_by to group common keys, and sum/size to get the average:
b = a.group_by(&:first).map do |k,v|
[k, v.map(&:last).inject(:+) / v.size]
end
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have an array [1,2,3] and want to create hash from it, so the result is {"kangaroo"=>1, "moose"=>2, "mouse"=>3}. What's the best way to do it?
What about:
Hash[%w(kangaroo moose mouse).zip [1,2,3]]
# => {"kangaroo"=>1, "moose"=>2, "mouse"=>3}
Explanation
Array#zip combines both arrays element-wise:
%w(kangaroo moose mouse).zip [1,2,3]
# => [["kangaroo", 1], ["moose", 2], ["mouse", 3]]
Hash::[] creates a Hash from this array.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
#ad = [2, 5, 5]
if #ad.size < 2
#ad = []
end
#ad # => [2, 5, 5]
Why is #ad not []? I want to store the last two records found in an array and if they are the same, it should find a new record.
I guess you got the > wrong.
Don’t worry, that happens to the best.
There is an even more terse way of writing your code, using a trailing if.
#ad = [2, 5, 5]
#ad = [] if #ad.size > 2
#ad # => []
Why is #ad not []?
Why would it be? Your code is extremely basic, and it says "if #ad has less than two items, set it to an empty array". As you clearly show, #ad doesn't have less than two items, it has three items. Does three seem less than two to you?
If you're tying to find the number of unique items, you need ot use .uniq, but it still won't be less than two, it will be exactly two, and so you still won't enter the if condition.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
What's the easiest way to get two nibbles (as Integers) from a byte in Ruby?
If the "byte" is a number between 0 and 255:
n1, n2 = (byte & 0xf0) >> 4, byte & 0x0f
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Suppose I have an array:
a=['hello','shivam','how','are','you']
... and I want to make it into a multidimensional array like this below:
[['hello','shivam'],'how',['are','you']]
How do I do this?
def transform ar
[ [ar[0], ar[1]], ar[2], [ar[3], ar[4]] ]
end
this does exactly what you want to do, i cant do more if you dont share the plattern you want it to order...
You're probably gonna need to look into using the zip method:
a = [1,2,3]
b = [4,5,6]
a.zip(b)
=> [[1, 4], [2, 5], [3, 6]]
Although this won't help you all the way, as you clearly have some singular array entries in there too.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
In an array, there is exactly one element which repeats odd number of times and rest all other elements repeat even number of the times. Find the element optimally
xor all the elements together.
Example:
In [3]: reduce( lambda x,y : x^y, [1, 2, 2, 1, 4, 5, 5] )
Out[3]: 4