aarti.parikh
BAN USER
Comments (3)
Reputation -10
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 vote
The last line should be
p max_indices[rand(max_indices.length-1)]
Comment hidden because of low score. Click to expand.
0
of 0 vote
1. Generate a random array of integers
2. Loop through to find max and store max_indices in the array.
3. Generate a random number between 0 and max_indices length.
4. Return that value from the max_indices array.
a= []
250.times { a.push(rand(50)) }
max_indices = []
max = nil
a.each_with_index do |v,i|
max_indices.push( i ) if v == max
if max.nil? || v > max
max = v
max_indices = [i]
end
end
p max_indices[rand(max_indices.length)]
Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
1. Generate a random string
2. Generate 3 random numbers, the first between 0 and string length, the second between 1st number and string length and 3rd between 2nd number and string length.
3. Concatenate string indices to get the random triplet.
- aarti.parikh August 11, 2014