Skip to content

Instantly share code, notes, and snippets.

@deHelden
Created January 3, 2019 17:26
Show Gist options
  • Save deHelden/44666dd9e49afce699f3f585f1f82586 to your computer and use it in GitHub Desktop.
Save deHelden/44666dd9e49afce699f3f585f1f82586 to your computer and use it in GitHub Desktop.
[Task 3] Replace max element in array with minimum one
# 3) Дан целочисленный массив. Заменить все положительные элементы на значение минимального.
class Max2MinArrayElement
# Условие описано не ясно. Минимальных елементов здесь 2:
# Минимальное отрицательное
# Минимальное положительное
def initialize
@the_greatest_array = Array.new(15){rand(-100...100)}
end
# Решение первое. При минимальном отрицательном
def negative_change
element_with_min_negative_price = @the_greatest_array.min
@the_greatest_array.map!{|i| (i > 0) ? i = element_with_min_negative_price : i}
end
# Решение второе.При минимальном положительном
def positive_change
positive_array = []
@the_greatest_array.select{|e| (e > 0)? positive_array.push(e) : e }
element_with_min_positive_price = positive_array.min
@the_greatest_array.map!{|i| (i > 0) ? i = element_with_min_positive_price : i}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment