|
|
@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def indices_sorted_by_descending_values(array)
|
|
|
|
def indices_sorted_by_descending_values(array)
|
|
|
|
indices = array.each_with_index.to_a
|
|
|
|
indices = array.each_with_index.to_a
|
|
|
|
sorted_indices = indices.sort_by { |(element, index)| -element }
|
|
|
|
sorted_indices = indices.sort_by { |(element, index)| -element }
|
|
|
@ -5,7 +7,30 @@ def indices_sorted_by_descending_values(array)
|
|
|
|
result
|
|
|
|
result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def elements_between_first_and_second_max(array)
|
|
|
|
|
|
|
|
max_value = array.max
|
|
|
|
|
|
|
|
max_indices = array.each_with_index.select { |element, index| element == max_value }.map { |_, index| index }
|
|
|
|
|
|
|
|
if max_indices.size >= 2
|
|
|
|
|
|
|
|
first_max_index = max_indices[0]
|
|
|
|
|
|
|
|
second_max_index = max_indices[1]
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
second_max_value = array.select { |element| element != max_value }.max
|
|
|
|
|
|
|
|
return [] if second_max_value.nil?
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
first_max_index = array.index(max_value)
|
|
|
|
|
|
|
|
second_max_index = array.index(second_max_value)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
first_max_index, second_max_index = [first_max_index, second_max_index].sort
|
|
|
|
|
|
|
|
array[(first_max_index + 1)...second_max_index]
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Ex
|
|
|
|
# Ex
|
|
|
|
array = [5, 3, 8, 1, 7]
|
|
|
|
array = [5, 3, 8, 1, 7]
|
|
|
|
puts "Indices in order of decreasing elements:"
|
|
|
|
puts "Indices in order of decreasing elements:"
|
|
|
|
puts indices_sorted_by_descending_values(array).join(", ")
|
|
|
|
puts indices_sorted_by_descending_values(array).join(", ")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
array = [5, 1, 8, 3, 8, 7]
|
|
|
|
|
|
|
|
puts "Elements between first and second maximum:"
|
|
|
|
|
|
|
|
puts elements_between_first_and_second_max(array).join(", ")
|
|
|
|