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.
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 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
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
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.
Problem statement:
You are given a set of k strings, each length n. You have to output the group of anagrams together. Anagrams are like e.g atm - mat , like-kile.
Just sort the word's letters to obtain a signature that's anagram-specific. E.g., in Python,
sig = ''.join(sorted(word))
and make a dict with sig as the key and the value being a list of words with that signature (defaultdict(list) works well for this). Of course, you can do it in any language with sorting abilities, and associative arrays whose values can be lists or vectors;-).