How to find an odd number in an array? [closed] - algorithm

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

Related

How do I do a simple array to hash conversion? [closed]

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.

I need to reduce with average [closed]

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

Get two nibbles from a byte in Ruby [closed]

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

Cells counting problem [closed]

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.
There is a 3D grid with cells for which we define its value (0 or 1) as F(i,j,k)=F(i-1,j,k) XOR F(i,j-1,k) XOR F(i,j,k-1), base cases are F(0,0,0)=1 and F(x,y,z)=0 for x < 0 or y < 0 or z < 0 (if at least one parameter is negative).
We want to count amount of cells with F()=1 in the cube with corners (0,0,0) and (N, M, K). Dimensions are about 2^30.
I'm looking for the fast approach on this problem.

Group together all the anagrams [closed]

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;-).

Resources