Skip to content

Instantly share code, notes, and snippets.

@alexandersazonof
Last active December 16, 2018 18:12
Show Gist options
  • Save alexandersazonof/29eda5023417dd10582a8d5bc2e3617f to your computer and use it in GitHub Desktop.
Save alexandersazonof/29eda5023417dd10582a8d5bc2e3617f to your computer and use it in GitHub Desktop.
#Дан целочисленный массив. Преобразовать его, вставив перед каждым положительным элементом нулевой элемент
number_array = [1, -2, -1, 4]
sort_array = number_array.each_with_object([]) do |item , sort_array |
sort_array << 0 if item.positive?
sort_array << item
end
puts sort_array
@aya-soft
Copy link

aya-soft commented Dec 2, 2018

  1. можешь переписать это условие в виде модификатора?
    if item.positive?
    sort_array << 0
    end
  2. а зачем flatten тут? ты же добавляешь элементы по одному

@alexandersazonof
Copy link
Author

  1. можешь переписать это условие в виде модификатора?
    if item.positive?
    sort_array << 0
    end
  2. а зачем flatten тут? ты же добавляешь элементы по одному

1 sort_array << 0 if item.positive?
2 убрал

@aya-soft
Copy link

aya-soft commented Dec 6, 2018

ок, Придумай решение с методом flatten - еще не сделал

@alexandersazonof
Copy link
Author

ок, Придумай решение с методом flatten - еще не сделал

number_array = [1, -2, -1, 4]
sort_array = number_array.collect { |item| item > 0 ? [0, item] : item }.flatten
puts sort_array

@aya-soft
Copy link

🥇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment