Todd Benson wrote:
> On Feb 2, 2008 11:05 PM, Todd Benson <> wrote:
>> a.inject(a.first) {|m, e| e < m ? e : m}
>> a.each_with_index {|v, i| s_indexes << i if v == a.min}
>
>
> Sorry, these lines are supposed to be...
>
> smallest = a.inject(a.first) {|m, e| e < m ? e : m}
> a.each_with_index {|v, i| s_indexes << i if v <= smallest}
>
> Todd
In Ruby 1.9, finding indexes of all elements which have minimal value
is easily done using a new iterator chaining feature:
a = 5, 1, 4, 7, 2, 1, 9, 2, 1
min_value = a.min
a.each_with_index.inject([]){|accum, (elem, index)|
elem == min_value ? accum << index : accum
}
Output:
=> [1, 5, 8]
Best regards
Isidor Nikolic
--
Posted via
http://www.ruby-forum.com/.